2019-11-07 13:11:05 +00:00
|
|
|
'use strict'
|
|
|
|
const path = require('path')
|
2019-09-18 08:11:16 +00:00
|
|
|
|
|
|
|
// add bash completions to your
|
|
|
|
// yargs-powered applications.
|
2019-11-07 13:11:05 +00:00
|
|
|
module.exports = function completion (yargs, usage, command) {
|
|
|
|
const self = {
|
2019-09-18 08:11:16 +00:00
|
|
|
completionKey: 'get-yargs-completions'
|
|
|
|
}
|
|
|
|
|
2019-11-07 13:11:05 +00:00
|
|
|
const zshShell = (process.env.SHELL && process.env.SHELL.indexOf('zsh') !== -1) ||
|
|
|
|
(process.env.ZSH_NAME && process.env.ZSH_NAME.indexOf('zsh') !== -1)
|
2019-09-18 08:11:16 +00:00
|
|
|
// get a list of completion commands.
|
2019-11-07 13:11:05 +00:00
|
|
|
// 'args' is the array of strings from the line to be completed
|
|
|
|
self.getCompletion = function getCompletion (args, done) {
|
|
|
|
const completions = []
|
|
|
|
const current = args.length ? args[args.length - 1] : ''
|
|
|
|
const argv = yargs.parse(args, true)
|
|
|
|
const aliases = yargs.parsed.aliases
|
|
|
|
const parentCommands = yargs.getContext().commands
|
2019-09-18 08:11:16 +00:00
|
|
|
|
|
|
|
// a custom completion function can be provided
|
|
|
|
// to completion().
|
|
|
|
if (completionFunction) {
|
|
|
|
if (completionFunction.length < 3) {
|
2019-11-07 13:11:05 +00:00
|
|
|
const result = completionFunction(current, argv)
|
2019-09-18 08:11:16 +00:00
|
|
|
|
|
|
|
// promise based completion function.
|
|
|
|
if (typeof result.then === 'function') {
|
2019-11-07 13:11:05 +00:00
|
|
|
return result.then((list) => {
|
|
|
|
process.nextTick(() => { done(list) })
|
|
|
|
}).catch((err) => {
|
|
|
|
process.nextTick(() => { throw err })
|
2019-09-18 08:11:16 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// synchronous completion function.
|
|
|
|
return done(result)
|
|
|
|
} else {
|
|
|
|
// asynchronous completion function
|
2019-11-07 13:11:05 +00:00
|
|
|
return completionFunction(current, argv, (completions) => {
|
2019-09-18 08:11:16 +00:00
|
|
|
done(completions)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-07 13:11:05 +00:00
|
|
|
const handlers = command.getCommandHandlers()
|
|
|
|
for (let i = 0, ii = args.length; i < ii; ++i) {
|
|
|
|
if (handlers[args[i]] && handlers[args[i]].builder) {
|
|
|
|
const builder = handlers[args[i]].builder
|
|
|
|
if (typeof builder === 'function') {
|
|
|
|
const y = yargs.reset()
|
|
|
|
builder(y)
|
|
|
|
return y.argv
|
|
|
|
}
|
2019-09-18 08:11:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-07 13:11:05 +00:00
|
|
|
if (!current.match(/^-/) && parentCommands[parentCommands.length - 1] !== current) {
|
|
|
|
usage.getCommands().forEach((usageCommand) => {
|
|
|
|
const commandName = command.parseCommand(usageCommand[0]).cmd
|
|
|
|
if (args.indexOf(commandName) === -1) {
|
|
|
|
if (!zshShell) {
|
|
|
|
completions.push(commandName)
|
|
|
|
} else {
|
|
|
|
const desc = usageCommand[1] || ''
|
|
|
|
completions.push(commandName.replace(/:/g, '\\:') + ':' + desc)
|
|
|
|
}
|
2019-09-18 08:11:16 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-11-07 13:11:05 +00:00
|
|
|
if (current.match(/^-/) || (current === '' && completions.length === 0)) {
|
|
|
|
const descs = usage.getDescriptions()
|
|
|
|
Object.keys(yargs.getOptions().key).forEach((key) => {
|
|
|
|
// If the key and its aliases aren't in 'args', add the key to 'completions'
|
|
|
|
const keyAndAliases = [key].concat(aliases[key] || [])
|
|
|
|
const notInArgs = keyAndAliases.every(val => args.indexOf(`--${val}`) === -1)
|
|
|
|
if (notInArgs) {
|
|
|
|
if (!zshShell) {
|
|
|
|
completions.push(`--${key}`)
|
|
|
|
} else {
|
|
|
|
const desc = descs[key] || ''
|
|
|
|
completions.push(`--${key.replace(/:/g, '\\:')}:${desc.replace('__yargsString__:', '')}`)
|
|
|
|
}
|
|
|
|
}
|
2019-09-18 08:11:16 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
done(completions)
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate the completion script to add to your .bashrc.
|
2019-11-07 13:11:05 +00:00
|
|
|
self.generateCompletionScript = function generateCompletionScript ($0, cmd) {
|
|
|
|
const templates = require('./completion-templates')
|
|
|
|
let script = zshShell ? templates.completionZshTemplate : templates.completionShTemplate
|
|
|
|
const name = path.basename($0)
|
2019-09-18 08:11:16 +00:00
|
|
|
|
|
|
|
// add ./to applications not yet installed as bin.
|
2019-11-07 13:11:05 +00:00
|
|
|
if ($0.match(/\.js$/)) $0 = `./${$0}`
|
2019-09-18 08:11:16 +00:00
|
|
|
|
|
|
|
script = script.replace(/{{app_name}}/g, name)
|
2019-11-07 13:11:05 +00:00
|
|
|
script = script.replace(/{{completion_command}}/g, cmd)
|
2019-09-18 08:11:16 +00:00
|
|
|
return script.replace(/{{app_path}}/g, $0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// register a function to perform your own custom
|
|
|
|
// completions., this function can be either
|
|
|
|
// synchrnous or asynchronous.
|
2019-11-07 13:11:05 +00:00
|
|
|
let completionFunction = null
|
|
|
|
self.registerFunction = (fn) => {
|
2019-09-18 08:11:16 +00:00
|
|
|
completionFunction = fn
|
|
|
|
}
|
|
|
|
|
|
|
|
return self
|
|
|
|
}
|