1
1
// Separated out for easier unit testing
2
- module . exports = ( process ) => {
2
+ module . exports = async ( process ) => {
3
3
// set it here so that regardless of what happens later, we don't
4
4
// leak any private CLI configs to other programs
5
5
process . title = 'npm'
@@ -19,8 +19,8 @@ module.exports = (process) => {
19
19
checkForUnsupportedNode ( )
20
20
21
21
const npm = require ( '../lib/npm.js' )
22
- const errorHandler = require ( '../lib/utils/error -handler.js' )
23
- errorHandler . setNpm ( npm )
22
+ const exitHandler = require ( '../lib/utils/exit -handler.js' )
23
+ exitHandler . setNpm ( npm )
24
24
25
25
// if npm is called as "npmg" or "npm_g", then
26
26
// run in global mode.
@@ -32,20 +32,18 @@ module.exports = (process) => {
32
32
log . info ( 'using' , 'npm@%s' , npm . version )
33
33
log . info ( 'using' , 'node@%s' , process . version )
34
34
35
- process . on ( 'uncaughtException' , errorHandler )
36
- process . on ( 'unhandledRejection' , errorHandler )
35
+ process . on ( 'uncaughtException' , exitHandler )
36
+ process . on ( 'unhandledRejection' , exitHandler )
37
37
38
- // now actually fire up npm and run the command.
39
- // this is how to use npm programmatically:
40
38
const updateNotifier = require ( '../lib/utils/update-notifier.js' )
41
- npm . load ( async er => {
42
- if ( er )
43
- return errorHandler ( er )
44
39
45
- // npm --version=cli
40
+ // now actually fire up npm and run the command.
41
+ // this is how to use npm programmatically:
42
+ try {
43
+ await npm . load ( )
46
44
if ( npm . config . get ( 'version' , 'cli' ) ) {
47
45
npm . output ( npm . version )
48
- return errorHandler . exit ( 0 )
46
+ return exitHandler ( )
49
47
}
50
48
51
49
// npm --versions=cli
@@ -57,22 +55,23 @@ module.exports = (process) => {
57
55
updateNotifier ( npm )
58
56
59
57
const cmd = npm . argv . shift ( )
58
+ if ( ! cmd ) {
59
+ npm . output ( npm . usage )
60
+ process . exitCode = 1
61
+ return exitHandler ( )
62
+ }
63
+
60
64
const impl = npm . commands [ cmd ]
61
- if ( impl )
62
- impl ( npm . argv , errorHandler )
63
- else {
64
- try {
65
- if ( cmd ) {
66
- const didYouMean = require ( './utils/did-you-mean.js' )
67
- const suggestions = await didYouMean ( npm , npm . localPrefix , cmd )
68
- npm . output ( `Unknown command: "${ cmd } "${ suggestions } \n\nTo see a list of supported npm commands, run:\n npm help` )
69
- } else
70
- npm . output ( npm . usage )
71
- process . exitCode = 1
72
- return errorHandler ( )
73
- } catch ( err ) {
74
- errorHandler ( err )
75
- }
65
+ if ( ! impl ) {
66
+ const didYouMean = require ( './utils/did-you-mean.js' )
67
+ const suggestions = await didYouMean ( npm , npm . localPrefix , cmd )
68
+ npm . output ( `Unknown command: "${ cmd } "${ suggestions } \n\nTo see a list of supported npm commands, run:\n npm help` )
69
+ process . exitCode = 1
70
+ return exitHandler ( )
76
71
}
77
- } )
72
+
73
+ impl ( npm . argv , exitHandler )
74
+ } catch ( err ) {
75
+ return exitHandler ( err )
76
+ }
78
77
}
0 commit comments