wallet-core/node_modules/yargs/lib/completion.js

106 lines
3.1 KiB
JavaScript
Raw Normal View History

2017-12-10 21:51:33 +01:00
'use strict'
2017-05-03 15:35:00 +02:00
const fs = require('fs')
const path = require('path')
// add bash completions to your
// yargs-powered applications.
2017-12-10 21:51:33 +01:00
module.exports = function completion (yargs, usage, command) {
2017-05-03 15:35:00 +02:00
const self = {
completionKey: 'get-yargs-completions'
}
// get a list of completion commands.
// 'args' is the array of strings from the line to be completed
2017-12-10 21:51:33 +01:00
self.getCompletion = function getCompletion (args, done) {
2017-05-03 15:35:00 +02:00
const completions = []
const current = args.length ? args[args.length - 1] : ''
const argv = yargs.parse(args, true)
const aliases = yargs.parsed.aliases
// a custom completion function can be provided
// to completion().
if (completionFunction) {
if (completionFunction.length < 3) {
2017-12-10 21:51:33 +01:00
const result = completionFunction(current, argv)
2017-05-03 15:35:00 +02:00
// promise based completion function.
if (typeof result.then === 'function') {
2017-12-10 21:51:33 +01:00
return result.then((list) => {
process.nextTick(() => { done(list) })
}).catch((err) => {
process.nextTick(() => { throw err })
2017-05-03 15:35:00 +02:00
})
}
// synchronous completion function.
return done(result)
} else {
// asynchronous completion function
2017-12-10 21:51:33 +01:00
return completionFunction(current, argv, (completions) => {
2017-05-03 15:35:00 +02:00
done(completions)
})
}
}
2017-12-10 21:51:33 +01:00
const handlers = command.getCommandHandlers()
for (let i = 0, ii = args.length; i < ii; ++i) {
2017-05-03 15:35:00 +02:00
if (handlers[args[i]] && handlers[args[i]].builder) {
2017-08-14 05:01:11 +02:00
const builder = handlers[args[i]].builder
if (typeof builder === 'function') {
const y = yargs.reset()
builder(y)
return y.argv
}
2017-05-03 15:35:00 +02:00
}
}
if (!current.match(/^-/)) {
2017-12-10 21:51:33 +01:00
usage.getCommands().forEach((usageCommand) => {
const commandName = command.parseCommand(usageCommand[0]).cmd
if (args.indexOf(commandName) === -1) {
completions.push(commandName)
2017-05-03 15:35:00 +02:00
}
})
}
if (current.match(/^-/)) {
2017-12-10 21:51:33 +01:00
Object.keys(yargs.getOptions().key).forEach((key) => {
2017-05-03 15:35:00 +02:00
// If the key and its aliases aren't in 'args', add the key to 'completions'
2017-12-10 21:51:33 +01:00
const keyAndAliases = [key].concat(aliases[key] || [])
const notInArgs = keyAndAliases.every(val => args.indexOf(`--${val}`) === -1)
2017-05-03 15:35:00 +02:00
if (notInArgs) {
2017-12-10 21:51:33 +01:00
completions.push(`--${key}`)
2017-05-03 15:35:00 +02:00
}
})
}
done(completions)
}
// generate the completion script to add to your .bashrc.
2017-12-10 21:51:33 +01:00
self.generateCompletionScript = function generateCompletionScript ($0, cmd) {
let script = fs.readFileSync(
2017-05-03 15:35:00 +02:00
path.resolve(__dirname, '../completion.sh.hbs'),
'utf-8'
)
2017-12-10 21:51:33 +01:00
const name = path.basename($0)
2017-05-03 15:35:00 +02:00
// add ./to applications not yet installed as bin.
2017-12-10 21:51:33 +01:00
if ($0.match(/\.js$/)) $0 = `./${$0}`
2017-05-03 15:35:00 +02:00
script = script.replace(/{{app_name}}/g, name)
2017-12-10 21:51:33 +01:00
script = script.replace(/{{completion_command}}/g, cmd)
2017-05-03 15:35:00 +02: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.
2017-12-10 21:51:33 +01:00
let completionFunction = null
self.registerFunction = (fn) => {
2017-05-03 15:35:00 +02:00
completionFunction = fn
}
return self
}