completion.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { isCommandBuilderCallback } from './command.js';
  2. import { assertNotStrictEqual } from './typings/common-types.js';
  3. import * as templates from './completion-templates.js';
  4. import { isPromise } from './utils/is-promise.js';
  5. import { parseCommand } from './parse-command.js';
  6. export function completion(yargs, usage, command, shim) {
  7. const self = {
  8. completionKey: 'get-yargs-completions',
  9. };
  10. let aliases;
  11. self.setParsed = function setParsed(parsed) {
  12. aliases = parsed.aliases;
  13. };
  14. const zshShell = (shim.getEnv('SHELL') && shim.getEnv('SHELL').indexOf('zsh') !== -1) ||
  15. (shim.getEnv('ZSH_NAME') && shim.getEnv('ZSH_NAME').indexOf('zsh') !== -1);
  16. self.getCompletion = function getCompletion(args, done) {
  17. const completions = [];
  18. const current = args.length ? args[args.length - 1] : '';
  19. const argv = yargs.parse(args, true);
  20. const parentCommands = yargs.getContext().commands;
  21. function runCompletionFunction(argv) {
  22. assertNotStrictEqual(completionFunction, null, shim);
  23. if (isSyncCompletionFunction(completionFunction)) {
  24. const result = completionFunction(current, argv);
  25. if (isPromise(result)) {
  26. return result
  27. .then(list => {
  28. shim.process.nextTick(() => {
  29. done(list);
  30. });
  31. })
  32. .catch(err => {
  33. shim.process.nextTick(() => {
  34. throw err;
  35. });
  36. });
  37. }
  38. return done(result);
  39. }
  40. else {
  41. return completionFunction(current, argv, completions => {
  42. done(completions);
  43. });
  44. }
  45. }
  46. if (completionFunction) {
  47. return isPromise(argv)
  48. ? argv.then(runCompletionFunction)
  49. : runCompletionFunction(argv);
  50. }
  51. const handlers = command.getCommandHandlers();
  52. for (let i = 0, ii = args.length; i < ii; ++i) {
  53. if (handlers[args[i]] && handlers[args[i]].builder) {
  54. const builder = handlers[args[i]].builder;
  55. if (isCommandBuilderCallback(builder)) {
  56. const y = yargs.reset();
  57. builder(y);
  58. return y.argv;
  59. }
  60. }
  61. }
  62. if (!current.match(/^-/) &&
  63. parentCommands[parentCommands.length - 1] !== current) {
  64. usage.getCommands().forEach(usageCommand => {
  65. const commandName = parseCommand(usageCommand[0]).cmd;
  66. if (args.indexOf(commandName) === -1) {
  67. if (!zshShell) {
  68. completions.push(commandName);
  69. }
  70. else {
  71. const desc = usageCommand[1] || '';
  72. completions.push(commandName.replace(/:/g, '\\:') + ':' + desc);
  73. }
  74. }
  75. });
  76. }
  77. if (current.match(/^-/) || (current === '' && completions.length === 0)) {
  78. const descs = usage.getDescriptions();
  79. const options = yargs.getOptions();
  80. Object.keys(options.key).forEach(key => {
  81. const negable = !!options.configuration['boolean-negation'] &&
  82. options.boolean.includes(key);
  83. let keyAndAliases = [key].concat(aliases[key] || []);
  84. if (negable)
  85. keyAndAliases = keyAndAliases.concat(keyAndAliases.map(key => `no-${key}`));
  86. function completeOptionKey(key) {
  87. const notInArgs = keyAndAliases.every(val => args.indexOf(`--${val}`) === -1);
  88. if (notInArgs) {
  89. const startsByTwoDashes = (s) => /^--/.test(s);
  90. const isShortOption = (s) => /^[^0-9]$/.test(s);
  91. const dashes = !startsByTwoDashes(current) && isShortOption(key) ? '-' : '--';
  92. if (!zshShell) {
  93. completions.push(dashes + key);
  94. }
  95. else {
  96. const desc = descs[key] || '';
  97. completions.push(dashes +
  98. `${key.replace(/:/g, '\\:')}:${desc.replace('__yargsString__:', '')}`);
  99. }
  100. }
  101. }
  102. completeOptionKey(key);
  103. if (negable && !!options.default[key])
  104. completeOptionKey(`no-${key}`);
  105. });
  106. }
  107. done(completions);
  108. };
  109. self.generateCompletionScript = function generateCompletionScript($0, cmd) {
  110. let script = zshShell
  111. ? templates.completionZshTemplate
  112. : templates.completionShTemplate;
  113. const name = shim.path.basename($0);
  114. if ($0.match(/\.js$/))
  115. $0 = `./${$0}`;
  116. script = script.replace(/{{app_name}}/g, name);
  117. script = script.replace(/{{completion_command}}/g, cmd);
  118. return script.replace(/{{app_path}}/g, $0);
  119. };
  120. let completionFunction = null;
  121. self.registerFunction = fn => {
  122. completionFunction = fn;
  123. };
  124. return self;
  125. }
  126. function isSyncCompletionFunction(completionFunction) {
  127. return completionFunction.length < 3;
  128. }