-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·335 lines (291 loc) · 8.29 KB
/
index.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/env node
const fs = require("fs").promises;
let args = null;
let allowList = [];
class ReplaceError extends Error {}
function printHelp() {
console.log(`
Usage ./env-at-startup <file>... [options]
Options:
--help Show this screen.
-v --verbose Show all replacements.
--vars Only replace these vars. Comma separated list, wildcards (*) allowed.
--ignore-other When using --vars, all other vars are ignored (by default we error out).
--allow-missing Missing env vars are set to undefined (by default we error out).
--rollback Rollback all replacements.
--debug Show debug logs
Examples:
./env-at-startup dist/*.js --vars 'API_URL,NEXT_PUBLIC_*'
./env-at-startup dist/*.js --rollback
Use 'find' to access files recursively:
./env-at-startup $(find . -name "*.js")
`);
}
function parseArgs(argv) {
const [_node, _script, ...args] = argv;
const files = [];
const flags = {};
let currentFlag = null;
args.forEach((arg) => {
if (currentFlag) {
flags[currentFlag] = arg;
currentFlag = null;
return;
}
if (arg.startsWith("-")) {
switch (arg) {
case "--help":
printHelp();
process.exit(0);
break;
case "--vars":
currentFlag = "vars";
break;
case "--ignore-other":
flags.ignoreOther = true;
break;
case "--allow-missing":
flags.allowMissing = true;
break;
case "--rollback":
flags.rollback = true;
break;
case "-v":
case "--verbose":
flags.verbose = true;
break;
case "--debug":
flags.debug = true;
break;
default:
console.error("Unknown flag:", arg);
process.exit(1);
break;
}
} else {
files.push(arg);
}
});
return {
files,
flags,
};
}
function parseAllowList(flagVars = "") {
return flagVars
.split(",")
.filter(Boolean)
.map((pattern) => new RegExp("^" + pattern.replace("*", ".*") + "$"));
}
function isAllowed(varName) {
if (allowList.length === 0) {
return true;
}
return allowList.some((regex) => regex.test(varName));
}
function getPosInFile(string, offset) {
let line = 1;
let col = 1;
for (let index = 0; index < Math.min(string.length, offset); index++) {
const char = string[index];
if (char == "\n") {
line += 1;
col = 1;
continue;
}
if (char == "\r") {
// Ignore CRLF
continue;
}
col += 1;
}
return { col, line };
}
async function execSubstitution(filePath) {
const stats = await fs.stat(filePath);
if (!stats.isFile()) {
return "SKIPPED";
}
const replacements = {};
let count = 0;
const contentBuffer = await fs.readFile(filePath);
const content = contentBuffer.toString("utf8");
const changes = [];
const newContent = content.replace(
/\bprocess\.env\.([\w\d_]+)\b/g,
(matchStr, varName, offset) => {
replacements[varName] = replacements[varName] || 0;
if (isAllowed(varName)) {
if (process.env[varName]) {
replacements[varName] = replacements[varName] + 1;
count += 1;
const replacement = JSON.stringify(process.env[varName]);
changes.push({ matchStr, offset, replacement });
return replacement;
} else if (args.flags.allowMissing) {
replacements[varName] = replacements[varName] + 1;
count += 1;
const replacement = "undefined";
changes.push({ matchStr, offset, replacement });
return replacement;
} else {
throw new ReplaceError(`'${varName}' is not set.`);
}
} else if (args.flags.ignoreOther) {
if (args.flags.debug) {
console.warn("Skipping", matchStr, "in", filePath);
}
return matchStr;
}
throw new ReplaceError(`'${matchStr}' missing in --vars`);
}
);
if (newContent === content) {
return "UNTOUCHED";
}
if (args.flags.verbose) {
changes.forEach((change) => {
const { line, col } = getPosInFile(content, change.offset);
console.log(`${filePath}:${line}:${col}`);
console.log(change.matchStr, "->", change.replacement);
console.log();
});
}
const backupPath = filePath + ".envs";
await fs.writeFile(backupPath, contentBuffer);
await fs.writeFile(filePath, newContent, "utf8");
return { replacements, count };
}
async function rollback(filePath) {
const stats = await fs.stat(filePath);
if (!stats.isFile()) {
return "SKIPPED";
}
const backupPath = filePath + ".envs";
try {
const statEnv = await fs.stat(backupPath);
if (!statEnv.isFile()) {
throw new ReplaceError("Not a file " + backupPath);
return "NOT_A_FILE";
}
} catch (error) {
return "NO_BAK";
}
const contentBuffer = await fs.readFile(filePath + ".envs");
await fs.writeFile(filePath, contentBuffer);
await fs.rm(filePath + ".envs");
return "ROLLED_BACK";
}
function printUpdate(file, result) {
if (result instanceof Error) {
console.debug(" -", file, "FAILED");
} else {
if (typeof result === "string") {
console.debug(" -", file, result);
} else if (typeof result === "object") {
console.debug(" -", file, "REPLACED", result.count);
} else {
console.debug(" -", file, "DONE");
}
}
}
async function runAll(list, func) {
const errors = [];
const done = [];
const proms = list.map(func).map((prom, index) =>
prom
.then((result) => {
if (args.flags.debug) printUpdate(list[index], result);
done.push({ index, result });
return result;
})
.catch((error) => {
if (args.flags.debug) printUpdate(list[index], error);
errors.push({ index, error });
})
);
const results = await Promise.all(proms);
return { errors, done, results };
}
function printErrors(errors) {
console.error("Failed files:");
errors.forEach(({ index, error }) => {
console.error(" -", args.files[index], `(${error.message})`);
});
if (!(errors[0].error instanceof ReplaceError)) {
console.error();
console.error("The first error was:");
console.error(errors[0].error);
}
}
async function main() {
args = parseArgs(process.argv);
allowList = parseAllowList(args.flags.vars);
if (args.flags.rollback) {
const { errors, results } = await runAll(args.files, (file) =>
rollback(file)
);
if (errors.length > 0) {
console.error(
`Rollback failed on ${errors.length}/${args.files.length} files`
);
console.error();
printErrors(errors);
process.exit(1);
} else {
console.log(`Finished rollback on ${args.files.length} files`);
const stats = {};
results.forEach((resCode) => {
if (typeof resCode === "string") {
stats[resCode] = (stats[resCode] || 0) + 1;
}
});
Object.entries(stats).forEach(([k, v]) => {
console.log(" -", k, `${v} times`);
});
process.exit(0);
}
}
const { errors, results } = await runAll(args.files, (file) =>
execSubstitution(file)
);
if (errors.length > 0) {
console.error(
`Substituting environment variables failed on ${errors.length}/${args.files.length} files`
);
const replaced = results.filter((res) => typeof res === "object");
if (replaced.length > 0) {
console.error("");
console.error(
`IMPORTANT: Some files where still updated. Run with --rollback to undo changes on ${replaced.length} files.`
);
}
console.error();
printErrors(errors);
process.exit(1);
} else {
console.log(
`Finished substituting environment variables in ${args.files.length} files`
);
const replacements = {};
results.forEach((res) => {
if (typeof res === "object") {
Object.entries(res.replacements).forEach(([k, v]) => {
replacements[k] = (replacements[k] || 0) + v;
});
}
});
Object.entries(replacements).forEach(([k, v]) => {
console.log(" -", k, `replaced ${v} times`);
});
if (Object.values(replacements).length === 0) {
console.log("No strings replaced");
}
}
}
main().catch((error) => {
console.error("Uncaught async error:");
console.error();
console.error(error);
process.exit(1);
});