"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/npm/lib/help.js" (7 Feb 2017, 6335 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 
    2 module.exports = help
    3 
    4 help.completion = function (opts, cb) {
    5   if (opts.conf.argv.remain.length > 2) return cb(null, [])
    6   getSections(cb)
    7 }
    8 
    9 var path = require('path')
   10 var spawn = require('./utils/spawn')
   11 var npm = require('./npm.js')
   12 var log = require('npmlog')
   13 var opener = require('opener')
   14 var glob = require('glob')
   15 var cmdList = require('./config/cmd-list').cmdList
   16 var shorthands = require('./config/cmd-list').shorthands
   17 var commands = cmdList.concat(Object.keys(shorthands))
   18 var output = require('./utils/output.js')
   19 
   20 function help (args, cb) {
   21   var argv = npm.config.get('argv').cooked
   22 
   23   var argnum = 0
   24   if (args.length === 2 && ~~args[0]) {
   25     argnum = ~~args.shift()
   26   }
   27 
   28   // npm help foo bar baz: search topics
   29   if (args.length > 1 && args[0]) {
   30     return npm.commands['help-search'](args, argnum, cb)
   31   }
   32 
   33   var section = npm.deref(args[0]) || args[0]
   34 
   35   // npm help <noargs>:  show basic usage
   36   if (!section) {
   37     var valid = argv[0] === 'help' ? 0 : 1
   38     return npmUsage(valid, cb)
   39   }
   40 
   41   // npm <cmd> -h: show command usage
   42   if (npm.config.get('usage') &&
   43       npm.commands[section] &&
   44       npm.commands[section].usage) {
   45     npm.config.set('loglevel', 'silent')
   46     log.level = 'silent'
   47     output(npm.commands[section].usage)
   48     return cb()
   49   }
   50 
   51   // npm apihelp <section>: Prefer section 3 over section 1
   52   var apihelp = argv.length && argv[0].indexOf('api') !== -1
   53   var pref = apihelp ? [3, 1, 5, 7] : [1, 3, 5, 7]
   54   if (argnum) {
   55     pref = [ argnum ].concat(pref.filter(function (n) {
   56       return n !== argnum
   57     }))
   58   }
   59 
   60   // npm help <section>: Try to find the path
   61   var manroot = path.resolve(__dirname, '..', 'man')
   62 
   63   // legacy
   64   if (section === 'global') section = 'folders'
   65   else if (section === 'json') section = 'package.json'
   66 
   67   // find either /section.n or /npm-section.n
   68   // The glob is used in the glob.  The regexp is used much
   69   // further down.  Globs and regexps are different
   70   var compextglob = '.+(gz|bz2|lzma|[FYzZ]|xz)'
   71   var compextre = '\\.(gz|bz2|lzma|[FYzZ]|xz)$'
   72   var f = '+(npm-' + section + '|' + section + ').[0-9]?(' + compextglob + ')'
   73   return glob(manroot + '/*/' + f, function (er, mans) {
   74     if (er) return cb(er)
   75 
   76     if (!mans.length) return npm.commands['help-search'](args, cb)
   77 
   78     mans = mans.map(function (man) {
   79       var ext = path.extname(man)
   80       if (man.match(new RegExp(compextre))) man = path.basename(man, ext)
   81 
   82       return man
   83     })
   84 
   85     viewMan(pickMan(mans, pref), cb)
   86   })
   87 }
   88 
   89 function pickMan (mans, pref_) {
   90   var nre = /([0-9]+)$/
   91   var pref = {}
   92   pref_.forEach(function (sect, i) {
   93     pref[sect] = i
   94   })
   95   mans = mans.sort(function (a, b) {
   96     var an = a.match(nre)[1]
   97     var bn = b.match(nre)[1]
   98     return an === bn ? (a > b ? -1 : 1)
   99          : pref[an] < pref[bn] ? -1
  100          : 1
  101   })
  102   return mans[0]
  103 }
  104 
  105 function viewMan (man, cb) {
  106   var nre = /([0-9]+)$/
  107   var num = man.match(nre)[1]
  108   var section = path.basename(man, '.' + num)
  109 
  110   // at this point, we know that the specified man page exists
  111   var manpath = path.join(__dirname, '..', 'man')
  112   var env = {}
  113   Object.keys(process.env).forEach(function (i) {
  114     env[i] = process.env[i]
  115   })
  116   env.MANPATH = manpath
  117   var viewer = npm.config.get('viewer')
  118 
  119   var conf
  120   switch (viewer) {
  121     case 'woman':
  122       var a = ['-e', '(woman-find-file \'' + man + '\')']
  123       conf = { env: env, stdio: 'inherit' }
  124       var woman = spawn('emacsclient', a, conf)
  125       woman.on('close', cb)
  126       break
  127 
  128     case 'browser':
  129       opener(htmlMan(man), { command: npm.config.get('browser') }, cb)
  130       break
  131 
  132     default:
  133       conf = { env: env, stdio: 'inherit' }
  134       var manProcess = spawn('man', [num, section], conf)
  135       manProcess.on('close', cb)
  136       break
  137   }
  138 }
  139 
  140 function htmlMan (man) {
  141   var sect = +man.match(/([0-9]+)$/)[1]
  142   var f = path.basename(man).replace(/([0-9]+)$/, 'html')
  143   switch (sect) {
  144     case 1:
  145       sect = 'cli'
  146       break
  147     case 3:
  148       sect = 'api'
  149       break
  150     case 5:
  151       sect = 'files'
  152       break
  153     case 7:
  154       sect = 'misc'
  155       break
  156     default:
  157       throw new Error('invalid man section: ' + sect)
  158   }
  159   return path.resolve(__dirname, '..', 'html', 'doc', sect, f)
  160 }
  161 
  162 function npmUsage (valid, cb) {
  163   npm.config.set('loglevel', 'silent')
  164   log.level = 'silent'
  165   output([
  166     '\nUsage: npm <command>',
  167     '',
  168     'where <command> is one of:',
  169     npm.config.get('long') ? usages()
  170         : '    ' + wrap(commands),
  171     '',
  172     'npm <cmd> -h     quick help on <cmd>',
  173     'npm -l           display full usage info',
  174     'npm help <term>  search for help on <term>',
  175     'npm help npm     involved overview',
  176     '',
  177     'Specify configs in the ini-formatted file:',
  178     '    ' + npm.config.get('userconfig'),
  179     'or on the command line via: npm <command> --key value',
  180     'Config info can be viewed via: npm help config',
  181     '',
  182     'npm@' + npm.version + ' ' + path.dirname(__dirname)
  183   ].join('\n'))
  184   cb(valid)
  185 }
  186 
  187 function usages () {
  188   // return a string of <cmd>: <usage>
  189   var maxLen = 0
  190   return Object.keys(npm.commands).filter(function (c) {
  191     return c === npm.deref(c)
  192   }).reduce(function (set, c) {
  193     set.push([c, npm.commands[c].usage || ''])
  194     maxLen = Math.max(maxLen, c.length)
  195     return set
  196   }, []).map(function (item) {
  197     var c = item[0]
  198     var usage = item[1]
  199     return '\n    ' +
  200       c + (new Array(maxLen - c.length + 2).join(' ')) +
  201       (usage.split('\n').join('\n' + (new Array(maxLen + 6).join(' '))))
  202   }).join('\n')
  203 }
  204 
  205 function wrap (arr) {
  206   var out = ['']
  207   var l = 0
  208   var line
  209 
  210   line = process.stdout.columns
  211   if (!line) {
  212     line = 60
  213   } else {
  214     line = Math.min(60, Math.max(line - 16, 24))
  215   }
  216 
  217   arr.sort(function (a, b) { return a < b ? -1 : 1 })
  218     .forEach(function (c) {
  219       if (out[l].length + c.length + 2 < line) {
  220         out[l] += ', ' + c
  221       } else {
  222         out[l++] += ','
  223         out[l] = c
  224       }
  225     })
  226   return out.join('\n    ').substr(2)
  227 }
  228 
  229 function getSections (cb) {
  230   var g = path.resolve(__dirname, '../man/man[0-9]/*.[0-9]')
  231   glob(g, function (er, files) {
  232     if (er) return cb(er)
  233 
  234     cb(null, Object.keys(files.reduce(function (acc, file) {
  235       file = path.basename(file).replace(/\.[0-9]+$/, '')
  236       file = file.replace(/^npm-/, '')
  237       acc[file] = true
  238       return acc
  239     }, { help: true })))
  240   })
  241 }