-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.ts
65 lines (52 loc) · 1.84 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
62
63
64
65
/* 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();
// CREATE ENV FILE
const contentFile = getFileContentWithModification('./src/environments/environment.exp.ts');
const envLocalFile = './src/environments/environment.ts';
if (!fs.existsSync(envLocalFile)) {
writeFileUsingFS(envLocalFile, contentFile);
}
const envProdFile = './src/environments/environment.prod.ts';
writeFileUsingFS(envProdFile, contentFile);
// CREATE MESSAGING FILE
const contentMessagingFile = getFileContentWithModification('./src/firebase-messaging-sw.exp.js');
const messagingLocalFile = './src/firebase-messaging-sw.js';
writeFileUsingFS(messagingLocalFile, contentMessagingFile);
function writeFileUsingFS(targetPath: string, environmentFileContent: string) {
fs.writeFile(targetPath, environmentFileContent, function (err: any) {
if (err) {
console.log(err);
}
if (environmentFileContent !== '') {
console.log(colors.magenta(`${targetPath} file generated correctly`));
}
});
}
function getFileContentWithModification(modelo: string) {
const regEx = /(\w+)\s?\:\s?(:\w+|'.*'),?/gm;
const file = fs.readFileSync(modelo, 'utf8');
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
let attrName = attr;
var attrValue: string | undefined = '';
if (attr === 'databaseURL') {
attrValue = process.env.DATABASE_URL;
} else {
attrValue = process.env[envVar];
}
if (vle) {
vle = `'${vle}'`;
}
return `${attrName}: '${attrValue}',`;
});
return envConfigFile;
}