UNPKG

4.71 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 npm = require('../lib/npm.js')
29 var npmconf = require('../lib/config/core.js')
30 var errorHandler = require('../lib/utils/error-handler.js')
31 var replaceInfo = require('../lib/utils/replace-info.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 (process.argv[1][process.argv[1].length - 1] === 'g') {
41 process.argv.splice(1, 1, 'npm', '-g')
42 }
43
44 var args = replaceInfo(process.argv)
45 log.verbose('cli', args)
46
47 var conf = nopt(types, shorthands)
48 npm.argv = conf.argv.remain
49 if (npm.deref(npm.argv[0])) npm.command = npm.argv.shift()
50 else conf.usage = true
51
52 if (conf.version) {
53 console.log(npm.version)
54 return errorHandler.exit(0)
55 }
56
57 if (conf.versions) {
58 npm.command = 'version'
59 conf.usage = false
60 npm.argv = []
61 }
62
63 log.info('using', 'npm@%s', npm.version)
64 log.info('using', 'node@%s', process.version)
65
66 process.on('uncaughtException', errorHandler)
67 process.on('unhandledRejection', errorHandler)
68
69 if (conf.usage && npm.command !== 'help') {
70 npm.argv.unshift(npm.command)
71 npm.command = 'help'
72 }
73
74 var isGlobalNpmUpdate = conf.global && ['install', 'update'].includes(npm.command) && npm.argv.includes('npm')
75
76 // now actually fire up npm and run the command.
77 // this is how to use npm programmatically:
78 conf._exit = true
79 npm.load(conf, function (er) {
80 if (er) return errorHandler(er)
81 if (
82 !isGlobalNpmUpdate &&
83 npm.config.get('update-notifier') &&
84 !unsupported.checkVersion(process.version).unsupported
85 ) {
86 const pkg = require('../package.json')
87 let notifier = require('update-notifier')({pkg})
88 const isCI = require('ci-info').isCI
89 if (
90 notifier.update &&
91 notifier.update.latest !== pkg.version &&
92 !isCI
93 ) {
94 const color = require('ansicolors')
95 const useColor = npm.config.get('color')
96 const useUnicode = npm.config.get('unicode')
97 const old = notifier.update.current
98 const latest = notifier.update.latest
99 let type = notifier.update.type
100 if (useColor) {
101 switch (type) {
102 case 'major':
103 type = color.red(type)
104 break
105 case 'minor':
106 type = color.yellow(type)
107 break
108 case 'patch':
109 type = color.green(type)
110 break
111 }
112 }
113 const changelog = `https://github.com/npm/cli/releases/tag/v${latest}`
114 notifier.notify({
115 message: `New ${type} version of ${pkg.name} available! ${
116 useColor ? color.red(old) : old
117 } ${useUnicode ? '→' : '->'} ${
118 useColor ? color.green(latest) : latest
119 }\n` +
120 `${
121 useColor ? color.yellow('Changelog:') : 'Changelog:'
122 } ${
123 useColor ? color.cyan(changelog) : changelog
124 }\n` +
125 `Run ${
126 useColor
127 ? color.green(`npm install -g ${pkg.name}`)
128 : `npm i -g ${pkg.name}`
129 } to update!`
130 })
131 }
132 }
133 npm.commands[npm.command](npm.argv, function (err) {
134 // https://genius.com/Lin-manuel-miranda-your-obedient-servant-lyrics
135 if (
136 !err &&
137 npm.config.get('ham-it-up') &&
138 !npm.config.get('json') &&
139 !npm.config.get('parseable') &&
140 npm.command !== 'completion'
141 ) {
142 console.error(
143 `\n ${
144 npm.config.get('unicode') ? '🎵 ' : ''
145 } I Have the Honour to Be Your Obedient Servant,${
146 npm.config.get('unicode') ? '🎵 ' : ''
147 } ~ npm ${
148 npm.config.get('unicode') ? '📜🖋 ' : ''
149 }\n`
150 )
151 }
152 errorHandler.apply(this, arguments)
153 })
154 })
155})()