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