UNPKG

1.46 kBJavaScriptView Raw
1///@ts-check
2'use strict';
3// 加载配置
4
5var fs = require('fs');
6// var minify = require('node-json-minify');
7var colors = require('colors');
8var log = require('fancy-log');
9var json5 = require('json5');
10
11var TITLE = colors.cyan('config:');
12
13var DEFAULT_CONFIG_FILES = [
14 'mpconfig.json',
15 'mpconfig.jsonc',
16 '.mpconfig.json',
17 '.mpconfig.jsonc',
18]
19
20/**
21 * 读取配置
22 * @param {string|any} configFile
23 */
24function 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 // process.exit(1);
39 throw ex;
40 }
41}
42
43module.exports.load = loadConfig;
44
45module.exports.default = function () {
46 // try load default configure file
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