command.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. import { assertNotStrictEqual, } from './typings/common-types.js';
  2. import { isPromise } from './utils/is-promise.js';
  3. import { applyMiddleware, commandMiddlewareFactory, } from './middleware.js';
  4. import { parseCommand } from './parse-command.js';
  5. import { isYargsInstance, } from './yargs-factory.js';
  6. import { maybeAsyncResult } from './utils/maybe-async-result.js';
  7. import whichModule from './utils/which-module.js';
  8. const DEFAULT_MARKER = /(^\*)|(^\$0)/;
  9. export class CommandInstance {
  10. constructor(usage, validation, globalMiddleware, shim) {
  11. this.requireCache = new Set();
  12. this.handlers = {};
  13. this.aliasMap = {};
  14. this.frozens = [];
  15. this.shim = shim;
  16. this.usage = usage;
  17. this.globalMiddleware = globalMiddleware;
  18. this.validation = validation;
  19. }
  20. addDirectory(dir, req, callerFile, opts) {
  21. opts = opts || {};
  22. if (typeof opts.recurse !== 'boolean')
  23. opts.recurse = false;
  24. if (!Array.isArray(opts.extensions))
  25. opts.extensions = ['js'];
  26. const parentVisit = typeof opts.visit === 'function' ? opts.visit : (o) => o;
  27. opts.visit = (obj, joined, filename) => {
  28. const visited = parentVisit(obj, joined, filename);
  29. if (visited) {
  30. if (this.requireCache.has(joined))
  31. return visited;
  32. else
  33. this.requireCache.add(joined);
  34. this.addHandler(visited);
  35. }
  36. return visited;
  37. };
  38. this.shim.requireDirectory({ require: req, filename: callerFile }, dir, opts);
  39. }
  40. addHandler(cmd, description, builder, handler, commandMiddleware, deprecated) {
  41. let aliases = [];
  42. const middlewares = commandMiddlewareFactory(commandMiddleware);
  43. handler = handler || (() => { });
  44. if (Array.isArray(cmd)) {
  45. if (isCommandAndAliases(cmd)) {
  46. [cmd, ...aliases] = cmd;
  47. }
  48. else {
  49. for (const command of cmd) {
  50. this.addHandler(command);
  51. }
  52. }
  53. }
  54. else if (isCommandHandlerDefinition(cmd)) {
  55. let command = Array.isArray(cmd.command) || typeof cmd.command === 'string'
  56. ? cmd.command
  57. : this.moduleName(cmd);
  58. if (cmd.aliases)
  59. command = [].concat(command).concat(cmd.aliases);
  60. this.addHandler(command, this.extractDesc(cmd), cmd.builder, cmd.handler, cmd.middlewares, cmd.deprecated);
  61. return;
  62. }
  63. else if (isCommandBuilderDefinition(builder)) {
  64. this.addHandler([cmd].concat(aliases), description, builder.builder, builder.handler, builder.middlewares, builder.deprecated);
  65. return;
  66. }
  67. if (typeof cmd === 'string') {
  68. const parsedCommand = parseCommand(cmd);
  69. aliases = aliases.map(alias => parseCommand(alias).cmd);
  70. let isDefault = false;
  71. const parsedAliases = [parsedCommand.cmd].concat(aliases).filter(c => {
  72. if (DEFAULT_MARKER.test(c)) {
  73. isDefault = true;
  74. return false;
  75. }
  76. return true;
  77. });
  78. if (parsedAliases.length === 0 && isDefault)
  79. parsedAliases.push('$0');
  80. if (isDefault) {
  81. parsedCommand.cmd = parsedAliases[0];
  82. aliases = parsedAliases.slice(1);
  83. cmd = cmd.replace(DEFAULT_MARKER, parsedCommand.cmd);
  84. }
  85. aliases.forEach(alias => {
  86. this.aliasMap[alias] = parsedCommand.cmd;
  87. });
  88. if (description !== false) {
  89. this.usage.command(cmd, description, isDefault, aliases, deprecated);
  90. }
  91. this.handlers[parsedCommand.cmd] = {
  92. original: cmd,
  93. description,
  94. handler,
  95. builder: builder || {},
  96. middlewares,
  97. deprecated,
  98. demanded: parsedCommand.demanded,
  99. optional: parsedCommand.optional,
  100. };
  101. if (isDefault)
  102. this.defaultCommand = this.handlers[parsedCommand.cmd];
  103. }
  104. }
  105. getCommandHandlers() {
  106. return this.handlers;
  107. }
  108. getCommands() {
  109. return Object.keys(this.handlers).concat(Object.keys(this.aliasMap));
  110. }
  111. hasDefaultCommand() {
  112. return !!this.defaultCommand;
  113. }
  114. runCommand(command, yargs, parsed, commandIndex, helpOnly, helpOrVersionSet) {
  115. const commandHandler = this.handlers[command] ||
  116. this.handlers[this.aliasMap[command]] ||
  117. this.defaultCommand;
  118. const currentContext = yargs.getInternalMethods().getContext();
  119. const parentCommands = currentContext.commands.slice();
  120. const isDefaultCommand = !command;
  121. if (command) {
  122. currentContext.commands.push(command);
  123. currentContext.fullCommands.push(commandHandler.original);
  124. }
  125. const builderResult = this.applyBuilderUpdateUsageAndParse(isDefaultCommand, commandHandler, yargs, parsed.aliases, parentCommands, commandIndex, helpOnly, helpOrVersionSet);
  126. return isPromise(builderResult)
  127. ? builderResult.then(result => this.applyMiddlewareAndGetResult(isDefaultCommand, commandHandler, result.innerArgv, currentContext, helpOnly, result.aliases, yargs))
  128. : this.applyMiddlewareAndGetResult(isDefaultCommand, commandHandler, builderResult.innerArgv, currentContext, helpOnly, builderResult.aliases, yargs);
  129. }
  130. applyBuilderUpdateUsageAndParse(isDefaultCommand, commandHandler, yargs, aliases, parentCommands, commandIndex, helpOnly, helpOrVersionSet) {
  131. const builder = commandHandler.builder;
  132. let innerYargs = yargs;
  133. if (isCommandBuilderCallback(builder)) {
  134. yargs.getInternalMethods().getUsageInstance().freeze();
  135. const builderOutput = builder(yargs.getInternalMethods().reset(aliases), helpOrVersionSet);
  136. if (isPromise(builderOutput)) {
  137. return builderOutput.then(output => {
  138. innerYargs = isYargsInstance(output) ? output : yargs;
  139. return this.parseAndUpdateUsage(isDefaultCommand, commandHandler, innerYargs, parentCommands, commandIndex, helpOnly);
  140. });
  141. }
  142. }
  143. else if (isCommandBuilderOptionDefinitions(builder)) {
  144. yargs.getInternalMethods().getUsageInstance().freeze();
  145. innerYargs = yargs.getInternalMethods().reset(aliases);
  146. Object.keys(commandHandler.builder).forEach(key => {
  147. innerYargs.option(key, builder[key]);
  148. });
  149. }
  150. return this.parseAndUpdateUsage(isDefaultCommand, commandHandler, innerYargs, parentCommands, commandIndex, helpOnly);
  151. }
  152. parseAndUpdateUsage(isDefaultCommand, commandHandler, innerYargs, parentCommands, commandIndex, helpOnly) {
  153. if (isDefaultCommand)
  154. innerYargs.getInternalMethods().getUsageInstance().unfreeze(true);
  155. if (this.shouldUpdateUsage(innerYargs)) {
  156. innerYargs
  157. .getInternalMethods()
  158. .getUsageInstance()
  159. .usage(this.usageFromParentCommandsCommandHandler(parentCommands, commandHandler), commandHandler.description);
  160. }
  161. const innerArgv = innerYargs
  162. .getInternalMethods()
  163. .runYargsParserAndExecuteCommands(null, undefined, true, commandIndex, helpOnly);
  164. return isPromise(innerArgv)
  165. ? innerArgv.then(argv => ({
  166. aliases: innerYargs.parsed.aliases,
  167. innerArgv: argv,
  168. }))
  169. : {
  170. aliases: innerYargs.parsed.aliases,
  171. innerArgv: innerArgv,
  172. };
  173. }
  174. shouldUpdateUsage(yargs) {
  175. return (!yargs.getInternalMethods().getUsageInstance().getUsageDisabled() &&
  176. yargs.getInternalMethods().getUsageInstance().getUsage().length === 0);
  177. }
  178. usageFromParentCommandsCommandHandler(parentCommands, commandHandler) {
  179. const c = DEFAULT_MARKER.test(commandHandler.original)
  180. ? commandHandler.original.replace(DEFAULT_MARKER, '').trim()
  181. : commandHandler.original;
  182. const pc = parentCommands.filter(c => {
  183. return !DEFAULT_MARKER.test(c);
  184. });
  185. pc.push(c);
  186. return `$0 ${pc.join(' ')}`;
  187. }
  188. handleValidationAndGetResult(isDefaultCommand, commandHandler, innerArgv, currentContext, aliases, yargs, middlewares, positionalMap) {
  189. if (!yargs.getInternalMethods().getHasOutput()) {
  190. const validation = yargs
  191. .getInternalMethods()
  192. .runValidation(aliases, positionalMap, yargs.parsed.error, isDefaultCommand);
  193. innerArgv = maybeAsyncResult(innerArgv, result => {
  194. validation(result);
  195. return result;
  196. });
  197. }
  198. if (commandHandler.handler && !yargs.getInternalMethods().getHasOutput()) {
  199. yargs.getInternalMethods().setHasOutput();
  200. const populateDoubleDash = !!yargs.getOptions().configuration['populate--'];
  201. yargs
  202. .getInternalMethods()
  203. .postProcess(innerArgv, populateDoubleDash, false, false);
  204. innerArgv = applyMiddleware(innerArgv, yargs, middlewares, false);
  205. innerArgv = maybeAsyncResult(innerArgv, result => {
  206. const handlerResult = commandHandler.handler(result);
  207. return isPromise(handlerResult)
  208. ? handlerResult.then(() => result)
  209. : result;
  210. });
  211. if (!isDefaultCommand) {
  212. yargs.getInternalMethods().getUsageInstance().cacheHelpMessage();
  213. }
  214. if (isPromise(innerArgv) &&
  215. !yargs.getInternalMethods().hasParseCallback()) {
  216. innerArgv.catch(error => {
  217. try {
  218. yargs.getInternalMethods().getUsageInstance().fail(null, error);
  219. }
  220. catch (_err) {
  221. }
  222. });
  223. }
  224. }
  225. if (!isDefaultCommand) {
  226. currentContext.commands.pop();
  227. currentContext.fullCommands.pop();
  228. }
  229. return innerArgv;
  230. }
  231. applyMiddlewareAndGetResult(isDefaultCommand, commandHandler, innerArgv, currentContext, helpOnly, aliases, yargs) {
  232. let positionalMap = {};
  233. if (helpOnly)
  234. return innerArgv;
  235. if (!yargs.getInternalMethods().getHasOutput()) {
  236. positionalMap = this.populatePositionals(commandHandler, innerArgv, currentContext, yargs);
  237. }
  238. const middlewares = this.globalMiddleware
  239. .getMiddleware()
  240. .slice(0)
  241. .concat(commandHandler.middlewares);
  242. const maybePromiseArgv = applyMiddleware(innerArgv, yargs, middlewares, true);
  243. return isPromise(maybePromiseArgv)
  244. ? maybePromiseArgv.then(resolvedInnerArgv => this.handleValidationAndGetResult(isDefaultCommand, commandHandler, resolvedInnerArgv, currentContext, aliases, yargs, middlewares, positionalMap))
  245. : this.handleValidationAndGetResult(isDefaultCommand, commandHandler, maybePromiseArgv, currentContext, aliases, yargs, middlewares, positionalMap);
  246. }
  247. populatePositionals(commandHandler, argv, context, yargs) {
  248. argv._ = argv._.slice(context.commands.length);
  249. const demanded = commandHandler.demanded.slice(0);
  250. const optional = commandHandler.optional.slice(0);
  251. const positionalMap = {};
  252. this.validation.positionalCount(demanded.length, argv._.length);
  253. while (demanded.length) {
  254. const demand = demanded.shift();
  255. this.populatePositional(demand, argv, positionalMap);
  256. }
  257. while (optional.length) {
  258. const maybe = optional.shift();
  259. this.populatePositional(maybe, argv, positionalMap);
  260. }
  261. argv._ = context.commands.concat(argv._.map(a => '' + a));
  262. this.postProcessPositionals(argv, positionalMap, this.cmdToParseOptions(commandHandler.original), yargs);
  263. return positionalMap;
  264. }
  265. populatePositional(positional, argv, positionalMap) {
  266. const cmd = positional.cmd[0];
  267. if (positional.variadic) {
  268. positionalMap[cmd] = argv._.splice(0).map(String);
  269. }
  270. else {
  271. if (argv._.length)
  272. positionalMap[cmd] = [String(argv._.shift())];
  273. }
  274. }
  275. cmdToParseOptions(cmdString) {
  276. const parseOptions = {
  277. array: [],
  278. default: {},
  279. alias: {},
  280. demand: {},
  281. };
  282. const parsed = parseCommand(cmdString);
  283. parsed.demanded.forEach(d => {
  284. const [cmd, ...aliases] = d.cmd;
  285. if (d.variadic) {
  286. parseOptions.array.push(cmd);
  287. parseOptions.default[cmd] = [];
  288. }
  289. parseOptions.alias[cmd] = aliases;
  290. parseOptions.demand[cmd] = true;
  291. });
  292. parsed.optional.forEach(o => {
  293. const [cmd, ...aliases] = o.cmd;
  294. if (o.variadic) {
  295. parseOptions.array.push(cmd);
  296. parseOptions.default[cmd] = [];
  297. }
  298. parseOptions.alias[cmd] = aliases;
  299. });
  300. return parseOptions;
  301. }
  302. postProcessPositionals(argv, positionalMap, parseOptions, yargs) {
  303. const options = Object.assign({}, yargs.getOptions());
  304. options.default = Object.assign(parseOptions.default, options.default);
  305. for (const key of Object.keys(parseOptions.alias)) {
  306. options.alias[key] = (options.alias[key] || []).concat(parseOptions.alias[key]);
  307. }
  308. options.array = options.array.concat(parseOptions.array);
  309. options.config = {};
  310. const unparsed = [];
  311. Object.keys(positionalMap).forEach(key => {
  312. positionalMap[key].map(value => {
  313. if (options.configuration['unknown-options-as-args'])
  314. options.key[key] = true;
  315. unparsed.push(`--${key}`);
  316. unparsed.push(value);
  317. });
  318. });
  319. if (!unparsed.length)
  320. return;
  321. const config = Object.assign({}, options.configuration, {
  322. 'populate--': false,
  323. });
  324. const parsed = this.shim.Parser.detailed(unparsed, Object.assign({}, options, {
  325. configuration: config,
  326. }));
  327. if (parsed.error) {
  328. yargs
  329. .getInternalMethods()
  330. .getUsageInstance()
  331. .fail(parsed.error.message, parsed.error);
  332. }
  333. else {
  334. const positionalKeys = Object.keys(positionalMap);
  335. Object.keys(positionalMap).forEach(key => {
  336. positionalKeys.push(...parsed.aliases[key]);
  337. });
  338. Object.keys(parsed.argv).forEach(key => {
  339. if (positionalKeys.includes(key)) {
  340. if (!positionalMap[key])
  341. positionalMap[key] = parsed.argv[key];
  342. if (!this.isInConfigs(yargs, key) &&
  343. !this.isDefaulted(yargs, key) &&
  344. Object.prototype.hasOwnProperty.call(argv, key) &&
  345. Object.prototype.hasOwnProperty.call(parsed.argv, key) &&
  346. (Array.isArray(argv[key]) || Array.isArray(parsed.argv[key]))) {
  347. argv[key] = [].concat(argv[key], parsed.argv[key]);
  348. }
  349. else {
  350. argv[key] = parsed.argv[key];
  351. }
  352. }
  353. });
  354. }
  355. }
  356. isDefaulted(yargs, key) {
  357. const { default: defaults } = yargs.getOptions();
  358. return (Object.prototype.hasOwnProperty.call(defaults, key) ||
  359. Object.prototype.hasOwnProperty.call(defaults, this.shim.Parser.camelCase(key)));
  360. }
  361. isInConfigs(yargs, key) {
  362. const { configObjects } = yargs.getOptions();
  363. return (configObjects.some(c => Object.prototype.hasOwnProperty.call(c, key)) ||
  364. configObjects.some(c => Object.prototype.hasOwnProperty.call(c, this.shim.Parser.camelCase(key))));
  365. }
  366. runDefaultBuilderOn(yargs) {
  367. if (!this.defaultCommand)
  368. return;
  369. if (this.shouldUpdateUsage(yargs)) {
  370. const commandString = DEFAULT_MARKER.test(this.defaultCommand.original)
  371. ? this.defaultCommand.original
  372. : this.defaultCommand.original.replace(/^[^[\]<>]*/, '$0 ');
  373. yargs
  374. .getInternalMethods()
  375. .getUsageInstance()
  376. .usage(commandString, this.defaultCommand.description);
  377. }
  378. const builder = this.defaultCommand.builder;
  379. if (isCommandBuilderCallback(builder)) {
  380. return builder(yargs, true);
  381. }
  382. else if (!isCommandBuilderDefinition(builder)) {
  383. Object.keys(builder).forEach(key => {
  384. yargs.option(key, builder[key]);
  385. });
  386. }
  387. return undefined;
  388. }
  389. moduleName(obj) {
  390. const mod = whichModule(obj);
  391. if (!mod)
  392. throw new Error(`No command name given for module: ${this.shim.inspect(obj)}`);
  393. return this.commandFromFilename(mod.filename);
  394. }
  395. commandFromFilename(filename) {
  396. return this.shim.path.basename(filename, this.shim.path.extname(filename));
  397. }
  398. extractDesc({ describe, description, desc }) {
  399. for (const test of [describe, description, desc]) {
  400. if (typeof test === 'string' || test === false)
  401. return test;
  402. assertNotStrictEqual(test, true, this.shim);
  403. }
  404. return false;
  405. }
  406. freeze() {
  407. this.frozens.push({
  408. handlers: this.handlers,
  409. aliasMap: this.aliasMap,
  410. defaultCommand: this.defaultCommand,
  411. });
  412. }
  413. unfreeze() {
  414. const frozen = this.frozens.pop();
  415. assertNotStrictEqual(frozen, undefined, this.shim);
  416. ({
  417. handlers: this.handlers,
  418. aliasMap: this.aliasMap,
  419. defaultCommand: this.defaultCommand,
  420. } = frozen);
  421. }
  422. reset() {
  423. this.handlers = {};
  424. this.aliasMap = {};
  425. this.defaultCommand = undefined;
  426. this.requireCache = new Set();
  427. return this;
  428. }
  429. }
  430. export function command(usage, validation, globalMiddleware, shim) {
  431. return new CommandInstance(usage, validation, globalMiddleware, shim);
  432. }
  433. export function isCommandBuilderDefinition(builder) {
  434. return (typeof builder === 'object' &&
  435. !!builder.builder &&
  436. typeof builder.handler === 'function');
  437. }
  438. function isCommandAndAliases(cmd) {
  439. return cmd.every(c => typeof c === 'string');
  440. }
  441. export function isCommandBuilderCallback(builder) {
  442. return typeof builder === 'function';
  443. }
  444. function isCommandBuilderOptionDefinitions(builder) {
  445. return typeof builder === 'object';
  446. }
  447. export function isCommandHandlerDefinition(cmd) {
  448. return typeof cmd === 'object' && !Array.isArray(cmd);
  449. }