diff options
author | Gar <gar+gh@danger.computer> | 2021-06-14 20:39:53 +0300 |
---|---|---|
committer | Gar <gar+gh@danger.computer> | 2021-06-23 19:18:03 +0300 |
commit | 54eae3063eeb197225ee930525a1316e34ecf34c (patch) | |
tree | e7c1e7d747fc7d4ef8cc605600b517752b55f6e4 /lib/cli.js | |
parent | 78da60ffefcfd457a4432ce1492ee7b53d854450 (diff) |
chore(errorHandler): rename to exit handler
Cleaned the code up too
PR-URL: https://github.com/npm/cli/pull/3416
Credit: @wraithgar
Close: #3416
Reviewed-by: @nlf
Diffstat (limited to 'lib/cli.js')
-rw-r--r-- | lib/cli.js | 45 |
1 files changed, 23 insertions, 22 deletions
diff --git a/lib/cli.js b/lib/cli.js index fbceb459d..106dd629d 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -19,8 +19,8 @@ module.exports = (process) => { checkForUnsupportedNode() const npm = require('../lib/npm.js') - const errorHandler = require('../lib/utils/error-handler.js') - errorHandler.setNpm(npm) + const exitHandler = require('../lib/utils/exit-handler.js') + exitHandler.setNpm(npm) // if npm is called as "npmg" or "npm_g", then // run in global mode. @@ -32,20 +32,22 @@ module.exports = (process) => { log.info('using', 'npm@%s', npm.version) log.info('using', 'node@%s', process.version) - process.on('uncaughtException', errorHandler) - process.on('unhandledRejection', errorHandler) + process.on('uncaughtException', exitHandler) + process.on('unhandledRejection', exitHandler) + + const updateNotifier = require('../lib/utils/update-notifier.js') // now actually fire up npm and run the command. // this is how to use npm programmatically: - const updateNotifier = require('../lib/utils/update-notifier.js') npm.load(async er => { + // Any exceptions here will be picked up by the uncaughtException handler if (er) - return errorHandler(er) + return exitHandler(er) // npm --version=cli if (npm.config.get('version', 'cli')) { npm.output(npm.version) - return errorHandler.exit(0) + return exitHandler() } // npm --versions=cli @@ -57,22 +59,21 @@ module.exports = (process) => { updateNotifier(npm) const cmd = npm.argv.shift() + if (!cmd) { + npm.output(npm.usage) + process.exitCode = 1 + return exitHandler() + } + const impl = npm.commands[cmd] - if (impl) - impl(npm.argv, errorHandler) - else { - try { - if (cmd) { - const didYouMean = require('./utils/did-you-mean.js') - const suggestions = await didYouMean(npm, npm.localPrefix, cmd) - npm.output(`Unknown command: "${cmd}"${suggestions}\n\nTo see a list of supported npm commands, run:\n npm help`) - } else - npm.output(npm.usage) - process.exitCode = 1 - return errorHandler() - } catch (err) { - errorHandler(err) - } + if (!impl) { + const didYouMean = require('./utils/did-you-mean.js') + const suggestions = await didYouMean(npm, npm.localPrefix, cmd) + npm.output(`Unknown command: "${cmd}"${suggestions}\n\nTo see a list of supported npm commands, run:\n npm help`) + process.exitCode = 1 + return exitHandler() } + + impl(npm.argv, exitHandler) }) } |