Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/yargs/lib/completion.js')
-rw-r--r--node_modules/yargs/lib/completion.js29
1 files changed, 20 insertions, 9 deletions
diff --git a/node_modules/yargs/lib/completion.js b/node_modules/yargs/lib/completion.js
index ad6969a2d..3f3bf16e1 100644
--- a/node_modules/yargs/lib/completion.js
+++ b/node_modules/yargs/lib/completion.js
@@ -1,5 +1,4 @@
'use strict'
-const fs = require('fs')
const path = require('path')
// add bash completions to your
@@ -9,6 +8,8 @@ module.exports = function completion (yargs, usage, command) {
completionKey: 'get-yargs-completions'
}
+ const zshShell = (process.env.SHELL && process.env.SHELL.indexOf('zsh') !== -1) ||
+ (process.env.ZSH_NAME && process.env.ZSH_NAME.indexOf('zsh') !== -1)
// get a list of completion commands.
// 'args' is the array of strings from the line to be completed
self.getCompletion = function getCompletion (args, done) {
@@ -16,6 +17,7 @@ module.exports = function completion (yargs, usage, command) {
const current = args.length ? args[args.length - 1] : ''
const argv = yargs.parse(args, true)
const aliases = yargs.parsed.aliases
+ const parentCommands = yargs.getContext().commands
// a custom completion function can be provided
// to completion().
@@ -54,22 +56,33 @@ module.exports = function completion (yargs, usage, command) {
}
}
- if (!current.match(/^-/)) {
+ if (!current.match(/^-/) && parentCommands[parentCommands.length - 1] !== current) {
usage.getCommands().forEach((usageCommand) => {
const commandName = command.parseCommand(usageCommand[0]).cmd
if (args.indexOf(commandName) === -1) {
- completions.push(commandName)
+ if (!zshShell) {
+ completions.push(commandName)
+ } else {
+ const desc = usageCommand[1] || ''
+ completions.push(commandName.replace(/:/g, '\\:') + ':' + desc)
+ }
}
})
}
- if (current.match(/^-/)) {
+ 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) {
- completions.push(`--${key}`)
+ if (!zshShell) {
+ completions.push(`--${key}`)
+ } else {
+ const desc = descs[key] || ''
+ completions.push(`--${key.replace(/:/g, '\\:')}:${desc.replace('__yargsString__:', '')}`)
+ }
}
})
}
@@ -79,10 +92,8 @@ module.exports = function completion (yargs, usage, command) {
// generate the completion script to add to your .bashrc.
self.generateCompletionScript = function generateCompletionScript ($0, cmd) {
- let script = fs.readFileSync(
- path.resolve(__dirname, '../completion.sh.hbs'),
- 'utf-8'
- )
+ const templates = require('./completion-templates')
+ let script = zshShell ? templates.completionZshTemplate : templates.completionShTemplate
const name = path.basename($0)
// add ./to applications not yet installed as bin.