UNPKG

2.26 kBJavaScriptView Raw
1#!/usr/bin/env node
2;(function () { // wrapper in case we're in module_context mode
3
4// windows: running "npm blah" in this folder will invoke WSH, not node.
5if (typeof WScript !== "undefined") {
6 WScript.echo("npm does not work when run\n"
7 +"with the Windows Scripting Host\n\n"
8 +"'cd' to a different directory,\n"
9 +"or type 'npm.cmd <args>',\n"
10 +"or type 'node npm <args>'.")
11 WScript.quit(1)
12 return
13}
14
15
16var log = require("npmlog")
17log.pause() // will be unpaused when config is loaded.
18log.info("it worked if it ends with", "ok")
19
20var fs = require("graceful-fs")
21 , path = require("path")
22 , npm = require("../lib/npm.js")
23 , npmconf = require("npmconf")
24 , errorHandler = require("../lib/utils/error-handler.js")
25
26 , configDefs = npmconf.defs
27 , shorthands = configDefs.shorthands
28 , types = configDefs.types
29 , nopt = require("nopt")
30
31process.title = npm.name
32
33// if npm is called as "npmg" or "npm_g", then
34// run in global mode.
35if (path.basename(process.argv[1]).slice(-1) === "g") {
36 process.argv.splice(1, 1, "npm", "-g")
37}
38
39log.verbose("cli", process.argv)
40
41var conf = nopt(types, shorthands)
42npm.argv = conf.argv.remain
43if (npm.deref(npm.argv[0])) npm.command = npm.argv.shift()
44else conf.usage = true
45
46
47if (conf.version) {
48 console.log(npm.version)
49 return
50}
51
52if (conf.versions) {
53 npm.command = "version"
54 conf.usage = false
55 npm.argv = []
56}
57
58log.info("using", npm.name + "@%s", npm.version)
59log.info("using", "node@%s", process.version)
60
61// make sure that this version of node works with this version of npm.
62var semver = require("semver")
63 , nodeVer = process.version
64 , reqVer = npm.nodeVersionRequired
65if (reqVer && !semver.satisfies(nodeVer, reqVer)) {
66 return errorHandler(new Error(
67 "npm doesn't work with node " + nodeVer
68 + "\nRequired: node@" + reqVer), true)
69}
70
71process.on("uncaughtException", errorHandler)
72
73if (conf.usage && npm.command !== "help") {
74 npm.argv.unshift(npm.command)
75 npm.command = "help"
76}
77
78// now actually fire up npm and run the command.
79// this is how to use npm programmatically:
80conf._exit = true
81npm.load(conf, function (er) {
82 if (er) return errorHandler(er)
83 npm.commands[npm.command](npm.argv, errorHandler)
84})
85
86})()