-
Notifications
You must be signed in to change notification settings - Fork 0
/
api-secret.js
62 lines (50 loc) · 1.38 KB
/
api-secret.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
const path = require('node:path');
const { readFileSync, writeFileSync } = require('node:fs');
let envBuildFile;
try {
envBuildFile = readFileSync(path.normalize('./env.build.json'), { encoding: 'utf8' });
} catch (error) {
console.error(error);
process.exit(-1);
}
const envBuildData = JSON.parse(envBuildFile);
const APIPath = path.normalize(envBuildData.API_PATH);
let APIEnvFile;
try {
APIEnvFile = readFileSync(path.join(APIPath, '.env'), { encoding: 'utf8' });
} catch (error) {
console.error(error);
process.exit(-1);
}
const APIEnvData = APIEnvFile.toString();
const lines = APIEnvData.split(/\r\n|\r|\n/g);
let APIKeyLine = null;
for (const line of lines) {
if (line.includes('API_KEY = ')) {
APIKeyLine = line;
break;
}
}
let key = null;
if (!(APIKeyLine === null)) {
const regexp = /(?<=")[^"]*(?=")/;
key = APIKeyLine.match(regexp)[0];
}
const envAppFilePath = path.normalize('./env.app.json');
let envAppFile;
try {
envAppFile = readFileSync(envAppFilePath, { encoding: 'utf8' });
} catch (error) {
console.error(error);
process.exit(-1);
}
let envAppData = JSON.parse(envAppFile);
envAppData.API_KEY = key;
envAppData = JSON.stringify(envAppData, null, 2);
try {
writeFileSync(envAppFilePath, envAppData, { encoding: 'utf8' });
} catch (error) {
console.error(error);
process.exit(-1);
}
console.log('--> Secret successfully retrieved from API');