1 |
|
2 | 'use strict';
|
3 |
|
4 |
|
5 | var fs = require('fs');
|
6 |
|
7 | var colors = require('colors');
|
8 | var log = require('fancy-log');
|
9 | var json5 = require('json5');
|
10 |
|
11 | var TITLE = colors.cyan('config:');
|
12 |
|
13 | var DEFAULT_CONFIG_FILES = [
|
14 | 'mpconfig.json',
|
15 | 'mpconfig.jsonc',
|
16 | '.mpconfig.json',
|
17 | '.mpconfig.jsonc',
|
18 | ]
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | function loadConfig(configFile) {
|
25 | if (configFile) {
|
26 | if (!fs.existsSync(configFile)) {
|
27 | log.error(TITLE, colors.red.underline(configFile), colors.bgRed('does not exist'));
|
28 | throw new Error(configFile + 'does not exist');
|
29 | }
|
30 | }
|
31 | try {
|
32 | var json = fs.readFileSync(configFile, 'utf-8');
|
33 | var config = json5.parse(json);
|
34 | log.info(TITLE, 'load configuration from', colors.blue.underline(configFile))
|
35 | return config;
|
36 | } catch (ex) {
|
37 | log.error(TITLE, colors.red.underline(configFile), 'failed to load.', colors.red(ex));
|
38 |
|
39 | throw ex;
|
40 | }
|
41 | }
|
42 |
|
43 | module.exports.load = loadConfig;
|
44 |
|
45 | module.exports.default = function () {
|
46 |
|
47 | for (var index = 0; index < DEFAULT_CONFIG_FILES.length; index++) {
|
48 | if (fs.existsSync(DEFAULT_CONFIG_FILES[index])) {
|
49 | return loadConfig(DEFAULT_CONFIG_FILES[index]);
|
50 | }
|
51 | }
|
52 | return {}
|
53 | } |
\ | No newline at end of file |