d975cc371d
modified: node_modules/.package-lock.json deleted: node_modules/array-union/index.d.ts deleted: node_modules/array-union/index.js deleted: node_modules/array-union/license deleted: node_modules/array-union/package.json deleted: node_modules/array-union/readme.md modified: node_modules/cliui/CHANGELOG.md modified: node_modules/cliui/package.json modified: node_modules/fast-glob/out/index.d.ts modified: node_modules/fast-glob/out/index.js modified: node_modules/fast-glob/out/managers/patterns.d.ts modified: node_modules/fast-glob/out/managers/patterns.js modified: node_modules/fast-glob/out/managers/tasks.d.ts modified: node_modules/fast-glob/out/managers/tasks.js modified: node_modules/fast-glob/out/providers/async.d.ts modified: node_modules/fast-glob/out/providers/async.js modified: node_modules/fast-glob/out/providers/filters/deep.d.ts modified: node_modules/fast-glob/out/providers/filters/deep.js modified: node_modules/fast-glob/out/providers/filters/entry.d.ts modified: node_modules/fast-glob/out/providers/filters/entry.js modified: node_modules/fast-glob/out/providers/filters/error.d.ts modified: node_modules/fast-glob/out/providers/filters/error.js modified: node_modules/fast-glob/out/providers/matchers/matcher.d.ts modified: node_modules/fast-glob/out/providers/matchers/matcher.js modified: node_modules/fast-glob/out/providers/matchers/partial.d.ts modified: node_modules/fast-glob/out/providers/matchers/partial.js modified: node_modules/fast-glob/out/providers/provider.d.ts modified: node_modules/fast-glob/out/providers/provider.js modified: node_modules/fast-glob/out/providers/stream.d.ts modified: node_modules/fast-glob/out/providers/stream.js modified: node_modules/fast-glob/out/providers/sync.d.ts modified: node_modules/fast-glob/out/providers/sync.js modified: node_modules/fast-glob/out/providers/transformers/entry.d.ts modified: node_modules/fast-glob/out/providers/transformers/entry.js modified: node_modules/fast-glob/out/readers/reader.d.ts modified: node_modules/fast-glob/out/readers/reader.js modified: node_modules/fast-glob/out/readers/stream.d.ts modified: node_modules/fast-glob/out/readers/stream.js modified: node_modules/fast-glob/out/readers/sync.d.ts modified: node_modules/fast-glob/out/readers/sync.js modified: node_modules/fast-glob/out/settings.d.ts modified: node_modules/fast-glob/out/settings.js modified: node_modules/fast-glob/out/types/index.d.ts modified: node_modules/fast-glob/out/types/index.js modified: node_modules/fast-glob/out/utils/array.d.ts modified: node_modules/fast-glob/out/utils/array.js modified: node_modules/fast-glob/out/utils/errno.d.ts modified: node_modules/fast-glob/out/utils/errno.js modified: node_modules/fast-glob/out/utils/fs.d.ts modified: node_modules/fast-glob/out/utils/fs.js modified: node_modules/fast-glob/out/utils/index.d.ts modified: node_modules/fast-glob/out/utils/index.js modified: node_modules/fast-glob/out/utils/path.d.ts modified: node_modules/fast-glob/out/utils/path.js modified: node_modules/fast-glob/out/utils/pattern.d.ts modified: node_modules/fast-glob/out/utils/pattern.js modified: node_modules/fast-glob/out/utils/stream.d.ts modified: node_modules/fast-glob/out/utils/stream.js modified: node_modules/fast-glob/out/utils/string.d.ts modified: node_modules/fast-glob/out/utils/string.js modified: node_modules/fast-glob/package.json modified: node_modules/fastq/README.md modified: node_modules/fastq/index.d.ts modified: node_modules/fastq/package.json modified: node_modules/fastq/queue.js modified: node_modules/fastq/test/promise.js deleted: node_modules/fs-extra/CHANGELOG.md modified: node_modules/fs-extra/README.md deleted: node_modules/fs-extra/lib/copy-sync/copy-sync.js deleted: node_modules/fs-extra/lib/copy-sync/index.js modified: node_modules/fs-extra/lib/copy/copy.js modified: node_modules/fs-extra/lib/copy/index.js modified: node_modules/fs-extra/lib/ensure/index.js modified: node_modules/fs-extra/lib/fs/index.js modified: node_modules/fs-extra/lib/index.js modified: node_modules/fs-extra/lib/json/output-json-sync.js modified: node_modules/fs-extra/lib/json/output-json.js deleted: node_modules/fs-extra/lib/move-sync/index.js deleted: node_modules/fs-extra/lib/move-sync/move-sync.js modified: node_modules/fs-extra/lib/move/index.js modified: node_modules/fs-extra/lib/move/move.js deleted: node_modules/fs-extra/lib/output/index.js modified: node_modules/fs-extra/lib/remove/index.js deleted: node_modules/fs-extra/lib/remove/rimraf.js modified: node_modules/fs-extra/package.json deleted: node_modules/globby/gitignore.js modified: node_modules/globby/index.d.ts modified: node_modules/globby/index.js modified: node_modules/globby/package.json modified: node_modules/globby/readme.md deleted: node_modules/globby/stream-utils.js deleted: node_modules/globby/to-path.js modified: node_modules/graceful-fs/LICENSE modified: node_modules/graceful-fs/graceful-fs.js modified: node_modules/graceful-fs/package.json modified: node_modules/graceful-fs/polyfills.js modified: node_modules/ignore/README.md modified: node_modules/ignore/index.js modified: node_modules/ignore/legacy.js modified: node_modules/ignore/package.json modified: node_modules/lilconfig/dist/index.js modified: node_modules/lilconfig/package.json modified: node_modules/lilconfig/readme.md deleted: node_modules/micromatch/CHANGELOG.md modified: node_modules/micromatch/README.md modified: node_modules/micromatch/index.js modified: node_modules/micromatch/package.json modified: node_modules/nanoid/bin/nanoid.cjs modified: node_modules/nanoid/index.browser.cjs modified: node_modules/nanoid/index.browser.js deleted: node_modules/nanoid/index.dev.js deleted: node_modules/nanoid/index.prod.js modified: node_modules/nanoid/nanoid.js modified: node_modules/nanoid/package.json modified: node_modules/postcss-cli/index.js modified: node_modules/postcss-cli/package.json modified: node_modules/postcss-load-config/README.md modified: node_modules/postcss-load-config/package.json modified: node_modules/postcss-load-config/src/index.d.ts modified: node_modules/postcss-load-config/src/index.js modified: node_modules/postcss-load-config/src/req.js modified: node_modules/postcss/README.md modified: node_modules/postcss/lib/at-rule.d.ts modified: node_modules/postcss/lib/comment.d.ts modified: node_modules/postcss/lib/container.d.ts modified: node_modules/postcss/lib/container.js modified: node_modules/postcss/lib/css-syntax-error.d.ts modified: node_modules/postcss/lib/declaration.d.ts modified: node_modules/postcss/lib/document.d.ts modified: node_modules/postcss/lib/fromJSON.d.ts modified: node_modules/postcss/lib/input.d.ts modified: node_modules/postcss/lib/input.js modified: node_modules/postcss/lib/lazy-result.d.ts modified: node_modules/postcss/lib/list.d.ts modified: node_modules/postcss/lib/list.js modified: node_modules/postcss/lib/map-generator.js modified: node_modules/postcss/lib/no-work-result.d.ts modified: node_modules/postcss/lib/node.d.ts modified: node_modules/postcss/lib/parse.d.ts modified: node_modules/postcss/lib/parser.js modified: node_modules/postcss/lib/postcss.d.ts modified: node_modules/postcss/lib/postcss.js modified: node_modules/postcss/lib/previous-map.d.ts modified: node_modules/postcss/lib/processor.d.ts modified: node_modules/postcss/lib/processor.js modified: node_modules/postcss/lib/result.d.ts modified: node_modules/postcss/lib/root.d.ts modified: node_modules/postcss/lib/root.js modified: node_modules/postcss/lib/rule.d.ts modified: node_modules/postcss/lib/stringifier.d.ts modified: node_modules/postcss/lib/stringify.d.ts modified: node_modules/postcss/lib/warning.d.ts modified: node_modules/postcss/package.json modified: node_modules/slash/index.d.ts modified: node_modules/slash/index.js modified: node_modules/slash/package.json modified: node_modules/slash/readme.md modified: node_modules/yaml/LICENSE modified: node_modules/yaml/README.md deleted: node_modules/yaml/browser/dist/PlainValue-b8036b75.js deleted: node_modules/yaml/browser/dist/Schema-e94716c8.js modified: node_modules/yaml/browser/dist/index.js deleted: node_modules/yaml/browser/dist/legacy-exports.js deleted: node_modules/yaml/browser/dist/package.json deleted: node_modules/yaml/browser/dist/parse-cst.js deleted: node_modules/yaml/browser/dist/resolveSeq-492ab440.js deleted: node_modules/yaml/browser/dist/types.js modified: node_modules/yaml/browser/dist/util.js deleted: node_modules/yaml/browser/dist/warnings-df54cb69.js modified: node_modules/yaml/browser/index.js deleted: node_modules/yaml/browser/map.js deleted: node_modules/yaml/browser/pair.js deleted: node_modules/yaml/browser/parse-cst.js deleted: node_modules/yaml/browser/scalar.js deleted: node_modules/yaml/browser/schema.js deleted: node_modules/yaml/browser/seq.js deleted: node_modules/yaml/browser/types.js deleted: node_modules/yaml/browser/types/binary.js deleted: node_modules/yaml/browser/types/omap.js deleted: node_modules/yaml/browser/types/pairs.js deleted: node_modules/yaml/browser/types/set.js deleted: node_modules/yaml/browser/types/timestamp.js deleted: node_modules/yaml/browser/util.js deleted: node_modules/yaml/dist/Document-9b4560a1.js deleted: node_modules/yaml/dist/PlainValue-ec8e588e.js deleted: node_modules/yaml/dist/Schema-88e323a7.js modified: node_modules/yaml/dist/index.js deleted: node_modules/yaml/dist/legacy-exports.js deleted: node_modules/yaml/dist/parse-cst.js deleted: node_modules/yaml/dist/resolveSeq-d03cb037.js modified: node_modules/yaml/dist/test-events.js deleted: node_modules/yaml/dist/types.js modified: node_modules/yaml/dist/util.js deleted: node_modules/yaml/dist/warnings-1000a372.js deleted: node_modules/yaml/index.d.ts deleted: node_modules/yaml/index.js deleted: node_modules/yaml/map.js modified: node_modules/yaml/package.json deleted: node_modules/yaml/pair.js deleted: node_modules/yaml/parse-cst.d.ts deleted: node_modules/yaml/parse-cst.js deleted: node_modules/yaml/scalar.js deleted: node_modules/yaml/schema.js deleted: node_modules/yaml/seq.js deleted: node_modules/yaml/types.d.ts deleted: node_modules/yaml/types.js deleted: node_modules/yaml/types.mjs deleted: node_modules/yaml/types/binary.js deleted: node_modules/yaml/types/omap.js deleted: node_modules/yaml/types/pairs.js deleted: node_modules/yaml/types/set.js deleted: node_modules/yaml/types/timestamp.js modified: node_modules/yaml/util.d.ts modified: node_modules/yaml/util.js deleted: node_modules/yaml/util.mjs modified: node_modules/yargs-parser/CHANGELOG.md modified: node_modules/yargs-parser/build/index.cjs modified: node_modules/yargs-parser/build/lib/index.js modified: node_modules/yargs-parser/build/lib/yargs-parser.js modified: node_modules/yargs-parser/package.json modified: node_modules/yargs/build/index.cjs modified: node_modules/yargs/build/lib/command.js modified: node_modules/yargs/build/lib/completion-templates.js modified: node_modules/yargs/build/lib/completion.js modified: node_modules/yargs/build/lib/usage.js modified: node_modules/yargs/build/lib/validation.js modified: node_modules/yargs/build/lib/yargs-factory.js modified: node_modules/yargs/build/lib/yerror.js modified: node_modules/yargs/lib/platform-shims/esm.mjs modified: node_modules/yargs/locales/en.json modified: node_modules/yargs/locales/fi.json modified: node_modules/yargs/locales/ru.json modified: node_modules/yargs/package.json modified: package-lock.json modified: package.json modified: public/about/index.html modified: public/access/index.html modified: public/blog/index.html modified: public/categories/index.html modified: public/index.html modified: public/manifesto/index.html modified: public/posts/apple-breaks-privacy/index.html modified: public/posts/index.html modified: public/posts/main_street_against_big_tech/index.html modified: public/posts/what_is_done_with_personal_data/index.html modified: public/projects/index.html modified: public/tags/index.html .hugo_build.lock go.mod go.sum node_modules/.bin/autoprefixer node_modules/.bin/browserslist node_modules/.bin/update-browserslist-db node_modules/autoprefixer/ node_modules/browserslist/ node_modules/caniuse-lite/ node_modules/cliui/build/index.d.cts node_modules/electron-to-chromium/ node_modules/fast-glob/out/readers/async.d.ts node_modules/fast-glob/out/readers/async.js node_modules/fraction.js/ node_modules/fs-extra/lib/copy/copy-sync.js node_modules/fs-extra/lib/esm.mjs node_modules/fs-extra/lib/move/move-sync.js node_modules/fs-extra/lib/output-file/ node_modules/globby/ignore.js node_modules/globby/node_modules/ node_modules/globby/utilities.js node_modules/lilconfig/LICENSE node_modules/node-releases/ node_modules/normalize-range/ node_modules/postcss-value-parser/ node_modules/postcss/lib/postcss.d.mts node_modules/update-browserslist-db/ node_modules/yaml/browser/dist/compose/ node_modules/yaml/browser/dist/doc/ node_modules/yaml/browser/dist/errors.js node_modules/yaml/browser/dist/log.js node_modules/yaml/browser/dist/node_modules/ node_modules/yaml/browser/dist/nodes/ node_modules/yaml/browser/dist/parse/ node_modules/yaml/browser/dist/public-api.js node_modules/yaml/browser/dist/schema/ node_modules/yaml/browser/dist/stringify/ node_modules/yaml/browser/dist/visit.js node_modules/yaml/browser/package.json node_modules/yaml/dist/compose/ node_modules/yaml/dist/doc/ node_modules/yaml/dist/errors.d.ts node_modules/yaml/dist/errors.js node_modules/yaml/dist/index.d.ts node_modules/yaml/dist/log.d.ts node_modules/yaml/dist/log.js node_modules/yaml/dist/nodes/ node_modules/yaml/dist/options.d.ts node_modules/yaml/dist/parse/ node_modules/yaml/dist/public-api.d.ts node_modules/yaml/dist/public-api.js node_modules/yaml/dist/schema/ node_modules/yaml/dist/stringify/ node_modules/yaml/dist/test-events.d.ts node_modules/yaml/dist/util.d.ts node_modules/yaml/dist/visit.d.ts node_modules/yaml/dist/visit.js node_modules/yargs/browser.d.ts node_modules/yargs/locales/cs.json node_modules/yargs/yargs.mjs public/css/main.min.c5514d3530979d291f3497facc20da1cec870028dbc2a3630b64bab8721bbe49.css resources/_gen/assets/sass/sass/style.sass_4d6f00452a35742ddbb489cb3703195e.content resources/_gen/assets/sass/sass/style.sass_4d6f00452a35742ddbb489cb3703195e.json resources/_gen/assets/sass/sass/style.sass_c87a2fc9dae9a546a3e3f1548c147175.content resources/_gen/assets/sass/sass/style.sass_c87a2fc9dae9a546a3e3f1548c147175.json
450 lines
19 KiB
JavaScript
450 lines
19 KiB
JavaScript
import { assertNotStrictEqual, } from './typings/common-types.js';
|
|
import { isPromise } from './utils/is-promise.js';
|
|
import { applyMiddleware, commandMiddlewareFactory, } from './middleware.js';
|
|
import { parseCommand } from './parse-command.js';
|
|
import { isYargsInstance, } from './yargs-factory.js';
|
|
import { maybeAsyncResult } from './utils/maybe-async-result.js';
|
|
import whichModule from './utils/which-module.js';
|
|
const DEFAULT_MARKER = /(^\*)|(^\$0)/;
|
|
export class CommandInstance {
|
|
constructor(usage, validation, globalMiddleware, shim) {
|
|
this.requireCache = new Set();
|
|
this.handlers = {};
|
|
this.aliasMap = {};
|
|
this.frozens = [];
|
|
this.shim = shim;
|
|
this.usage = usage;
|
|
this.globalMiddleware = globalMiddleware;
|
|
this.validation = validation;
|
|
}
|
|
addDirectory(dir, req, callerFile, opts) {
|
|
opts = opts || {};
|
|
if (typeof opts.recurse !== 'boolean')
|
|
opts.recurse = false;
|
|
if (!Array.isArray(opts.extensions))
|
|
opts.extensions = ['js'];
|
|
const parentVisit = typeof opts.visit === 'function' ? opts.visit : (o) => o;
|
|
opts.visit = (obj, joined, filename) => {
|
|
const visited = parentVisit(obj, joined, filename);
|
|
if (visited) {
|
|
if (this.requireCache.has(joined))
|
|
return visited;
|
|
else
|
|
this.requireCache.add(joined);
|
|
this.addHandler(visited);
|
|
}
|
|
return visited;
|
|
};
|
|
this.shim.requireDirectory({ require: req, filename: callerFile }, dir, opts);
|
|
}
|
|
addHandler(cmd, description, builder, handler, commandMiddleware, deprecated) {
|
|
let aliases = [];
|
|
const middlewares = commandMiddlewareFactory(commandMiddleware);
|
|
handler = handler || (() => { });
|
|
if (Array.isArray(cmd)) {
|
|
if (isCommandAndAliases(cmd)) {
|
|
[cmd, ...aliases] = cmd;
|
|
}
|
|
else {
|
|
for (const command of cmd) {
|
|
this.addHandler(command);
|
|
}
|
|
}
|
|
}
|
|
else if (isCommandHandlerDefinition(cmd)) {
|
|
let command = Array.isArray(cmd.command) || typeof cmd.command === 'string'
|
|
? cmd.command
|
|
: this.moduleName(cmd);
|
|
if (cmd.aliases)
|
|
command = [].concat(command).concat(cmd.aliases);
|
|
this.addHandler(command, this.extractDesc(cmd), cmd.builder, cmd.handler, cmd.middlewares, cmd.deprecated);
|
|
return;
|
|
}
|
|
else if (isCommandBuilderDefinition(builder)) {
|
|
this.addHandler([cmd].concat(aliases), description, builder.builder, builder.handler, builder.middlewares, builder.deprecated);
|
|
return;
|
|
}
|
|
if (typeof cmd === 'string') {
|
|
const parsedCommand = parseCommand(cmd);
|
|
aliases = aliases.map(alias => parseCommand(alias).cmd);
|
|
let isDefault = false;
|
|
const parsedAliases = [parsedCommand.cmd].concat(aliases).filter(c => {
|
|
if (DEFAULT_MARKER.test(c)) {
|
|
isDefault = true;
|
|
return false;
|
|
}
|
|
return true;
|
|
});
|
|
if (parsedAliases.length === 0 && isDefault)
|
|
parsedAliases.push('$0');
|
|
if (isDefault) {
|
|
parsedCommand.cmd = parsedAliases[0];
|
|
aliases = parsedAliases.slice(1);
|
|
cmd = cmd.replace(DEFAULT_MARKER, parsedCommand.cmd);
|
|
}
|
|
aliases.forEach(alias => {
|
|
this.aliasMap[alias] = parsedCommand.cmd;
|
|
});
|
|
if (description !== false) {
|
|
this.usage.command(cmd, description, isDefault, aliases, deprecated);
|
|
}
|
|
this.handlers[parsedCommand.cmd] = {
|
|
original: cmd,
|
|
description,
|
|
handler,
|
|
builder: builder || {},
|
|
middlewares,
|
|
deprecated,
|
|
demanded: parsedCommand.demanded,
|
|
optional: parsedCommand.optional,
|
|
};
|
|
if (isDefault)
|
|
this.defaultCommand = this.handlers[parsedCommand.cmd];
|
|
}
|
|
}
|
|
getCommandHandlers() {
|
|
return this.handlers;
|
|
}
|
|
getCommands() {
|
|
return Object.keys(this.handlers).concat(Object.keys(this.aliasMap));
|
|
}
|
|
hasDefaultCommand() {
|
|
return !!this.defaultCommand;
|
|
}
|
|
runCommand(command, yargs, parsed, commandIndex, helpOnly, helpOrVersionSet) {
|
|
const commandHandler = this.handlers[command] ||
|
|
this.handlers[this.aliasMap[command]] ||
|
|
this.defaultCommand;
|
|
const currentContext = yargs.getInternalMethods().getContext();
|
|
const parentCommands = currentContext.commands.slice();
|
|
const isDefaultCommand = !command;
|
|
if (command) {
|
|
currentContext.commands.push(command);
|
|
currentContext.fullCommands.push(commandHandler.original);
|
|
}
|
|
const builderResult = this.applyBuilderUpdateUsageAndParse(isDefaultCommand, commandHandler, yargs, parsed.aliases, parentCommands, commandIndex, helpOnly, helpOrVersionSet);
|
|
return isPromise(builderResult)
|
|
? builderResult.then(result => this.applyMiddlewareAndGetResult(isDefaultCommand, commandHandler, result.innerArgv, currentContext, helpOnly, result.aliases, yargs))
|
|
: this.applyMiddlewareAndGetResult(isDefaultCommand, commandHandler, builderResult.innerArgv, currentContext, helpOnly, builderResult.aliases, yargs);
|
|
}
|
|
applyBuilderUpdateUsageAndParse(isDefaultCommand, commandHandler, yargs, aliases, parentCommands, commandIndex, helpOnly, helpOrVersionSet) {
|
|
const builder = commandHandler.builder;
|
|
let innerYargs = yargs;
|
|
if (isCommandBuilderCallback(builder)) {
|
|
yargs.getInternalMethods().getUsageInstance().freeze();
|
|
const builderOutput = builder(yargs.getInternalMethods().reset(aliases), helpOrVersionSet);
|
|
if (isPromise(builderOutput)) {
|
|
return builderOutput.then(output => {
|
|
innerYargs = isYargsInstance(output) ? output : yargs;
|
|
return this.parseAndUpdateUsage(isDefaultCommand, commandHandler, innerYargs, parentCommands, commandIndex, helpOnly);
|
|
});
|
|
}
|
|
}
|
|
else if (isCommandBuilderOptionDefinitions(builder)) {
|
|
yargs.getInternalMethods().getUsageInstance().freeze();
|
|
innerYargs = yargs.getInternalMethods().reset(aliases);
|
|
Object.keys(commandHandler.builder).forEach(key => {
|
|
innerYargs.option(key, builder[key]);
|
|
});
|
|
}
|
|
return this.parseAndUpdateUsage(isDefaultCommand, commandHandler, innerYargs, parentCommands, commandIndex, helpOnly);
|
|
}
|
|
parseAndUpdateUsage(isDefaultCommand, commandHandler, innerYargs, parentCommands, commandIndex, helpOnly) {
|
|
if (isDefaultCommand)
|
|
innerYargs.getInternalMethods().getUsageInstance().unfreeze(true);
|
|
if (this.shouldUpdateUsage(innerYargs)) {
|
|
innerYargs
|
|
.getInternalMethods()
|
|
.getUsageInstance()
|
|
.usage(this.usageFromParentCommandsCommandHandler(parentCommands, commandHandler), commandHandler.description);
|
|
}
|
|
const innerArgv = innerYargs
|
|
.getInternalMethods()
|
|
.runYargsParserAndExecuteCommands(null, undefined, true, commandIndex, helpOnly);
|
|
return isPromise(innerArgv)
|
|
? innerArgv.then(argv => ({
|
|
aliases: innerYargs.parsed.aliases,
|
|
innerArgv: argv,
|
|
}))
|
|
: {
|
|
aliases: innerYargs.parsed.aliases,
|
|
innerArgv: innerArgv,
|
|
};
|
|
}
|
|
shouldUpdateUsage(yargs) {
|
|
return (!yargs.getInternalMethods().getUsageInstance().getUsageDisabled() &&
|
|
yargs.getInternalMethods().getUsageInstance().getUsage().length === 0);
|
|
}
|
|
usageFromParentCommandsCommandHandler(parentCommands, commandHandler) {
|
|
const c = DEFAULT_MARKER.test(commandHandler.original)
|
|
? commandHandler.original.replace(DEFAULT_MARKER, '').trim()
|
|
: commandHandler.original;
|
|
const pc = parentCommands.filter(c => {
|
|
return !DEFAULT_MARKER.test(c);
|
|
});
|
|
pc.push(c);
|
|
return `$0 ${pc.join(' ')}`;
|
|
}
|
|
handleValidationAndGetResult(isDefaultCommand, commandHandler, innerArgv, currentContext, aliases, yargs, middlewares, positionalMap) {
|
|
if (!yargs.getInternalMethods().getHasOutput()) {
|
|
const validation = yargs
|
|
.getInternalMethods()
|
|
.runValidation(aliases, positionalMap, yargs.parsed.error, isDefaultCommand);
|
|
innerArgv = maybeAsyncResult(innerArgv, result => {
|
|
validation(result);
|
|
return result;
|
|
});
|
|
}
|
|
if (commandHandler.handler && !yargs.getInternalMethods().getHasOutput()) {
|
|
yargs.getInternalMethods().setHasOutput();
|
|
const populateDoubleDash = !!yargs.getOptions().configuration['populate--'];
|
|
yargs
|
|
.getInternalMethods()
|
|
.postProcess(innerArgv, populateDoubleDash, false, false);
|
|
innerArgv = applyMiddleware(innerArgv, yargs, middlewares, false);
|
|
innerArgv = maybeAsyncResult(innerArgv, result => {
|
|
const handlerResult = commandHandler.handler(result);
|
|
return isPromise(handlerResult)
|
|
? handlerResult.then(() => result)
|
|
: result;
|
|
});
|
|
if (!isDefaultCommand) {
|
|
yargs.getInternalMethods().getUsageInstance().cacheHelpMessage();
|
|
}
|
|
if (isPromise(innerArgv) &&
|
|
!yargs.getInternalMethods().hasParseCallback()) {
|
|
innerArgv.catch(error => {
|
|
try {
|
|
yargs.getInternalMethods().getUsageInstance().fail(null, error);
|
|
}
|
|
catch (_err) {
|
|
}
|
|
});
|
|
}
|
|
}
|
|
if (!isDefaultCommand) {
|
|
currentContext.commands.pop();
|
|
currentContext.fullCommands.pop();
|
|
}
|
|
return innerArgv;
|
|
}
|
|
applyMiddlewareAndGetResult(isDefaultCommand, commandHandler, innerArgv, currentContext, helpOnly, aliases, yargs) {
|
|
let positionalMap = {};
|
|
if (helpOnly)
|
|
return innerArgv;
|
|
if (!yargs.getInternalMethods().getHasOutput()) {
|
|
positionalMap = this.populatePositionals(commandHandler, innerArgv, currentContext, yargs);
|
|
}
|
|
const middlewares = this.globalMiddleware
|
|
.getMiddleware()
|
|
.slice(0)
|
|
.concat(commandHandler.middlewares);
|
|
const maybePromiseArgv = applyMiddleware(innerArgv, yargs, middlewares, true);
|
|
return isPromise(maybePromiseArgv)
|
|
? maybePromiseArgv.then(resolvedInnerArgv => this.handleValidationAndGetResult(isDefaultCommand, commandHandler, resolvedInnerArgv, currentContext, aliases, yargs, middlewares, positionalMap))
|
|
: this.handleValidationAndGetResult(isDefaultCommand, commandHandler, maybePromiseArgv, currentContext, aliases, yargs, middlewares, positionalMap);
|
|
}
|
|
populatePositionals(commandHandler, argv, context, yargs) {
|
|
argv._ = argv._.slice(context.commands.length);
|
|
const demanded = commandHandler.demanded.slice(0);
|
|
const optional = commandHandler.optional.slice(0);
|
|
const positionalMap = {};
|
|
this.validation.positionalCount(demanded.length, argv._.length);
|
|
while (demanded.length) {
|
|
const demand = demanded.shift();
|
|
this.populatePositional(demand, argv, positionalMap);
|
|
}
|
|
while (optional.length) {
|
|
const maybe = optional.shift();
|
|
this.populatePositional(maybe, argv, positionalMap);
|
|
}
|
|
argv._ = context.commands.concat(argv._.map(a => '' + a));
|
|
this.postProcessPositionals(argv, positionalMap, this.cmdToParseOptions(commandHandler.original), yargs);
|
|
return positionalMap;
|
|
}
|
|
populatePositional(positional, argv, positionalMap) {
|
|
const cmd = positional.cmd[0];
|
|
if (positional.variadic) {
|
|
positionalMap[cmd] = argv._.splice(0).map(String);
|
|
}
|
|
else {
|
|
if (argv._.length)
|
|
positionalMap[cmd] = [String(argv._.shift())];
|
|
}
|
|
}
|
|
cmdToParseOptions(cmdString) {
|
|
const parseOptions = {
|
|
array: [],
|
|
default: {},
|
|
alias: {},
|
|
demand: {},
|
|
};
|
|
const parsed = parseCommand(cmdString);
|
|
parsed.demanded.forEach(d => {
|
|
const [cmd, ...aliases] = d.cmd;
|
|
if (d.variadic) {
|
|
parseOptions.array.push(cmd);
|
|
parseOptions.default[cmd] = [];
|
|
}
|
|
parseOptions.alias[cmd] = aliases;
|
|
parseOptions.demand[cmd] = true;
|
|
});
|
|
parsed.optional.forEach(o => {
|
|
const [cmd, ...aliases] = o.cmd;
|
|
if (o.variadic) {
|
|
parseOptions.array.push(cmd);
|
|
parseOptions.default[cmd] = [];
|
|
}
|
|
parseOptions.alias[cmd] = aliases;
|
|
});
|
|
return parseOptions;
|
|
}
|
|
postProcessPositionals(argv, positionalMap, parseOptions, yargs) {
|
|
const options = Object.assign({}, yargs.getOptions());
|
|
options.default = Object.assign(parseOptions.default, options.default);
|
|
for (const key of Object.keys(parseOptions.alias)) {
|
|
options.alias[key] = (options.alias[key] || []).concat(parseOptions.alias[key]);
|
|
}
|
|
options.array = options.array.concat(parseOptions.array);
|
|
options.config = {};
|
|
const unparsed = [];
|
|
Object.keys(positionalMap).forEach(key => {
|
|
positionalMap[key].map(value => {
|
|
if (options.configuration['unknown-options-as-args'])
|
|
options.key[key] = true;
|
|
unparsed.push(`--${key}`);
|
|
unparsed.push(value);
|
|
});
|
|
});
|
|
if (!unparsed.length)
|
|
return;
|
|
const config = Object.assign({}, options.configuration, {
|
|
'populate--': false,
|
|
});
|
|
const parsed = this.shim.Parser.detailed(unparsed, Object.assign({}, options, {
|
|
configuration: config,
|
|
}));
|
|
if (parsed.error) {
|
|
yargs
|
|
.getInternalMethods()
|
|
.getUsageInstance()
|
|
.fail(parsed.error.message, parsed.error);
|
|
}
|
|
else {
|
|
const positionalKeys = Object.keys(positionalMap);
|
|
Object.keys(positionalMap).forEach(key => {
|
|
positionalKeys.push(...parsed.aliases[key]);
|
|
});
|
|
Object.keys(parsed.argv).forEach(key => {
|
|
if (positionalKeys.includes(key)) {
|
|
if (!positionalMap[key])
|
|
positionalMap[key] = parsed.argv[key];
|
|
if (!this.isInConfigs(yargs, key) &&
|
|
!this.isDefaulted(yargs, key) &&
|
|
Object.prototype.hasOwnProperty.call(argv, key) &&
|
|
Object.prototype.hasOwnProperty.call(parsed.argv, key) &&
|
|
(Array.isArray(argv[key]) || Array.isArray(parsed.argv[key]))) {
|
|
argv[key] = [].concat(argv[key], parsed.argv[key]);
|
|
}
|
|
else {
|
|
argv[key] = parsed.argv[key];
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|
|
isDefaulted(yargs, key) {
|
|
const { default: defaults } = yargs.getOptions();
|
|
return (Object.prototype.hasOwnProperty.call(defaults, key) ||
|
|
Object.prototype.hasOwnProperty.call(defaults, this.shim.Parser.camelCase(key)));
|
|
}
|
|
isInConfigs(yargs, key) {
|
|
const { configObjects } = yargs.getOptions();
|
|
return (configObjects.some(c => Object.prototype.hasOwnProperty.call(c, key)) ||
|
|
configObjects.some(c => Object.prototype.hasOwnProperty.call(c, this.shim.Parser.camelCase(key))));
|
|
}
|
|
runDefaultBuilderOn(yargs) {
|
|
if (!this.defaultCommand)
|
|
return;
|
|
if (this.shouldUpdateUsage(yargs)) {
|
|
const commandString = DEFAULT_MARKER.test(this.defaultCommand.original)
|
|
? this.defaultCommand.original
|
|
: this.defaultCommand.original.replace(/^[^[\]<>]*/, '$0 ');
|
|
yargs
|
|
.getInternalMethods()
|
|
.getUsageInstance()
|
|
.usage(commandString, this.defaultCommand.description);
|
|
}
|
|
const builder = this.defaultCommand.builder;
|
|
if (isCommandBuilderCallback(builder)) {
|
|
return builder(yargs, true);
|
|
}
|
|
else if (!isCommandBuilderDefinition(builder)) {
|
|
Object.keys(builder).forEach(key => {
|
|
yargs.option(key, builder[key]);
|
|
});
|
|
}
|
|
return undefined;
|
|
}
|
|
moduleName(obj) {
|
|
const mod = whichModule(obj);
|
|
if (!mod)
|
|
throw new Error(`No command name given for module: ${this.shim.inspect(obj)}`);
|
|
return this.commandFromFilename(mod.filename);
|
|
}
|
|
commandFromFilename(filename) {
|
|
return this.shim.path.basename(filename, this.shim.path.extname(filename));
|
|
}
|
|
extractDesc({ describe, description, desc }) {
|
|
for (const test of [describe, description, desc]) {
|
|
if (typeof test === 'string' || test === false)
|
|
return test;
|
|
assertNotStrictEqual(test, true, this.shim);
|
|
}
|
|
return false;
|
|
}
|
|
freeze() {
|
|
this.frozens.push({
|
|
handlers: this.handlers,
|
|
aliasMap: this.aliasMap,
|
|
defaultCommand: this.defaultCommand,
|
|
});
|
|
}
|
|
unfreeze() {
|
|
const frozen = this.frozens.pop();
|
|
assertNotStrictEqual(frozen, undefined, this.shim);
|
|
({
|
|
handlers: this.handlers,
|
|
aliasMap: this.aliasMap,
|
|
defaultCommand: this.defaultCommand,
|
|
} = frozen);
|
|
}
|
|
reset() {
|
|
this.handlers = {};
|
|
this.aliasMap = {};
|
|
this.defaultCommand = undefined;
|
|
this.requireCache = new Set();
|
|
return this;
|
|
}
|
|
}
|
|
export function command(usage, validation, globalMiddleware, shim) {
|
|
return new CommandInstance(usage, validation, globalMiddleware, shim);
|
|
}
|
|
export function isCommandBuilderDefinition(builder) {
|
|
return (typeof builder === 'object' &&
|
|
!!builder.builder &&
|
|
typeof builder.handler === 'function');
|
|
}
|
|
function isCommandAndAliases(cmd) {
|
|
return cmd.every(c => typeof c === 'string');
|
|
}
|
|
export function isCommandBuilderCallback(builder) {
|
|
return typeof builder === 'function';
|
|
}
|
|
function isCommandBuilderOptionDefinitions(builder) {
|
|
return typeof builder === 'object';
|
|
}
|
|
export function isCommandHandlerDefinition(cmd) {
|
|
return typeof cmd === 'object' && !Array.isArray(cmd);
|
|
}
|