|
| 1 | +## 源码解析 |
| 2 | +```js |
| 3 | +#!/usr/bin/env node |
| 4 | +;(function () { // wrapper in case we're in module_context mode |
| 5 | + // windows: running "npm blah" in this folder will invoke WSH, not node. |
| 6 | + /*global WScript*/ |
| 7 | + // WScript 是 windows 下的一个运行脚本的工具 |
| 8 | + if (typeof WScript !== 'undefined') { |
| 9 | + WScript.echo( |
| 10 | + 'npm does not work when run\n' + |
| 11 | + 'with the Windows Scripting Host\n\n' + |
| 12 | + "'cd' to a different directory,\n" + |
| 13 | + "or type 'npm.cmd <args>',\n" + |
| 14 | + "or type 'node npm <args>'." |
| 15 | + ) |
| 16 | + WScript.quit(1) |
| 17 | + return |
| 18 | + } |
| 19 | + |
| 20 | + process.title = 'npm' |
| 21 | + |
| 22 | + var unsupported = require('../lib/utils/unsupported.js') |
| 23 | + unsupported.checkForBrokenNode() |
| 24 | + |
| 25 | + var log = require('npmlog') |
| 26 | + log.pause() // will be unpaused when config is loaded. |
| 27 | + log.info('it worked if it ends with', 'ok') |
| 28 | + |
| 29 | + unsupported.checkForUnsupportedNode() |
| 30 | + |
| 31 | + if (!unsupported.checkVersion(process.version).unsupported) { |
| 32 | + var updater = require('update-notifier') |
| 33 | + var pkg = require('../package.json') |
| 34 | + updater({pkg: pkg}).notify({defer: true}) |
| 35 | + } |
| 36 | + |
| 37 | + var path = require('path') |
| 38 | + var npm = require('../lib/npm.js') |
| 39 | + var npmconf = require('../lib/config/core.js') |
| 40 | + var errorHandler = require('../lib/utils/error-handler.js') |
| 41 | + var output = require('../lib/utils/output.js') |
| 42 | + |
| 43 | + var configDefs = npmconf.defs |
| 44 | + var shorthands = configDefs.shorthands |
| 45 | + var types = configDefs.types |
| 46 | + var nopt = require('nopt') |
| 47 | + |
| 48 | + // if npm is called as "npmg" or "npm_g", then |
| 49 | + // run in global mode. |
| 50 | + if (path.basename(process.argv[1]).slice(-1) === 'g') { |
| 51 | + process.argv.splice(1, 1, 'npm', '-g') |
| 52 | + } |
| 53 | + |
| 54 | + log.verbose('cli', process.argv) |
| 55 | + |
| 56 | + var conf = nopt(types, shorthands) |
| 57 | + npm.argv = conf.argv.remain |
| 58 | + if (npm.deref(npm.argv[0])) npm.command = npm.argv.shift() |
| 59 | + else conf.usage = true |
| 60 | + |
| 61 | + if (conf.version) { |
| 62 | + console.log(npm.version) |
| 63 | + return errorHandler.exit(0) |
| 64 | + } |
| 65 | + |
| 66 | + if (conf.versions) { |
| 67 | + npm.command = 'version' |
| 68 | + conf.usage = false |
| 69 | + npm.argv = [] |
| 70 | + } |
| 71 | + |
| 72 | + log.info('using', 'npm@%s', npm.version) |
| 73 | + log.info('using', 'node@%s', process.version) |
| 74 | + |
| 75 | + process.on('uncaughtException', errorHandler) |
| 76 | + |
| 77 | + if (conf.usage && npm.command !== 'help') { |
| 78 | + npm.argv.unshift(npm.command) |
| 79 | + npm.command = 'help' |
| 80 | + } |
| 81 | + |
| 82 | + // now actually fire up npm and run the command. |
| 83 | + // this is how to use npm programmatically: |
| 84 | + conf._exit = true |
| 85 | + npm.load(conf, function (er) { |
| 86 | + if (er) return errorHandler(er) |
| 87 | + npm.commands[npm.command](npm.argv, function (err) { |
| 88 | + // https://www.youtube.com/watch?v=7nfPu8qTiQU |
| 89 | + if (!err && npm.config.get('ham-it-up') && !npm.config.get('json') && !npm.config.get('parseable') && npm.command !== 'completion') { |
| 90 | + output('\n 🎵 I Have the Honour to Be Your Obedient Servant,🎵 ~ npm 📜🖋\n') |
| 91 | + } |
| 92 | + errorHandler.apply(this, arguments) |
| 93 | + }) |
| 94 | + }) |
| 95 | +})() |
| 96 | +``` |
0 commit comments