forked from taskcluster/taskcluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-env-js.js
46 lines (40 loc) · 1.43 KB
/
generate-env-js.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
const { dirname } = require('path');
const fs = require('fs');
const ENV_VARS = [
{name: 'APPLICATION_NAME', defaultValue: 'Taskcluster', json: false},
{name: 'TASKCLUSTER_ROOT_URL', defaultValue: 'https://tc.example.com', json: false},
{name: 'GRAPHQL_ENDPOINT', defaultValue: '/graphql', json: false},
{name: 'GRAPHQL_SUBSCRIPTION_ENDPOINT', defaultValue: '/subscription', json: false},
{name: 'DOCS_ONLY', defaultValue: false, json: false},
{name: 'UI_LOGIN_STRATEGY_NAMES', defaultValue: '', json: false},
{name: 'GA_TRACKING_ID', defaultValue: '', json: false},
{name: 'SENTRY_DSN', defaultValue: '', json: false},
{name: 'BANNER_MESSAGE', defaultValue: '', json: false},
{name: 'SITE_SPECIFIC', defaultValue: {}, json: true},
];
/**
* Generate `env.js` in the static directory based on the current
* environment variables.
*/
const generateEnvJs = filename => {
const env = {};
for (const {name, defaultValue, json} of ENV_VARS) {
if (process.env[name]) {
env[name] = json ? JSON.parse(process.env[name]) : process.env[name];
} else {
env[name] = defaultValue;
}
}
const envJs = `window.env = ${JSON.stringify(env, null, 2)}`;
const dir = dirname(filename);
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}
if (!fs.existsSync(filename)){
fs.writeFileSync(filename, envJs, 'utf8');
}
};
module.exports = generateEnvJs
if (require.main === module) {
generateEnvJs(process.argv[2]);
}