1 | const CryptoJS = require("crypto-js");
|
2 | const chalk = require("chalk");
|
3 | var inquirer = require("inquirer");
|
4 |
|
5 | const {
|
6 | appendToFile,
|
7 | ensureFile,
|
8 | writeFile,
|
9 | boxInform,
|
10 | readJson,
|
11 | readFile
|
12 | } = require("../helpers.js");
|
13 | const path = require("path");
|
14 | const scriptsDir = process.cwd();
|
15 | const rootDir = path.join(scriptsDir, "./");
|
16 | const packagePath = path.join(rootDir, "package.json");
|
17 | const igPath = path.join(rootDir, ".gitignore");
|
18 | const prettier = require("prettier");
|
19 | const encrypt = { encryptedFiles: [] };
|
20 |
|
21 |
|
22 | encrypt.encrypt = async (pass, encryptedFile, decryptedFile) => {
|
23 | let willEncrypt = true;
|
24 | let sureSure = await ensureFile(encryptedFile);
|
25 | if (sureSure) {
|
26 | console.log(`${chalk.bold.red("FILE ALREADY EXISTS ARE YOU SURE?")}`);
|
27 | willEncrypt = await new Promise(resolve => {
|
28 | inquirer
|
29 | .prompt([
|
30 | {
|
31 | type: "confirm",
|
32 | message: chalk.bold.hex("#38be18")(`Sure? (y/n): `),
|
33 | name: "sure"
|
34 | }
|
35 | ])
|
36 | .then(({ sure }) => {
|
37 | resolve(sure);
|
38 | });
|
39 | });
|
40 | }
|
41 | if (willEncrypt) {
|
42 | let toEncrypt = await readFile(decryptedFile);
|
43 | const ciphertext = CryptoJS.AES.encrypt(toEncrypt, pass);
|
44 | await writeFile(encryptedFile, ciphertext.toString());
|
45 | }
|
46 |
|
47 | };
|
48 |
|
49 | encrypt.decrypt = async (pass, encryptedFileLocation, decryptedFileLocation) => {
|
50 |
|
51 |
|
52 | let willEncrypt = true;
|
53 | let sureSure = await ensureFile(decryptedFileLocation);
|
54 | if (sureSure) {
|
55 | console.log(`${chalk.bold.red("FILE ALREADY EXISTS ARE YOU SURE?")}`);
|
56 | willEncrypt = await new Promise(resolve => {
|
57 | inquirer
|
58 | .prompt([
|
59 | {
|
60 | type: "confirm",
|
61 | message: chalk.bold.hex("#38be18")(`Sure wanna override file ${decryptedFileLocation}? (y/n): `),
|
62 | name: "sure"
|
63 | }
|
64 | ])
|
65 | .then(({ sure }) => {
|
66 | resolve(sure);
|
67 | });
|
68 | });
|
69 | }
|
70 | if (willEncrypt) {
|
71 | let toDecrypt = await readFile(encryptedFileLocation);
|
72 | const bytes = CryptoJS.AES.decrypt(toDecrypt, pass);
|
73 | let decryptedData;
|
74 | try {
|
75 | decryptedData = bytes.toString(CryptoJS.enc.Utf8);
|
76 | } catch (e) {
|
77 | console.error("-- Console Problem ", e);
|
78 | }
|
79 | await writeFile(decryptedFileLocation, decryptedData);
|
80 | console.warn(`${chalk.bold.green.underline("DECRYPTED FILE:")} ${chalk.bold.dim(path.join(scriptsDir, decryptedFileLocation))}`);
|
81 | }
|
82 |
|
83 | };
|
84 |
|
85 | encrypt.init = async () => {
|
86 | try {
|
87 | let pass = await encrypt.getPass();
|
88 | let toEncrypt = await encrypt.toEncrypt();
|
89 | encrypt.packageJson = await readJson(packagePath);
|
90 | encrypt.ignore = await readFile(igPath);
|
91 | encrypt.ignoredFiles = encrypt.ignore.split("\n");
|
92 | if (encrypt.packageJson.fscripts) {
|
93 | if (encrypt.packageJson.fscripts.encryptedFiles) {
|
94 | encrypt.encryptedFiles = encrypt.packageJson.fscripts.encryptedFiles;
|
95 | let filesToAdd = "\n";
|
96 | for (const e of encrypt.encryptedFiles) {
|
97 | if (encrypt.ignoredFiles.indexOf(e) === -1) {
|
98 | filesToAdd += e + "\n";
|
99 | }
|
100 | let file = e + "";
|
101 | let fileSplit = file.split("/");
|
102 | let name = fileSplit.pop();
|
103 | let encryptedFile = fileSplit.slice();
|
104 | let decryptedFile = fileSplit.slice();
|
105 | encryptedFile.push("." + name);
|
106 | encryptedFile = path.join(rootDir, encryptedFile.join("/"));
|
107 | decryptedFile.push(name);
|
108 | decryptedFile = path.join(rootDir, decryptedFile.join("/"));
|
109 |
|
110 | if (toEncrypt) {
|
111 | await encrypt.encrypt(pass, encryptedFile, decryptedFile);
|
112 | } else {
|
113 | await encrypt.decrypt(pass, encryptedFile, decryptedFile);
|
114 | }
|
115 | }
|
116 |
|
117 | if (filesToAdd.trim().length > 0) {
|
118 | await appendToFile(igPath, filesToAdd + "\n");
|
119 | boxInform(" Added files to .gitignore: ", filesToAdd);
|
120 | }
|
121 | }
|
122 | }
|
123 | } catch (err) {
|
124 | console.error(err);
|
125 | }
|
126 | };
|
127 |
|
128 | encrypt.getPass = async () => {
|
129 | return await new Promise(resolve => {
|
130 | inquirer
|
131 | .prompt([
|
132 | {
|
133 | type: "password",
|
134 | mask: chalk.underline(" ●"),
|
135 | message: chalk.bold.hex("#38be18")(`Enter a SECRET key (same as pass app) : `),
|
136 | name: "pass"
|
137 | }
|
138 | ])
|
139 | .then(({ pass }) => {
|
140 | resolve(pass);
|
141 | });
|
142 | });
|
143 | };
|
144 | encrypt.toEncrypt = async () => {
|
145 | return await new Promise(resolve => {
|
146 | inquirer
|
147 | .prompt([
|
148 | {
|
149 | type: "list",
|
150 | message: chalk.bold.hex("#38be18")("Which direction?"),
|
151 | choices: ["encrypt", "decrypt"],
|
152 | name: "encryptDecrypt"
|
153 | }
|
154 | ])
|
155 | .then(async ({ encryptDecrypt }) => {
|
156 | resolve(encryptDecrypt === "encrypt");
|
157 | });
|
158 | });
|
159 | };
|
160 |
|
161 | module.exports = { ...encrypt };
|