/
cli.js
executable file
·86 lines (75 loc) · 2.1 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
#!/usr/bin/env node
// usage:
// npm [global options] command [command args]
// only run as main module.
if (module.id !== ".") return;
// figure out where we're at.
// don't assume that npm is installed in any particular spot, since this
// might conceivably be a bootstrap attempt.
var fs = require("fs"),
path = require("path"),
sys = require("sys"),
path = require("path"),
npm;
if (fs.lstatSync(__filename).isSymbolicLink()) {
do {
__filename = fs.readlinkSync(__filename);
npm = path.join(path.dirname(__filename), "npm");
} while (fs.lstatSync(__filename).isSymbolicLink());
} else {
npm = path.join(__dirname, "npm");
}
npm = require(npm);
// supported commands.
var commands =
[ "help"
, "install"
, "activate"
, "ls"
, "list"
, "deactivate"
, "uninstall"
, "link"
],
log = require(npm.moduleName+"/../lib/utils/log");
var argv = process.argv, arg = "";
while (argv.shift() !== module.filename);
log(sys.inspect(process.argv), "cli");
// add usage onto any existing help documentation.
npm.help = (function (h) { return function () {
usage();
h && h.apply(npm, arguments);
}})(npm.help);
var globalOption = (function () {
// state or something goes here.
return function (arg) {
usage();
throw new Error("global options not yet implemented");
}
})();
var state = "globalOptions", command = "help";
while (arg = argv.shift()) {
if (commands.indexOf(arg) !== -1) {
command = arg;
break;
} else globalOption(arg);
}
function usage () {
var out = (arg === "help" ? "puts" : "error");
function p (m) { sys[out](m); return p };
p
("")
("Usage: ")
(" " + path.basename(module.filename) + " [global options] <command> [command options]")
("")
("[global options] Not yet implemented")
("<command> Method to call on the npm object.")
(" Supported: "+commands)
("[command options] The arguments to pass to the function.");
}
var result = npm[command].apply(npm, argv.concat(function (er, ok) {
if (er) {
log("failed " + er.message);
throw er;
} else log("ok");
}));