This repository has been archived by the owner on May 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathget-env.js
57 lines (52 loc) · 1.48 KB
/
get-env.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
/** Copyright (c) 2018 Uber Technologies, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
/* eslint-env node */
import assert from 'assert';
export default (__BROWSER__ ? () => {} : loadEnv());
function load(key, value) {
return process.env[key] || value;
}
export function loadEnv() {
const rootDir = load('ROOT_DIR', '.');
const env = load('NODE_ENV', 'development');
if (!(env === 'development' || env === 'production' || env === 'test')) {
throw new Error(`Invalid NODE_ENV loaded: ${env}.`);
}
const prefix = load('ROUTE_PREFIX', '');
assert(!prefix.endsWith('/'), 'ROUTE_PREFIX must not end with /');
const baseAssetPath = load('FRAMEWORK_STATIC_ASSET_PATH', `/_static`);
assert(
!baseAssetPath.endsWith('/'),
'FRAMEWORK_STATIC_ASSET_PATH must not end with /'
);
const cdnUrl = load('CDN_URL', '');
assert(!cdnUrl.endsWith('/'), 'CDN_URL must not end with /');
const assetPath = `${prefix}${baseAssetPath}`;
return function loadEnv(): Env {
return {
rootDir,
env,
prefix,
assetPath,
baseAssetPath,
cdnUrl,
webpackPublicPath: cdnUrl || assetPath,
};
};
}
// Handle flow-types for export so browser export is ignored.
type Env = {
rootDir: string,
env: string,
prefix: string,
assetPath: string,
baseAssetPath: string,
cdnUrl: string,
webpackPublicPath: string,
};
declare export default () => Env;