-
Notifications
You must be signed in to change notification settings - Fork 0
/
replace.js
executable file
·59 lines (50 loc) · 1.79 KB
/
replace.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
#!/usr/bin/env node
/*
Command line utility for automatically injecting environment variables into files.
*/
'use strict';
const readline = require('readline');
const fs = require('fs');
const { EOL } = require('os');
let filename;
if (process.argv.length === 3) {
filename = process.argv[2];
console.log("Filename:", filename);
if (!fs.existsSync(filename)) {
console.log("Error: file does not exists");
process.exit(1);
}
// Create copy of file to use for attempted string replacement - Keep file permissions the same
// Fail if file already exists
fs.copyFileSync(filename, filename + '-new', fs.constants.COPYFILE_EXCL);
const readInterface = readline.createInterface({
input: fs.createReadStream(filename),
output: fs.createWriteStream(filename + '-new'),
terminal: false
});
readInterface.on('line', function(line) {
if (line) {
const regex = /%[a-zA-Z_][a-zA-Z0-9_]*%/g;
let envArray = line.match(regex);
if (envArray && envArray.length) {
for (let e of envArray) {
let envValue = process.env[e.slice(1, -1)];
if (envValue) {
line = line.replace(e, envValue);
}
}
}
this.output.write(`${line}${EOL}`);
} else {
this.output.write(EOL);
}
});
readInterface.on('close', function (event) {
// Rename updated version of file to original overwriting original
fs.renameSync(filename + "-new", filename)
console.log(`Succesfully updated ${filename}`);
})
} else {
console.log("Error: must receive single argument containing filename");
console.log("Usage: 'replace-env <filename.txt>'");
}