11// Separated out for easier unit testing
2- module . exports = ( process ) => {
2+ module . exports = async ( process ) => {
33 // set it here so that regardless of what happens later, we don't
44 // leak any private CLI configs to other programs
55 process . title = 'npm'
@@ -19,8 +19,8 @@ module.exports = (process) => {
1919 checkForUnsupportedNode ( )
2020
2121 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 )
2424
2525 // if npm is called as "npmg" or "npm_g", then
2626 // run in global mode.
@@ -32,20 +32,18 @@ module.exports = (process) => {
3232 log . info ( 'using' , 'npm@%s' , npm . version )
3333 log . info ( 'using' , 'node@%s' , process . version )
3434
35- process . on ( 'uncaughtException' , errorHandler )
36- process . on ( 'unhandledRejection' , errorHandler )
35+ process . on ( 'uncaughtException' , exitHandler )
36+ process . on ( 'unhandledRejection' , exitHandler )
3737
38- // now actually fire up npm and run the command.
39- // this is how to use npm programmatically:
4038 const updateNotifier = require ( '../lib/utils/update-notifier.js' )
41- npm . load ( async er => {
42- if ( er )
43- return errorHandler ( er )
4439
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 ( )
4644 if ( npm . config . get ( 'version' , 'cli' ) ) {
4745 npm . output ( npm . version )
48- return errorHandler . exit ( 0 )
46+ return exitHandler ( )
4947 }
5048
5149 // npm --versions=cli
@@ -57,22 +55,23 @@ module.exports = (process) => {
5755 updateNotifier ( npm )
5856
5957 const cmd = npm . argv . shift ( )
58+ if ( ! cmd ) {
59+ npm . output ( npm . usage )
60+ process . exitCode = 1
61+ return exitHandler ( )
62+ }
63+
6064 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 ( )
7671 }
77- } )
72+
73+ impl ( npm . argv , exitHandler )
74+ } catch ( err ) {
75+ return exitHandler ( err )
76+ }
7877}
0 commit comments