-
Notifications
You must be signed in to change notification settings - Fork 0
/
env2config.js
51 lines (40 loc) · 1.21 KB
/
env2config.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
const fs = require('fs');
const reactPrefix = 'REACT_APP_';
const files = ['.env'];
const env = {};
switch (process.env.NODE_ENV) {
case 'production':
files.push('.env.production', '.env.production.local');
break;
case 'test':
files.push('.env.test', '.env.test.local');
break;
case 'development':
default:
files.push('.env.development', '.env.development.local');
}
files.forEach(filename => {
if (!fs.existsSync(filename)) return;
fs.readFileSync(filename).toString()
.split("\n")
.map(s => s.trim())
.filter(entry => entry.length > 0)
.filter(entry => entry.substr(0, 1) !== '#')
.forEach(entry => {
const chunks = entry.split('=');
const name = chunks.shift();
if (!isReactVariableName(name)) return;
env[getReactVariableBasename(name)] = chunks.join();
})
});
Object.entries(process.env)
.filter(([key]) => isReactVariableName(key))
.forEach(([key, value]) => env[key] = value);
const data = `window.APP_CONFIG = ${JSON.stringify(env)};`;
fs.writeFileSync(process.argv[2], data, {flag: 'w+'});
function isReactVariableName(v) {
return v.substr(0, reactPrefix.length) === reactPrefix;
}
function getReactVariableBasename(v) {
return v.substr(reactPrefix.length);
}