completion.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. 'use strict'
  2. const fs = require('fs')
  3. const path = require('path')
  4. // add bash completions to your
  5. // yargs-powered applications.
  6. module.exports = function completion (yargs, usage, command) {
  7. const self = {
  8. completionKey: 'get-yargs-completions'
  9. }
  10. // get a list of completion commands.
  11. // 'args' is the array of strings from the line to be completed
  12. self.getCompletion = function getCompletion (args, done) {
  13. const completions = []
  14. const current = args.length ? args[args.length - 1] : ''
  15. const argv = yargs.parse(args, true)
  16. const aliases = yargs.parsed.aliases
  17. // a custom completion function can be provided
  18. // to completion().
  19. if (completionFunction) {
  20. if (completionFunction.length < 3) {
  21. const result = completionFunction(current, argv)
  22. // promise based completion function.
  23. if (typeof result.then === 'function') {
  24. return result.then((list) => {
  25. process.nextTick(() => { done(list) })
  26. }).catch((err) => {
  27. process.nextTick(() => { throw err })
  28. })
  29. }
  30. // synchronous completion function.
  31. return done(result)
  32. } else {
  33. // asynchronous completion function
  34. return completionFunction(current, argv, (completions) => {
  35. done(completions)
  36. })
  37. }
  38. }
  39. const handlers = command.getCommandHandlers()
  40. for (let i = 0, ii = args.length; i < ii; ++i) {
  41. if (handlers[args[i]] && handlers[args[i]].builder) {
  42. const builder = handlers[args[i]].builder
  43. if (typeof builder === 'function') {
  44. const y = yargs.reset()
  45. builder(y)
  46. return y.argv
  47. }
  48. }
  49. }
  50. if (!current.match(/^-/)) {
  51. usage.getCommands().forEach((usageCommand) => {
  52. const commandName = command.parseCommand(usageCommand[0]).cmd
  53. if (args.indexOf(commandName) === -1) {
  54. completions.push(commandName)
  55. }
  56. })
  57. }
  58. if (current.match(/^-/)) {
  59. Object.keys(yargs.getOptions().key).forEach((key) => {
  60. // If the key and its aliases aren't in 'args', add the key to 'completions'
  61. const keyAndAliases = [key].concat(aliases[key] || [])
  62. const notInArgs = keyAndAliases.every(val => args.indexOf(`--${val}`) === -1)
  63. if (notInArgs) {
  64. completions.push(`--${key}`)
  65. }
  66. })
  67. }
  68. done(completions)
  69. }
  70. // generate the completion script to add to your .bashrc.
  71. self.generateCompletionScript = function generateCompletionScript ($0, cmd) {
  72. let script = fs.readFileSync(
  73. path.resolve(__dirname, '../completion.sh.hbs'),
  74. 'utf-8'
  75. )
  76. const name = path.basename($0)
  77. // add ./to applications not yet installed as bin.
  78. if ($0.match(/\.js$/)) $0 = `./${$0}`
  79. script = script.replace(/{{app_name}}/g, name)
  80. script = script.replace(/{{completion_command}}/g, cmd)
  81. return script.replace(/{{app_path}}/g, $0)
  82. }
  83. // register a function to perform your own custom
  84. // completions., this function can be either
  85. // synchrnous or asynchronous.
  86. let completionFunction = null
  87. self.registerFunction = (fn) => {
  88. completionFunction = fn
  89. }
  90. return self
  91. }