-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverride.js
57 lines (52 loc) · 1.46 KB
/
override.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
/*eslint no-console: off */
const fs = require('fs');
const path = require('path');
const utils = require('./utils');
const fsx = require('./utils/fs');
const envs = utils.env(process.env.ENV_FILE || '.env', process.env.ENV_INJECT);
let dirname = process.env.CONFIG_FILE || envs.CONFIG_FILE;
if (!dirname) {
dirname = path.resolve(process.cwd(), 'config');
} else if (!path.isAbsolute(dirname)) {
dirname = path.resolve(process.cwd(), dirname);
}
let config;
if (!fsx.isDirectory(dirname)) {
console.warn('config directory not found at path "%s"', dirname);
config = {};
} else {
const env = process.env.NODE_ENV;
const basefile = path.join(dirname, 'default.json');
const envfile = path.join(dirname, env + '.json');
if (fs.existsSync(basefile)) {
config = utils.load(basefile, envs);
}
if (fs.existsSync(envfile)) {
const envconfig = utils.load(envfile, envs);
if (config) {
config = utils.merge(config, envconfig);
} else {
config = envconfig;
}
}
if (!config) {
console.warn(
'no configs loaded with env "%s" at path "%s"',
env,
dirname
);
config = {};
}
}
const __ = Object.assign(
{ desolve: () => delete require.cache[__filename] },
utils
);
Object.defineProperty(config, '__', {
configurable: false,
enumerable: false,
get() {
return __;
}
});
module.exports = config;