-
Notifications
You must be signed in to change notification settings - Fork 4
/
resolveBuildConfig.js
51 lines (42 loc) · 1.43 KB
/
resolveBuildConfig.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
var path = require('path'),
fs = require('fs'),
_ = require('lodash'),
extend = require('extend'),
minimist = require('minimist'),
clc = require('cli-color'),
moment = require('moment'),
config = require('../defaults/build.default.json');
module.exports = resolveBuildConfig;
function resolveBuildConfig () {
var localConfig;
try {
localConfig = fs.readFileSync(path.resolve('build.json'));
} catch (err) {
if (err.code != 'ENOENT') {
throw err;
}
}
if (localConfig) {
extend(true, config, JSON.parse(localConfig));
}
var args = minimist(process.argv),
hasModes = 'modes' in config,
argsMode = args.mode || args.m || 'default',
mode = argsMode != 'default' ? argsMode : (hasModes && _.findKey(config.modes, 'default')),
missingMode = hasModes && !mode,
wrongMode = mode && (!hasModes || typeof config.modes[mode] != 'object');
if (missingMode || wrongMode) {
throw new Error('You should specify one of modes listed in your `build.json`.');
}
if (mode) {
extend(true, config, config.modes[mode], { mode: mode });
}
console.log(
'[' + clc.blackBright(moment().format('HH:mm:ss')) + '] ' +
'Using ' +
clc.magenta(localConfig ? 'build.json' : 'build.default.json') +
' with mode ' +
clc.cyan(mode || 'default')
);
return config;
}