/
options.js
91 lines (79 loc) 路 3.09 KB
/
options.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
91
import process from 'node:process';
import {dirname} from 'node:path';
import {fileURLToPath} from 'node:url';
import {readPackageUpSync} from 'read-package-up';
import {decamelizeFlagKey, joinFlagKeys} from './utils.js';
const validateOptions = options => {
const invalidOptionFilters = {
flags: {
keyContainsDashes: {
filter: ([flagKey]) => flagKey.includes('-') && flagKey !== '--',
message: flagKeys => `Flag keys may not contain '-'. Invalid flags: ${joinFlagKeys(flagKeys, '')}`,
},
aliasIsSet: {
filter: ([, flag]) => Object.hasOwn(flag, 'alias'),
message: flagKeys => `The option \`alias\` has been renamed to \`shortFlag\`. The following flags need to be updated: ${joinFlagKeys(flagKeys)}`,
},
choicesNotAnArray: {
filter: ([, flag]) => Object.hasOwn(flag, 'choices') && !Array.isArray(flag.choices),
message: flagKeys => `The option \`choices\` must be an array. Invalid flags: ${joinFlagKeys(flagKeys)}`,
},
choicesNotMatchFlagType: {
filter: ([, flag]) => flag.type && Array.isArray(flag.choices) && flag.choices.some(choice => typeof choice !== flag.type),
message(flagKeys) {
const flagKeysAndTypes = flagKeys.map(flagKey => `(\`${decamelizeFlagKey(flagKey)}\`, type: '${options.flags[flagKey].type}')`);
return `Each value of the option \`choices\` must be of the same type as its flag. Invalid flags: ${flagKeysAndTypes.join(', ')}`;
},
},
defaultNotInChoices: {
filter: ([, flag]) => flag.default && Array.isArray(flag.choices) && ![flag.default].flat().every(value => flag.choices.includes(value)),
message: flagKeys => `Each value of the option \`default\` must exist within the option \`choices\`. Invalid flags: ${joinFlagKeys(flagKeys)}`,
},
},
};
const errorMessages = [];
for (const [optionKey, filters] of Object.entries(invalidOptionFilters)) {
const optionEntries = Object.entries(options[optionKey]);
for (const {filter, message} of Object.values(filters)) {
const invalidOptions = optionEntries.filter(option => filter(option));
const invalidOptionKeys = invalidOptions.map(([key]) => key);
if (invalidOptions.length > 0) {
errorMessages.push(message(invalidOptionKeys));
}
}
}
if (errorMessages.length > 0) {
throw new Error(errorMessages.join('\n'));
}
};
export const buildOptions = (helpText, options) => {
if (typeof helpText !== 'string') {
options = helpText;
helpText = '';
}
if (!options.importMeta?.url) {
throw new TypeError('The `importMeta` option is required. Its value must be `import.meta`.');
}
const foundPackage = readPackageUpSync({
cwd: dirname(fileURLToPath(options.importMeta.url)),
normalize: false,
});
const parsedOptions = {
pkg: foundPackage ? foundPackage.packageJson : {},
argv: process.argv.slice(2),
flags: {},
inferType: false,
input: 'string',
help: helpText,
version: foundPackage?.packageJson.version || 'No version found',
autoHelp: true,
autoVersion: true,
booleanDefault: false,
allowUnknownFlags: true,
allowParentFlags: true,
helpIndent: 2,
...options,
};
validateOptions(parsedOptions);
return parsedOptions;
};