Welcome to mirror list, hosted at ThFree Co, Russian Federation.

npm-cli.js « bin - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1380e320d185e86965c5158eee46122b485886d9 (plain)
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
#!/usr/bin/env node

process.title = 'npm'

const {
  checkForBrokenNode,
  checkForUnsupportedNode,
  checkVersion
} = 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')
}

log.verbose('cli', process.argv)

const conf = nopt(types, shorthands)
npm.argv = conf.argv.remain

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

if (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)

const isGlobalNpmUpdate = npm => {
  return npm.config.get('global') &&
    ['install', 'update'].includes(npm.command) &&
    npm.argv.includes('npm')
}

// now actually fire up npm and run the command.
// this is how to use npm programmatically:
conf._exit = true
npm.load(conf, function (er) {
  if (er) return errorHandler(er)
  if (
    !isGlobalNpmUpdate(npm) &&
    npm.config.get('update-notifier') &&
    !checkVersion(process.version).unsupported
  ) {
    // XXX move update notifier stuff into separate module
    const pkg = require('../package.json')
    let notifier = require('update-notifier')({pkg})
    const isCI = require('ci-info').isCI
    if (
      notifier.update &&
      notifier.update.latest !== pkg.version &&
      !isCI
    ) {
      const color = require('ansicolors')
      const useColor = npm.color
      const useUnicode = npm.config.get('unicode')
      const old = notifier.update.current
      const latest = notifier.update.latest
      let type = notifier.update.type
      if (useColor) {
        switch (type) {
          case 'major':
            type = color.red(type)
            break
          case 'minor':
            type = color.yellow(type)
            break
          case 'patch':
            type = color.green(type)
            break
        }
      }
      const changelog = `https://github.com/npm/cli/releases/tag/v${latest}`
      notifier.notify({
        message: `New ${type} version of ${pkg.name} available! ${
          useColor ? color.red(old) : old
        } ${useUnicode ? '→' : '->'} ${
          useColor ? color.green(latest) : latest
        }\n` +
        `${
          useColor ? color.yellow('Changelog:') : 'Changelog:'
        } ${
          useColor ? color.cyan(changelog) : changelog
        }\n` +
        `Run ${
          useColor
            ? color.green(`npm install -g ${pkg.name}`)
            : `npm i -g ${pkg.name}`
        } to update!`
      })
    }
  }

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