-
Notifications
You must be signed in to change notification settings - Fork 160
/
findConfig.js
83 lines (69 loc) · 1.7 KB
/
findConfig.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
var fs = require('fs')
var pack = require('reapp-pack');
require('reapp-object-assign');
function moduleExists(path) {
try {
require.resolve(path);
return true;
} catch (e) {
return false;
}
}
function requireFirst(paths, getDefault) {
if (typeof getDefault === 'undefined') {
getDefault = function() { return null; };
}
if (paths.length === 0) {
return getDefault();
}
// Try to resolve all alternatives except the last one
var path, i;
for (i = 0; i < paths.length; i++) {
path = paths[i];
if (moduleExists(path)) {
return require(path);
}
}
return getDefault();
}
module.exports = function(opts) {
var config;
var defaults = {
entry: require(opts.dir + '/package.json')["main"]
};
var configName = opts.name + (
opts.platform === 'web' ?
'' :
'.' + opts.platform
) + '.config.js';
if (opts.debug)
console.log('Looking for config file: %s'.blue, configName);
config = requireFirst([
opts.dir + '/config/' + configName,
opts.dir + '/' + configName
], function() {
return getDefaultConfig(opts);
});
// global config
var globalConfig;
globalConfig = requireFirst([
opts.dir + '/config/all.config.js',
opts.dir + '/all.config.js'
], function() {
return null;
});
if (globalConfig) {
config = Object.assign({}, globalConfig, config);
}
return pack(
Object.assign(defaults, opts, config)
);
}
function getDefaultConfig(opts) {
var defaultConfigPath = 'reapp-pack/config/'+opts.name+'.js';
if (opts.debug) {
console.log('No user config found...'.blue);
console.log('Looking for default config: ', defaultConfigPath);
}
return require(defaultConfigPath);
}