-
Notifications
You must be signed in to change notification settings - Fork 0
/
production.js
153 lines (135 loc) · 4.65 KB
/
production.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const util = require("util");
const exec = util.promisify(require("child_process").exec);
const fs = require("fs");
function fancyLog(text, type = "success") {
let logText;
if (type === "success") {
logText = `\x1b[0;32m✓ ${text}\x1b[0m`;
} else if (type === "warn") {
logText = `\x1b[0;33m⚠ ${text}\x1b[0m`;
} else {
logText = `\x1b[0;31m× Error: ${text}\x1b[0m`;
}
console.log(logText);
}
async function question(text) {
const readline = require("readline").createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise((resolve) => {
readline.question(text, (answer) => {
readline.close();
resolve(answer.trim());
});
});
}
async function executeCommand(command, options = {}, successMessage) {
try {
let commandThrewFatalError = false;
const { stdout, stderr } = await exec(command);
if (stderr && Object.keys(options).length > 0) {
const specialKeys = Object.keys(options);
let errorHandled = false;
for (const line of stderr.split("\n")) {
let errorFlagged = false;
for (const opkey of specialKeys) {
if (line.includes(opkey) && options[opkey] !== "ignore") {
let plainlog = options[opkey].split("---")[0];
let type = options[opkey].split("---")[1] || "error";
if (type !== "warn") {
commandThrewFatalError = true;
}
fancyLog(plainlog, type);
errorFlagged = true;
break; // Exit loop since the error was flagged
}
}
if (!errorFlagged && !errorHandled) {
// Unhandled fatal error
let commandname = command.split(" ")[0];
fs.appendFileSync("latest.log", options[opkey] !== "ignore" ? stderr.trim() + "\n" : `'${commandname}' logged to stderr but was ignored\n`);
commandThrewFatalError = options[opkey] !== "ignore";
fancyLog(options[opkey] !== "ignore" ? `'${commandname}' threw a fatal unhandled error, check latest.log` : `${commandname} logged to stderr but was ignored`, "error");
errorHandled = true;
}
}
}
if (!commandThrewFatalError && successMessage) {
fancyLog(successMessage, "success");
}
} catch (flies) {
// Prevent errors escalating to shell/console
}
}
async function remoteCommand(webSourcePass) {
await executeCommand(
"find ./src -name '.DS_Store' -type f -delete 2>/dev/null",
);
const response = await question("Are you sure you want to proceed? (y/n): ");
if (response === "y") {
await executeCommand("zip -r src.zip ./src -x '*.DS_Store'");
await executeCommand(
`openssl enc -aes-256-cbc -salt -in src.zip -out source_encrypted.enc -pass pass:${webSourcePass}`,
{ deprecated: "Encryption method is deprecated---warn", better:"ignore" },
"Encryption succeeded",
);
await executeCommand("rm src.zip");
await executeCommand(
`git commit -a -m "${await question("Enter a custom commit message: ")}"`,
{},
"Files successfully committed",
);
await executeCommand(
"git push 2>&1 >/dev/null",
{
hint: "ignore",
To: "ignore",
rejected: "Upstream changes, rebase might be required---warn",
"failed to push": "Push failed",
},
"Files successfully pushed",
);
}
}
async function decryptCommand(webSourcePass) {
await executeCommand(
`openssl enc -d -aes-256-cbc -in source_encrypted.enc -out src.zip -pass pass:${webSourcePass}`,
{ deprecated: "Encryption method is deprecated---warn" },
"Decryption succeeded",
);
await executeCommand("unzip src.zip");
await executeCommand("rm src.zip");
}
async function encryptCommand(webSourcePass) {
await executeCommand("zip -r src.zip ./src -x '*.DS_Store'");
await executeCommand(
`openssl enc -aes-256-cbc -salt -in src.zip -out source_encrypted.enc -pass pass:${webSourcePass}`,
{ deprecated: "Encryption method is deprecated---warn" },
"Encryption succeeded",
);
await executeCommand("rm src.zip");
}
const args = process.argv.slice(2);
const command = args[0];
(async () => {
let webSourcePass =
process.env.web_source_pass ||
process.env["web_source_pass"] ||
(await question("Enter encryption password: "));
await executeCommand("rm latest.log", "ignore");
switch (command) {
case "remote":
await remoteCommand(webSourcePass);
break;
case "decrypt":
await decryptCommand(webSourcePass);
break;
case "encrypt":
await encryptCommand(webSourcePass);
break;
default:
fancyLog("You're in my website, and I'm in your walls >:]", "warn");
break;
}
})();