-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.ts
61 lines (50 loc) · 1.58 KB
/
env.ts
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
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/no-var-requires */
const fs = require('fs');
const colors = require('colors');
const dotenv = require('dotenv');
dotenv.config();
// DEFAULT FILE NAMES
const targetLocal = './src/environments/environment.ts';
const targetProd = './src/environments/environment.prod.ts';
const targetExep = './src/environments/environment.exp.ts';
// READE EXAMPLE FILE
const file = fs.readFileSync(targetExep, 'utf8');
// SEARCH IN FILE EVERY ATTR
const regEx = /(\w+)\s?\:\s?(:\w+|'.*'),?/gm;
const envConfigFile = file.replace(regEx, (_match: string, attr: string, vle: string) => {
// TRANSFORM ATTR IN SNAKE CASE
const envVar = attr
.split(/(?=[A-Z])/)
.join('_')
.toUpperCase();
// FORMAT VALUE
const attrName = attr;
let attrValue = '';
if (attr === 'databaseURL') {
attrValue = process.env.DATABASE_URL;
} else {
attrValue = process.env[envVar];
}
if (vle) {
vle = `'${vle}'`;
}
return `${attrName}: '${attrValue}',`;
});
// IF NOT EXIST LOCAL ENVERIONMENT
if (!fs.existsSync(targetLocal)) {
writeFileUsingFS(targetLocal, envConfigFile);
}
// FORCE REWRITE PROD ENVERIOMENT
writeFileUsingFS(targetProd, envConfigFile);
// NODE FUNCTIONS TO WRITE FILE
function writeFileUsingFS(targetPath, environmentFileContent) {
fs.writeFile(targetPath, environmentFileContent, function (err) {
if (err) {
console.log(err);
}
if (environmentFileContent !== '') {
console.log(colors.magenta(`Environment file generated correctly at ${targetPath}`));
}
});
}