-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·107 lines (96 loc) · 2.8 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env node
;(function () { // wrapper in case we're in module_context mode
var log = require("./lib/utils/log")
log.waitForConfig()
log.info("ok", "it worked if it ends with")
var fs = require("./lib/utils/graceful-fs")
, path = require("path")
, sys = require("./lib/utils/sys")
, npm = require("./npm")
, ini = require("./lib/utils/ini")
, rm = require("./lib/utils/rm-rf")
, errorHandler = require("./lib/utils/error-handler")
// supported commands.
, argv = process.argv.slice(2)
, arg = ""
, conf = {}
, key
, arglist = []
, command
, flagsDone
log.verbose(argv, "cli")
while (arg = argv.shift()) {
// FIXME: This is a mess right here.
// FIXME: Add a "config.flags" somewhere for configs that don't take values
// FIXME: Add config aliases for the shorthands and such.
if (!key && (arg.match(/^-+[h?]$/i) || arg.match(/^-+help$/i))) {
arg = "--usage"
}
if (!key && arg.match(/^-d+$/i)) {
// -d --loglevel info
// -dd --loglevel verbose
// -ddd --loglevel silly
key = "loglevel"
switch (arg.length) {
case 2: arg = "info"
break
case 3: arg = "verbose"
break
default: arg = "silly"
}
}
if (!key && (arg.match(/^-+s$/) || arg.match(/^--silent$/))) {
key = "loglevel"
arg = "silent"
} else if (!key && arg.match(/^--verbose$/)) {
key = "loglevel"
arg = "verbose"
}
if (!command && (npm.commands.hasOwnProperty(arg))) {
if (key) {
conf[key] = true
key = null
}
command = arg
} else if (!flagsDone && arg.substr(0, 2) === "--") {
if (key) conf[key] = true
key = arg.substr(2)
if (key === "usage") conf[key] = true, key = null
flagsDone = (key === "")
} else if (key) {
conf[key] = arg
key = null
} else arglist.push(arg)
}
if (key) conf[key] = true
if (conf.noreg) conf.registry = null
npm.argv = arglist
var vindex = arglist.indexOf("-v")
, printVersion = vindex !== -1 || conf.version
if (printVersion) {
console.log(npm.version)
return
} else log("npm@"+npm.version, "using")
log("node@"+process.version, "using")
// make sure that this version of node works with this version of npm.
var semver = require("./lib/utils/semver")
, nodeVer = process.version
, reqVer = npm.nodeVersionRequired
if (reqVer && !semver.satisfies(nodeVer, reqVer)) {
errorHandler(new Error(
"npm doesn't work with node " + nodeVer + "\nRequired: node@" + reqVer), true)
}
process.on("uncaughtException", errorHandler)
if (!command) conf.usage = true
if (conf.usage && command !== "help") {
arglist.unshift(command)
command = "help"
}
// now actually fire up npm and run the command.
// this is how to use npm programmatically:
conf._exit = true
npm.load(conf, function (er) {
if (er) return errorHandler(er)
npm.commands[command](arglist, errorHandler)
})
})()