-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·63 lines (56 loc) · 1.79 KB
/
cli.js
File metadata and controls
executable file
·63 lines (56 loc) · 1.79 KB
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
#!/usr/bin/env node
'use strict';
const walk = require('walk');
const path = require('path');
const fs = require('fs');
const directoryExists = require('directory-exists');
const folderDelete = require('folder-delete');
const { ncp } = require('ncp');
(async () => {
if (process.argv.length <= 2) {
console.error('Not enough args');
return;
}
const [,, folder] = process.argv;
const folderBasePath = path.resolve(folder);
const folderBackupPath = path.resolve(folder + '_bak');
const backupExists = await directoryExists(folderBackupPath);
if (backupExists) {
console.log('Backup folder exists. Restoring backup.');
folderDelete(folderBasePath);
await new Promise(resolve =>
ncp(folderBackupPath, folderBasePath, resolve)
);
} else {
console.log('Backup folder does NOT exists. Creating backup.');
await new Promise(resolve =>
ncp(folderBasePath, folderBackupPath, resolve)
);
}
const walker = walk.walk(folderBasePath, { followLinks: false });
console.log('Injecting environment config\n');
const envVarsFound = {};
walker.on('file', (root, stat, next) => {
const fileName = root + '/' + stat.name;
if (fileName.endsWith('.html')) {
let content = fs.readFileSync(fileName).toString();
for (let key in process.env) {
const newContent = content.replace(
new RegExp('\\${' + key + '}', 'gi'),
process.env[key]
);
if (newContent !== content) envVarsFound[key] = process.env[key];
content = newContent;
}
fs.writeFileSync(fileName, content);
console.log('✅ ' + fileName);
}
next();
});
walker.on('end', () => {
console.log('\nSummary\n');
for (let key in envVarsFound) {
console.log(key + ' ➡️ ' + envVarsFound[key]);
}
});
})();