All files help.js

100% Statements 107/107
100% Branches 47/47
100% Functions 14/14
100% Lines 107/107

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185  1x   1x 3x 1x 2x     1x 1x 1x 1x 1x 1x 1x 1x   1x   1x     14x   14x 14x 4x     14x 1x   13x     13x     13x 1x 1x       12x     1x 1x 1x 1x     11x 11x 4x 12x         11x     11x 1x 10x 1x         11x 11x 11x 11x 11x 1x   10x 1x   9x 17x 17x 1x   17x     9x         9x 9x 9x 28x   9x 12x 12x 12x       9x       9x 9x 9x     9x 9x 9x 630x   9x 9x     9x   1x 1x 1x 1x 1x     4x 4x   1x   3x 3x     4x 4x 4x 4x         4x 4x 4x   1x 1x   1x 1x   1x 1x   1x   3x       2x 2x 2x 1x   1x 3x 3x 3x 3x        
 
module.exports = help
 
help.completion = function (opts, cb) {
  if (opts.conf.argv.remain.length > 2)
    return cb(null, [])
  getSections(cb)
}
 
const npmUsage = require('./utils/npm-usage.js')
var path = require('path')
var spawn = require('./utils/spawn')
var npm = require('./npm.js')
var log = require('npmlog')
var openUrl = require('./utils/open-url')
var glob = require('glob')
var output = require('./utils/output.js')
 
const usage = require('./utils/usage.js')
 
help.usage = usage('help', 'npm help <term> [<terms..>]')
 
function help (args, cb) {
  var argv = npm.config.parsedArgv.cooked
 
  var argnum = 0
  if (args.length === 2 && ~~args[0])
    argnum = ~~args.shift()
 
  // npm help foo bar baz: search topics
  if (args.length > 1 && args[0])
    return npm.commands['help-search'](args, cb)
 
  const affordances = {
    'find-dupes': 'dedupe',
  }
  var section = affordances[args[0]] || npm.deref(args[0]) || args[0]
 
  // npm help <noargs>:  show basic usage
  if (!section) {
    npmUsage(argv[0] === 'help')
    return cb()
  }
 
  // npm <command> -h: show command usage
  if (npm.config.get('usage') &&
      npm.commands[section] &&
      npm.commands[section].usage) {
    npm.config.set('loglevel', 'silent')
    log.level = 'silent'
    output(npm.commands[section].usage)
    return cb()
  }
 
  var pref = [1, 5, 7]
  if (argnum) {
    pref = [argnum].concat(pref.filter(function (n) {
      return n !== argnum
    }))
  }
 
  // npm help <section>: Try to find the path
  var manroot = path.resolve(__dirname, '..', 'man')
 
  // legacy
  if (section === 'global')
    section = 'folders'
  else if (section.match(/.*json/))
    section = section.replace('.json', '-json')
 
  // find either /section.n or /npm-section.n
  // The glob is used in the glob.  The regexp is used much
  // further down.  Globs and regexps are different
  var compextglob = '.+(gz|bz2|lzma|[FYzZ]|xz)'
  var compextre = '\\.(gz|bz2|lzma|[FYzZ]|xz)$'
  var f = '+(npm-' + section + '|' + section + ').[0-9]?(' + compextglob + ')'
  return glob(manroot + '/*/' + f, function (er, mans) {
    if (er)
      return cb(er)
 
    if (!mans.length)
      return npm.commands['help-search'](args, cb)
 
    mans = mans.map(function (man) {
      var ext = path.extname(man)
      if (man.match(new RegExp(compextre)))
        man = path.basename(man, ext)
 
      return man
    })
 
    viewMan(pickMan(mans, pref), cb)
  })
}
 
function pickMan (mans, pref_) {
  var nre = /([0-9]+)$/
  var pref = {}
  pref_.forEach(function (sect, i) {
    pref[sect] = i
  })
  mans = mans.sort(function (a, b) {
    var an = a.match(nre)[1]
    var bn = b.match(nre)[1]
    return an === bn ? (a > b ? -1 : 1)
      : pref[an] < pref[bn] ? -1
      : 1
  })
  return mans[0]
}
 
function viewMan (man, cb) {
  var nre = /([0-9]+)$/
  var num = man.match(nre)[1]
  var section = path.basename(man, '.' + num)
 
  // at this point, we know that the specified man page exists
  var manpath = path.join(__dirname, '..', 'man')
  var env = {}
  Object.keys(process.env).forEach(function (i) {
    env[i] = process.env[i]
  })
  env.MANPATH = manpath
  var viewer = npm.config.get('viewer')
 
  var conf
  switch (viewer) {
    case 'woman':
      var a = ['-e', '(woman-find-file \'' + man + '\')']
      conf = { env: env, stdio: 'inherit' }
      var woman = spawn('emacsclient', a, conf)
      woman.on('close', cb)
      break
 
    case 'browser':
      try {
        var url = htmlMan(man)
      } catch (err) {
        return cb(err)
      }
      openUrl(url, 'help available at the following URL', cb)
      break
 
    default:
      conf = { env: env, stdio: 'inherit' }
      var manProcess = spawn('man', [num, section], conf)
      manProcess.on('close', cb)
      break
  }
}
 
function htmlMan (man) {
  var sect = +man.match(/([0-9]+)$/)[1]
  var f = path.basename(man).replace(/[.]([0-9]+)$/, '')
  switch (sect) {
    case 1:
      sect = 'commands'
      break
    case 5:
      sect = 'configuring-npm'
      break
    case 7:
      sect = 'using-npm'
      break
    default:
      throw new Error('invalid man section: ' + sect)
  }
  return 'file://' + path.resolve(__dirname, '..', 'docs', 'output', sect, f + '.html')
}
 
function getSections (cb) {
  var g = path.resolve(__dirname, '../man/man[0-9]/*.[0-9]')
  glob(g, function (er, files) {
    if (er)
      return cb(er)
 
    cb(null, Object.keys(files.reduce(function (acc, file) {
      file = path.basename(file).replace(/\.[0-9]+$/, '')
      file = file.replace(/^npm-/, '')
      acc[file] = true
      return acc
    }, { help: true })))
  })
}