# All files npm-cli.js

90.24% Statements 37/41
70% Branches 7/10
100% Functions 2/2
92.5% Lines 37/40

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
    1x
    2x

    2x

    2x

    2x

    2x

    2x

    2x
    2x
    2x
    2x

    2x
    1x

    2x
    2x

    2x
    2x
    2x

    2x
    1x
    1x

    1x
    1x
    1x

    1x
    1x

    1x
    1x

    1x
    1x
    1x
    1x

    1x

    1x
    1x
    1x
    1x

     // Separated out for easier unit testing
    module.exports = (process) => {
      process.title = 'npm'

      const {
        checkForBrokenNode,
        checkForUnsupportedNode
      } = require('../lib/utils/unsupported.js')

      checkForBrokenNode()

      const log = require('npmlog')
      // pause it here so it can unpause when we've loaded the configs
      // and know what loglevel we should be printing.
      log.pause()

      checkForUnsupportedNode()

      const npm = require('../lib/npm.js')
      const { defs: { shorthands, types } } = require('../lib/config/core.js')
      const errorHandler = require('../lib/utils/error-handler.js')
      const nopt = require('nopt')

      // if npm is called as "npmg" or "npm_g", then
      // run in global mode.
      if (process.argv[1][process.argv[1].length - 1] === 'g') {
        process.argv.splice(1, 1, 'npm', '-g')
      }

      console.error('ARGV BEFORE', process.argv)
      log.verbose('cli', process.argv)

      const conf = nopt(types, shorthands, process.argv)
      console.error('ARGV AFTER', process.argv)
      npm.argv = conf.argv.remain

      if (conf.version) {
        console.log(npm.version)
        return errorHandler.exit(0)
      }

      Eif (conf.versions) {
        npm.argv = ['version']
        conf.usage = false
      }

      log.info('using', 'npm@%s', npm.version)
      log.info('using', 'node@%s', process.version)

      process.on('uncaughtException', errorHandler)
      process.on('unhandledRejection', errorHandler)

      // now actually fire up npm and run the command.
      // this is how to use npm programmatically:
      conf._exit = true
      const updateNotifier = require('../lib/utils/update-notifier.js')
      npm.load(conf, er => {
        Iif (er) return errorHandler(er)

        updateNotifier(npm)

        const cmd = npm.argv.shift()
        const impl = npm.commands[cmd]
        Eif (impl) {
          impl(npm.argv, errorHandler)
        } else {
          npm.config.set('usage', false)
          npm.argv.unshift(cmd)
          npm.commands.help(npm.argv, errorHandler)
        }
      })
    }