-
Notifications
You must be signed in to change notification settings - Fork 34
/
npmunbox.js
156 lines (136 loc) · 4.38 KB
/
npmunbox.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// npmbox by Glen R. Goodwin (@areinet)
// https://github.com/arei/npmbox.git
// Extracts an .npmbox file and installs the contained package.
"use strict";
var boxxer = require("./npmboxxer.js");
var utils = require("./utils");
var argv = require("optimist")
.string([
"C","prefix",
"proxy",
"https-proxy"
])
.boolean([
"v","verbose",
"s","silent",
"g","global",
"scripts",
"S","save",
"D","save-dev",
"O","save-optional",
"B","save-bundle",
"E","save-exact"
])
.options("p", {
alias: "path",
default: process.cwd()
})
.options("i", {
alias: "install"
})
.argv;
var args = argv._;
if (args.length<1 || argv.help) {
console.log("npmunbox - Extracts one or more .npmbox files and installs the contained package(s).");
console.log("");
console.log("Usage: ");
console.log("");
console.log(" npmunbox --help");
console.log(" npmunbox [options] <nmpbox-file> <npmbox-file>...");
console.log("");
console.log("Options:");
console.log("");
console.log(" -v, --verbose Shows npm output which is normally hidden.");
console.log(" -s, --silent Hide all output.");
console.log(" -p, --path Specify the path to a folder from which the .npmbox file(s) will be read.");
console.log(" -i, --install=<pkg> Installs the indicated package instead of using the .npmbox manifest.");
console.log(" --scripts Enable running of scripts during installation.");
console.log(" -g, --global Installs package(s) globally as if --global was passed to npm.");
console.log(" -C, --prefix npm --prefix switch.");
console.log(" -S, --save npm --save switch.");
console.log(" -D, --save-dev npm --save-dev switch.");
console.log(" -O, --save-optional npm --save-optional switch.");
console.log(" -B, --save-bundle npm --save-bundle switch.");
console.log(" -E, --save-exact npm --save-exact switch.");
console.log(" --proxy=<url> npm --proxy switch.");
console.log(" --https-proxy=<url> npm --https-proxy switch.");
console.log("");
process.exit(0);
}
var options = {
verbose: argv.v || argv.verbose || false,
silent: argv.s || argv.silent || false,
global: argv.g || argv.global || false,
save: argv.S || argv.save || false,
"save-dev": argv.D || argv["save-dev"] || false,
"save-optional": argv.O || argv["save-optional"] || false,
"save-bundle": argv.B || argv["save-bundle"] || false,
"save-exact": argv.E || argv["save-exact"] || false,
"ignore-scripts": !argv.scripts,
path: argv.p || argv.path || false
};
if (argv.C || argv.prefix) options.prefix = argv.C || argv.prefix;
if (argv.proxy) options.proxy = argv.proxy;
if (argv["https-proxy"]) options["https-proxy"] = argv["https-proxy"];
var errorCount = 0;
var sources = args.filter(function(source){
return !!source;
});
var complete = function() {
boxxer.cleanup(function(){
process.reallyExit(errorCount);
});
};
var reportErrors = function(args) {
args.forEach(function(arg){
errorCount += 1;
console.error(" ",arg);
});
}
// Handles the case where we pay attention to the manifests inside the box(es).
var unboxFromManifest = function() {
var unboxDone = function(err) {
if (err) reportErrors(utils.flatten(utils.toArray(arguments)));
unboxNext();
};
var unboxNext = function() {
var source = sources.shift();
if (!source) return complete();
unboxExecute(source);
};
var unboxExecute = function(source) {
if (!options.silent) console.log("\nUnboxing "+source+"...");
boxxer.unbox(source,options,unboxDone);
};
unboxNext();
}
// Handles the case where we are installing a package listed via the `--install`
// option. This unpacks all the boxes (on top of each other) and then does a
// single `npm install`.
var unboxWithInstallOption = function(pkg) {
var installDone = function(err) {
if (err) reportErrors(utils.flatten(utils.toArray(arguments)));
complete();
};
var unpackDone = function(err) {
if (err) {
reportErrors(utils.flatten(utils.toArray(arguments)));
return complete();
}
unpackNext();
};
var unpackNext = function() {
var source = sources.shift();
if (source) unpackExecute(source);
else {
boxxer.install(pkg,options,installDone);
}
};
var unpackExecute = function(source) {
boxxer.unpack(source,options,unpackDone);
};
unpackNext();
}
if (sources.length===0) return complete();
else if (argv.install) unboxWithInstallOption(argv.install);
else unboxFromManifest();