npmtest-auto-install (v0.0.1)

Code coverage report for node-npmtest-auto-install/auto-install/node_modules/yargs/lib/completion.js

Statements: 18.75% (9 / 48)      Branches: 0% (0 / 24)      Functions: 8.33% (1 / 12)      Lines: 20% (9 / 45)      Ignored: none     

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