-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil.js
32 lines (31 loc) · 821 Bytes
/
util.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
module.exports.safeRequire = async function(path) {
try {
return require(path);
} catch(e) {
if (!e.message || !e.message.includes('Cannot find module')) {
throw e;
}
const mdl = await import(path);
return mdl.default || mdl;
}
};
module.exports.createConfig = function(config, argv, prefix, keys) {
config = { ...config };
if (keys) {
keys.forEach(key => {
if (config[key] == undefined) {
config[key] = argv[`${prefix}_${key}`] || argv[key];
}
});
} else {
Object.keys(argv).forEach(key => {
if (key.startsWith(prefix + '_')) {
let value = argv[key];
if (value === 'true') value = true;
if (value === 'false') value = false;
config[key.replace(prefix + '_', '')] = value;
}
});
}
return config;
};