UNPKG

4.59 kBJavaScriptView Raw
1#!/usr/bin/env node
2;(function () { // wrapper in case we're in module_context mode
3 // windows: running "npm blah" in this folder will invoke WSH, not node.
4 /* global WScript */
5 if (typeof WScript !== 'undefined') {
6 WScript.echo(
7 'npm does not work when run\n' +
8 'with the Windows Scripting Host\n\n' +
9 "'cd' to a different directory,\n" +
10 "or type 'npm.cmd <args>',\n" +
11 "or type 'node npm <args>'."
12 )
13 WScript.quit(1)
14 return
15 }
16
17 process.title = 'npm'
18
19 var unsupported = require('../lib/utils/unsupported.js')
20 unsupported.checkForBrokenNode()
21
22 var log = require('npmlog')
23 log.pause() // will be unpaused when config is loaded.
24 log.info('it worked if it ends with', 'ok')
25
26 unsupported.checkForUnsupportedNode()
27
28 var path = require('path')
29 var npm = require('../lib/npm.js')
30 var npmconf = require('../lib/config/core.js')
31 var errorHandler = require('../lib/utils/error-handler.js')
32
33 var configDefs = npmconf.defs
34 var shorthands = configDefs.shorthands
35 var types = configDefs.types
36 var nopt = require('nopt')
37
38 // if npm is called as "npmg" or "npm_g", then
39 // run in global mode.
40 if (path.basename(process.argv[1]).slice(-1) === 'g') {
41 process.argv.splice(1, 1, 'npm', '-g')
42 }
43
44 log.verbose('cli', process.argv)
45
46 var conf = nopt(types, shorthands)
47 npm.argv = conf.argv.remain
48 if (npm.deref(npm.argv[0])) npm.command = npm.argv.shift()
49 else conf.usage = true
50
51 if (conf.version) {
52 console.log(npm.version)
53 return errorHandler.exit(0)
54 }
55
56 if (conf.versions) {
57 npm.command = 'version'
58 conf.usage = false
59 npm.argv = []
60 }
61
62 log.info('using', 'npm@%s', npm.version)
63 log.info('using', 'node@%s', process.version)
64
65 process.on('uncaughtException', errorHandler)
66
67 if (conf.usage && npm.command !== 'help') {
68 npm.argv.unshift(npm.command)
69 npm.command = 'help'
70 }
71
72 var isGlobalNpmUpdate = conf.global && ['install', 'update'].includes(npm.command) && npm.argv.includes('npm')
73
74 // now actually fire up npm and run the command.
75 // this is how to use npm programmatically:
76 conf._exit = true
77 npm.load(conf, function (er) {
78 if (er) return errorHandler(er)
79 if (
80 !isGlobalNpmUpdate &&
81 npm.config.get('update-notifier') &&
82 !unsupported.checkVersion(process.version).unsupported
83 ) {
84 const pkg = require('../package.json')
85 let notifier = require('update-notifier')({pkg})
86 const isCI = require('ci-info').isCI
87 if (
88 notifier.update &&
89 notifier.update.latest !== pkg.version &&
90 !isCI
91 ) {
92 const color = require('ansicolors')
93 const useColor = npm.config.get('color')
94 const useUnicode = npm.config.get('unicode')
95 const old = notifier.update.current
96 const latest = notifier.update.latest
97 let type = notifier.update.type
98 if (useColor) {
99 switch (type) {
100 case 'major':
101 type = color.red(type)
102 break
103 case 'minor':
104 type = color.yellow(type)
105 break
106 case 'patch':
107 type = color.green(type)
108 break
109 }
110 }
111 const changelog = `https://github.com/npm/cli/releases/tag/v${latest}`
112 notifier.notify({
113 message: `New ${type} version of ${pkg.name} available! ${
114 useColor ? color.red(old) : old
115 } ${useUnicode ? '→' : '->'} ${
116 useColor ? color.green(latest) : latest
117 }\n` +
118 `${
119 useColor ? color.yellow('Changelog:') : 'Changelog:'
120 } ${
121 useColor ? color.cyan(changelog) : changelog
122 }\n` +
123 `Run ${
124 useColor
125 ? color.green(`npm install -g ${pkg.name}`)
126 : `npm i -g ${pkg.name}`
127 } to update!`
128 })
129 }
130 }
131 npm.commands[npm.command](npm.argv, function (err) {
132 // https://genius.com/Lin-manuel-miranda-your-obedient-servant-lyrics
133 if (
134 !err &&
135 npm.config.get('ham-it-up') &&
136 !npm.config.get('json') &&
137 !npm.config.get('parseable') &&
138 npm.command !== 'completion'
139 ) {
140 console.error(
141 `\n ${
142 npm.config.get('unicode') ? '🎵 ' : ''
143 } I Have the Honour to Be Your Obedient Servant,${
144 npm.config.get('unicode') ? '🎵 ' : ''
145 } ~ npm ${
146 npm.config.get('unicode') ? '📜🖋 ' : ''
147 }\n`
148 )
149 }
150 errorHandler.apply(this, arguments)
151 })
152 })
153})()