UNPKG

1.53 kBJavaScriptView Raw
1if (!process.env.NODE_CONFIG_DIR) {
2 process.env.NODE_CONFIG_DIR = require('path').join(__dirname, '..', 'config');
3}
4
5const
6 fs = require('fs'),
7 path = require('path'),
8 config = require('config');
9
10class ConfigManager {
11
12 constructor() {
13
14 this.config = config;
15 this.util = config.util;
16
17 this.copyExtend = this.copyExtend.bind(this);
18 }
19
20 extend(config) {
21 this.config = this.util.extendDeep({}, this.config, config);
22 }
23
24 copyExtend(config) {
25 const conf = {
26 config: this.util.extendDeep({}, this.util.cloneDeep(this.config), config),
27 };
28 conf.has = this.has.bind({config: conf.config});
29 conf.get = this.get.bind({has: conf.has, config: conf.config});
30 return conf;
31 }
32
33 initConfig(config) {
34
35 const rootPath = process.cwd();
36
37 if (typeof config === 'string') {
38 config = require(path.join(rootPath, config));
39 }
40
41 this.extend(config);
42 this.extend({
43 rootPath,
44 viewerDest: path.join(this.get('dest'), this.get('app.viewerRootPath')),
45 package: JSON.parse(fs.readFileSync('./package.json')),
46 globOptions: {
47 cache:{},
48 statCache: {},
49 symlinks: {},
50 nodir: true,
51 nocase: true,
52 absolute: false
53 }
54 });
55 return this;
56 }
57
58 get(configPath, defaultValue) {
59 return ('undefined' !== typeof defaultValue && !this.has(configPath)) ? defaultValue : this.config.get(configPath);
60 }
61
62 has(configPath) {
63 return this.config.has(configPath);
64 }
65}
66
67module.exports = new ConfigManager();
\No newline at end of file