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