-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcmd.js
61 lines (49 loc) · 1.86 KB
/
cmd.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
const fs = require('fs');
const path = require('path')
const { randomUUID } = require('crypto');
const allowedExtensions = ['.json', '.yml', '.yaml', '.csv', '.xml', '.properties', '.txt', '.html', '.htm'];
let clients = [];
if (process.env.CLIENTS) {
clients = process.env.CLIENTS.split(',');
}
const processDirs = (dirPath) => {
fs.readdirSync(dirPath).forEach(item => {
const itemAbsPath = `${dirPath}/${item}`;
const itemStat = fs.statSync(itemAbsPath);
if (itemStat.isDirectory()) {
processDirs(itemAbsPath);
} else {
if (allowedExtensions.includes(path.extname(itemAbsPath))) {
fileEnvSubstitution(itemAbsPath);
}
}
});
};
const fileEnvSubstitution = (itemAbsPath) => {
let hydratedTemplate = fs.readFileSync(itemAbsPath, 'utf8');
Object.entries(process.env).forEach(([name, value]) => {
if (typeof value !== 'string') {
// stringify strings adds surrounding quotes; we don't want that
value = JSON.stringify(value)
}
// replace ${name}
hydratedTemplate = hydratedTemplate.replace(new RegExp(`\\$\{${name}}`, 'g'), () => value);
// replace $name
hydratedTemplate = hydratedTemplate.replace(new RegExp(`\\$${name}`, 'g'), () => value);
});
fs.writeFileSync(itemAbsPath, hydratedTemplate);
};
const generateClientUuidAndSecrets = (clients) => {
for (let client of clients) {
const key = `${client.toUpperCase()}_CLIENT_SECRET`;
if(!(key in process.env)){
process.env[key] = randomUUID();
}
const clientUuidKey = `${client.toUpperCase()}_CLIENT_UUID`;
if(!(clientUuidKey in process.env)){
process.env[clientUuidKey] = randomUUID();
}
}
}
generateClientUuidAndSecrets(clients);
processDirs(process.env.FILE_PATH);