-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathparseUserConfig.js
90 lines (79 loc) · 2.42 KB
/
parseUserConfig.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
84
85
86
87
88
89
90
const excludeNameRegexp = [/^\/\//, /^(scripts|\$schema|better-scripts)$/];
/** Parse user config
* @param {import('./types').UserConfig} userConfig
* @returns {import('./types').ParsedUserConfig}
*/
export function parseUserConfig(userConfig) {
const entries = Object.entries(userConfig).map(([name, value]) => [
name,
parseUserConfigValue(
name,
value,
userConfig['pre' + name],
userConfig['post' + name]
)
]);
const filteredEntries = entries.filter(
([name]) => !excludeNameRegexp.some(reg => reg.test(name))
);
const parsedUserConfig = Object.fromEntries(filteredEntries);
// Remove prev, post keys if script exists
for (const key in parsedUserConfig) {
const [, name] = key.match(/^(?:pre|post)(.*)/) ?? [];
if (name) delete parsedUserConfig[key];
}
return parsedUserConfig;
}
/** Parse user config value into Script object
* @param {import('./types').Valueof<import('./types').UserConfig>} value
* @param {import('./types').Valueof<import('./types').UserConfig>} prevalue
* @param {import('./types').Valueof<import('./types').UserConfig>} postvalue
*/
function parseUserConfigValue(name, value, prevalue, postvalue) {
/** @type {import('./types').Script} */
const script = {
name,
prescript: prevalue && parseUserConfigValue('pre' + name, prevalue),
postscript: postvalue && parseUserConfigValue('post' + name, postvalue)
};
// string
if (typeof value === 'string') script.command = value;
// array
if (Array.isArray(value)) {
script.command = value[0];
script.desc = value[1];
}
// object
if (!Array.isArray(value) && typeof value === 'object') {
const {
alias,
command,
desc,
describe,
description,
scripts,
prescript,
postscript,
env
} = value;
script.alias = alias;
script.command = command;
script.desc = desc ?? describe ?? description;
script.scripts = scripts && parseUserConfig(scripts);
if (prescript)
script.prescript = {
name: 'pre' + script.name,
command: prescript?.command,
desc: prescript?.desc ?? prescript?.describe ?? prescript?.description
};
if (postscript)
script.postscript = {
name: 'post' + script.name,
command: postscript?.command,
desc:
postscript?.desc ?? postscript?.describe ?? postscript?.description
};
script.env = env;
}
return script;
}