更新
This commit is contained in:
+8828
File diff suppressed because it is too large
Load Diff
+1425
File diff suppressed because it is too large
Load Diff
+95
@@ -0,0 +1,95 @@
|
||||
import process from 'node:process';
|
||||
import { y as yargsParser, t as trimNewlines, r as redent, n as normalizePackageData, c as camelcaseKeys } from './dependencies.js';
|
||||
import { buildOptions } from './options.js';
|
||||
import { buildParserOptions } from './parser.js';
|
||||
import { checkUnknownFlags, validate, checkMissingRequiredFlags } from './validate.js';
|
||||
|
||||
const buildResult = (options, parserOptions) => {
|
||||
const {pkg: package_} = options;
|
||||
const argv = yargsParser(options.argv, parserOptions);
|
||||
let help = '';
|
||||
|
||||
if (options.help) {
|
||||
help = trimNewlines((options.help || '').replace(/\t+\n*$/, ''));
|
||||
|
||||
if (help.includes('\n')) {
|
||||
help = redent(help, options.helpIndent);
|
||||
}
|
||||
|
||||
help = `\n${help}`;
|
||||
}
|
||||
|
||||
normalizePackageData(package_);
|
||||
|
||||
let {description} = options;
|
||||
if (!description && description !== false) {
|
||||
({description} = package_);
|
||||
}
|
||||
|
||||
description &&= help ? redent(`\n${description}\n`, options.helpIndent) : `\n${description}`;
|
||||
help = `${description || ''}${help}\n`;
|
||||
|
||||
const showHelp = code => {
|
||||
console.log(help);
|
||||
process.exit(typeof code === 'number' ? code : 2);
|
||||
};
|
||||
|
||||
const showVersion = () => {
|
||||
console.log(typeof options.version === 'string' ? options.version : package_.version);
|
||||
process.exit(0);
|
||||
};
|
||||
|
||||
if (argv._.length === 0 && options.argv.length === 1) {
|
||||
if (argv.version === true && options.autoVersion) {
|
||||
showVersion();
|
||||
} else if (argv.help === true && options.autoHelp) {
|
||||
showHelp(0);
|
||||
}
|
||||
}
|
||||
|
||||
const input = argv._;
|
||||
delete argv._;
|
||||
|
||||
if (!options.allowUnknownFlags) {
|
||||
checkUnknownFlags(input);
|
||||
}
|
||||
|
||||
const flags = camelcaseKeys(argv, {exclude: ['--', /^\w$/]});
|
||||
const unnormalizedFlags = {...flags};
|
||||
|
||||
validate(flags, options);
|
||||
|
||||
for (const flagValue of Object.values(options.flags)) {
|
||||
if (Array.isArray(flagValue.aliases)) {
|
||||
for (const alias of flagValue.aliases) {
|
||||
delete flags[alias];
|
||||
}
|
||||
}
|
||||
|
||||
delete flags[flagValue.shortFlag];
|
||||
}
|
||||
|
||||
checkMissingRequiredFlags(options.flags, flags, input);
|
||||
|
||||
return {
|
||||
input,
|
||||
flags,
|
||||
unnormalizedFlags,
|
||||
pkg: package_,
|
||||
help,
|
||||
showHelp,
|
||||
showVersion,
|
||||
};
|
||||
};
|
||||
|
||||
const meow = (helpText, options = {}) => {
|
||||
const parsedOptions = buildOptions(helpText, options);
|
||||
const parserOptions = buildParserOptions(parsedOptions);
|
||||
const result = buildResult(parsedOptions, parserOptions);
|
||||
|
||||
process.title = result.pkg.bin ? Object.keys(result.pkg.bin).at(0) : result.pkg.name;
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
export { meow as default };
|
||||
+1620
File diff suppressed because it is too large
Load Diff
+92
@@ -0,0 +1,92 @@
|
||||
import process from 'node:process';
|
||||
import { dirname } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { a as readPackageUpSync } from './dependencies.js';
|
||||
import { joinFlagKeys, decamelizeFlagKey } 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'));
|
||||
}
|
||||
};
|
||||
|
||||
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,
|
||||
autoHelp: true,
|
||||
autoVersion: true,
|
||||
booleanDefault: false,
|
||||
allowUnknownFlags: true,
|
||||
allowParentFlags: true,
|
||||
helpIndent: 2,
|
||||
...options,
|
||||
};
|
||||
|
||||
validateOptions(parsedOptions);
|
||||
|
||||
return parsedOptions;
|
||||
};
|
||||
|
||||
export { buildOptions };
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
import { d as decamelizeKeys, b as constructParserOptions } from './dependencies.js';
|
||||
|
||||
const buildParserFlags = ({flags, booleanDefault}) => {
|
||||
const parserFlags = {};
|
||||
|
||||
for (const [flagKey, flagValue] of Object.entries(flags)) {
|
||||
const flag = {...flagValue};
|
||||
|
||||
// `minimist-options` expects `flag.alias`
|
||||
if (flag.shortFlag) {
|
||||
flag.alias = flag.shortFlag;
|
||||
delete flag.shortFlag;
|
||||
}
|
||||
|
||||
if (
|
||||
booleanDefault !== undefined
|
||||
&& flag.type === 'boolean'
|
||||
&& !Object.hasOwn(flag, 'default')
|
||||
) {
|
||||
flag.default = flag.isMultiple ? [booleanDefault] : booleanDefault;
|
||||
}
|
||||
|
||||
if (flag.isMultiple) {
|
||||
flag.type = flag.type ? `${flag.type}-array` : 'array';
|
||||
flag.default = flag.default ?? [];
|
||||
delete flag.isMultiple;
|
||||
}
|
||||
|
||||
if (Array.isArray(flag.aliases)) {
|
||||
if (flag.alias) {
|
||||
flag.aliases.push(flag.alias);
|
||||
}
|
||||
|
||||
flag.alias = flag.aliases;
|
||||
delete flag.aliases;
|
||||
}
|
||||
|
||||
parserFlags[flagKey] = flag;
|
||||
}
|
||||
|
||||
return parserFlags;
|
||||
};
|
||||
|
||||
const buildParserOptions = options => {
|
||||
let parserOptions = buildParserFlags(options);
|
||||
parserOptions.arguments = options.input;
|
||||
|
||||
parserOptions = decamelizeKeys(parserOptions, {separator: '-', exclude: ['stopEarly', '--']});
|
||||
|
||||
if (options.inferType) {
|
||||
delete parserOptions.arguments;
|
||||
}
|
||||
|
||||
// Add --help and --version to known flags if autoHelp or autoVersion are set
|
||||
if (!options.allowUnknownFlags) {
|
||||
if (options.autoHelp && !parserOptions.help) {
|
||||
parserOptions.help = {type: 'boolean'};
|
||||
}
|
||||
|
||||
if (options.autoVersion && !parserOptions.version) {
|
||||
parserOptions.version = {type: 'boolean'};
|
||||
}
|
||||
}
|
||||
|
||||
parserOptions = constructParserOptions(parserOptions);
|
||||
|
||||
parserOptions.configuration = {
|
||||
...parserOptions.configuration,
|
||||
'greedy-arrays': false,
|
||||
};
|
||||
|
||||
if (parserOptions['--']) {
|
||||
parserOptions.configuration['populate--'] = true;
|
||||
}
|
||||
|
||||
if (!options.allowUnknownFlags) {
|
||||
// Collect unknown options in `argv._` to be checked later.
|
||||
parserOptions.configuration['unknown-options-as-args'] = true;
|
||||
}
|
||||
|
||||
return parserOptions;
|
||||
};
|
||||
|
||||
export { buildParserOptions };
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
import { e as decamelize } from './dependencies.js';
|
||||
|
||||
const decamelizeFlagKey = flagKey => `--${decamelize(flagKey, {separator: '-'})}`;
|
||||
|
||||
const joinFlagKeys = (flagKeys, prefix = '--') => `\`${prefix}${flagKeys.join(`\`, \`${prefix}`)}\``;
|
||||
|
||||
export { decamelizeFlagKey, joinFlagKeys };
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
import process from 'node:process';
|
||||
import { decamelizeFlagKey } from './utils.js';
|
||||
|
||||
const validateFlags = (flags, options) => {
|
||||
for (const [flagKey, flagValue] of Object.entries(options.flags)) {
|
||||
if (flagKey !== '--' && !flagValue.isMultiple && Array.isArray(flags[flagKey])) {
|
||||
throw new Error(`The flag --${flagKey} can only be set once.`);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const validateChoicesByFlag = (flagKey, flagValue, receivedInput) => {
|
||||
const {choices, isRequired} = flagValue;
|
||||
|
||||
if (!choices) {
|
||||
return;
|
||||
}
|
||||
|
||||
const valueMustBeOneOf = `Value must be one of: [\`${choices.join('`, `')}\`]`;
|
||||
|
||||
if (!receivedInput) {
|
||||
if (isRequired) {
|
||||
return `Flag \`${decamelizeFlagKey(flagKey)}\` has no value. ${valueMustBeOneOf}`;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (Array.isArray(receivedInput)) {
|
||||
const unknownValues = receivedInput.filter(index => !choices.includes(index));
|
||||
|
||||
if (unknownValues.length > 0) {
|
||||
const valuesText = unknownValues.length > 1 ? 'values' : 'value';
|
||||
|
||||
return `Unknown ${valuesText} for flag \`${decamelizeFlagKey(flagKey)}\`: \`${unknownValues.join('`, `')}\`. ${valueMustBeOneOf}`;
|
||||
}
|
||||
} else if (!choices.includes(receivedInput)) {
|
||||
return `Unknown value for flag \`${decamelizeFlagKey(flagKey)}\`: \`${receivedInput}\`. ${valueMustBeOneOf}`;
|
||||
}
|
||||
};
|
||||
|
||||
const validateChoices = (flags, receivedFlags) => {
|
||||
const errors = [];
|
||||
|
||||
for (const [flagKey, flagValue] of Object.entries(flags)) {
|
||||
const receivedInput = receivedFlags[flagKey];
|
||||
const errorMessage = validateChoicesByFlag(flagKey, flagValue, receivedInput);
|
||||
|
||||
if (errorMessage) {
|
||||
errors.push(errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
if (errors.length > 0) {
|
||||
throw new Error(`${errors.join('\n')}`);
|
||||
}
|
||||
};
|
||||
|
||||
const validate = (flags, options) => {
|
||||
validateFlags(flags, options);
|
||||
validateChoices(options.flags, flags);
|
||||
};
|
||||
|
||||
const reportUnknownFlags = unknownFlags => {
|
||||
console.error([
|
||||
`Unknown flag${unknownFlags.length > 1 ? 's' : ''}`,
|
||||
...unknownFlags,
|
||||
].join('\n'));
|
||||
};
|
||||
|
||||
const checkUnknownFlags = input => {
|
||||
const unknownFlags = input.filter(item => typeof item === 'string' && item.startsWith('-'));
|
||||
if (unknownFlags.length > 0) {
|
||||
reportUnknownFlags(unknownFlags);
|
||||
process.exit(2);
|
||||
}
|
||||
};
|
||||
|
||||
const isFlagMissing = (flagName, definedFlags, receivedFlags, input) => {
|
||||
const flag = definedFlags[flagName];
|
||||
let isFlagRequired = true;
|
||||
|
||||
if (typeof flag.isRequired === 'function') {
|
||||
isFlagRequired = flag.isRequired(receivedFlags, input);
|
||||
if (typeof isFlagRequired !== 'boolean') {
|
||||
throw new TypeError(`Return value for isRequired callback should be of type boolean, but ${typeof isFlagRequired} was returned.`);
|
||||
}
|
||||
}
|
||||
|
||||
if (receivedFlags[flagName] === undefined) {
|
||||
return isFlagRequired;
|
||||
}
|
||||
|
||||
return flag.isMultiple && receivedFlags[flagName].length === 0 && isFlagRequired;
|
||||
};
|
||||
|
||||
const reportMissingRequiredFlags = missingRequiredFlags => {
|
||||
console.error(`Missing required flag${missingRequiredFlags.length > 1 ? 's' : ''}`);
|
||||
for (const flag of missingRequiredFlags) {
|
||||
console.error(`\t${decamelizeFlagKey(flag.key)}${flag.shortFlag ? `, -${flag.shortFlag}` : ''}`);
|
||||
}
|
||||
};
|
||||
|
||||
const checkMissingRequiredFlags = (flags, receivedFlags, input) => {
|
||||
const missingRequiredFlags = [];
|
||||
if (flags === undefined) {
|
||||
return [];
|
||||
}
|
||||
|
||||
for (const flagName of Object.keys(flags)) {
|
||||
if (flags[flagName].isRequired && isFlagMissing(flagName, flags, receivedFlags, input)) {
|
||||
missingRequiredFlags.push({key: flagName, ...flags[flagName]});
|
||||
}
|
||||
}
|
||||
|
||||
if (missingRequiredFlags.length > 0) {
|
||||
reportMissingRequiredFlags(missingRequiredFlags);
|
||||
process.exit(2);
|
||||
}
|
||||
};
|
||||
|
||||
export { checkMissingRequiredFlags, checkUnknownFlags, validate };
|
||||
Reference in New Issue
Block a user