forked from arei/npmbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnpmboxxer.js
296 lines (235 loc) · 6.89 KB
/
npmboxxer.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// npmbox by Glen R. Goodwin (@areinet)
// https://github.com/arei/npmbox.git
// Shared code for npmbox/npmunbox
"use strict";
(function(){
var npm = require("npm");
var fs = require("fs");
var path = require("path");
var targz = require("tar.gz");
var rimraf = require("rimraf");
var is = require("is");
var Decompress = require("decompress");
var cwd = process.cwd();
var work = path.resolve(cwd,".npmbox.work");
var cache = path.resolve(cwd,".npmbox.cache");
var cleanCache = function(callback) {
process.chdir(cwd);
if (fs.existsSync(cache)) rimraf(cache,callback);
else callback();
};
var cleanWork = function(callback) {
process.chdir(cwd);
if (fs.existsSync(work)) rimraf(work,callback);
else callback();
};
var cleanAll = function(callback) {
cleanCache(function(){
cleanWork(function(){
callback();
});
});
};
var tarCreate = function(source,target,callback) {
new targz(6,6,false).compress(source,target,callback);
};
var tarExtract = function(source,target,callback) {
// new targz().extract(source,target,callback);
var decompress = new Decompress({
mode:755
});
decompress.src(source);
decompress.dest(target);
decompress.use(Decompress.targz({}));
decompress.run(callback);
};
var npmInit = function(options,callback) {
npm.load(options,callback);
};
var npmInstall = function(source,callback) {
if (!is.array(source)) source = [source];
npm.commands.install(source,callback);
};
var npmDependencies = function(packageName,options,callback) {
if (!packageName) return callback(null);
var checked = {};
var results = {};
var pending = [];
var done = function(packageName) {
pending = pending.filter(function(p){
return p!==packageName;
});
if (pending.length>0) return;
var deps = Object.keys(results);
deps = deps.sort();
callback(null,deps);
};
var lookup = function(packageName) {
if (checked[packageName]) return;
pending.push(packageName);
checked[packageName] = true;
var args = [packageName];
if (!options.silent) console.log(" Querying "+packageName);
npm.commands.view(args,true,function(err,deps){
if (err && err.statusCode && err.statusCode===404) return done(packageName);
if (err) return callback(err);
if (!deps) return callback("Package '"+packageName+"' was not found in npm.");
var found = Object.keys(deps).slice(-1)[0]; // we want the last entry.
if (found) {
var fullname = packageName.split(/@/g)[0] || packageName;
fullname += "@"+found;
results[fullname] = true;
var children = [];
if (deps[found].dependencies) {
Object.keys(deps[found].dependencies).forEach(function(key){
var value = deps[found].dependencies[key];
if (key && value) children.push(key+"@"+value);
});
}
if (deps[found].optionalDependencies) {
Object.keys(deps[found].optionalDependencies).forEach(function(key){
var value = deps[found].optionalDependencies[key];
if (key && value) children.push(key+"@"+value);
});
}
// if (deps[found].devDependencies) {
// Object.keys(deps[found].devDependencies).forEach(function(key){
// var value = deps[found].devDependencies[key];
// if (key && value) children.push(key+"@"+value);
// });
// }
children.forEach(function(childPackageName){
lookup(childPackageName);
});
}
done(packageName);
});
};
lookup(packageName);
};
var npmDownload = function(packageNames,options,callback) {
if (!packageNames) return callback(null);
if (!is.array(packageNames)) packageNames = [packageNames];
var pending = [].concat(packageNames);
var done = function(packageName) {
pending = pending.filter(function(p){
return p!==packageName;
});
if (pending.length>0) return;
callback(null,packageNames);
};
var populate = function(packageName) {
if (!options.silent) console.log(" Downloading "+packageName);
//npm.commands.cache.add(name,ver,where,scrub,cb)
npm.commands.cache.add(packageName,null,null,false,function(err){
if (err) return callback("Error occured downloading '"+packageName+"' to cache.");
done(packageName);
});
};
packageNames.forEach(function(packageName){
populate(packageName);
});
};
var box = function(source,options,callback) {
var target = path.resolve(cwd,source+".npmbox");
if (fs.existsSync(target)) {
callback("An .npmbox file already exist with this name. Please remove it and try again.");
return;
}
if (fs.existsSync(cache)) {
callback("An .npmbox.cache folder already exist and might conflict. Please remove it first or work from a different directory.");
return;
}
fs.mkdirSync(cache);
var done = function(err) {
var args = arguments;
cleanAll(function(){
if (!err && !options.silent) console.log(" Done.");
callback.apply(null,args);
});
};
var pack = function() {
if (!options.silent) console.log(" Packing "+target+"...");
tarCreate(cache,target,function(err){
if (err) return done(err);
done();
});
};
var stack = function(deps) {
npmDownload(deps,options,function(err){
if (err) return done(err);
pack(deps);
});
};
var rack = function() {
npmDependencies(source,options,function(err,deps){
if (err) return done(err);
stack(deps);
});
};
var init = function() {
var npmoptions = {
cache: cache,
prefix: work,
global: true,
optional: true,
force: true,
loglevel: options && options.verbose ? "http" : "silent"
};
npmInit(npmoptions,function(err){
if (err) return done(err);
rack();
});
};
init();
};
var unbox = function(source,options,callback) {
var target = source.replace(/\.npmbox$/,"");
if (!fs.existsSync(source)) {
source = path.resolve(cwd,source+".npmbox");
if (!fs.existsSync(source)) {
callback("No .npmbox file found: "+source);
return;
}
}
if (!fs.existsSync(cache)) fs.mkdirSync(cache);
var done = function(err) {
cleanAll(function(){
callback(err);
});
};
var install = function() {
if (!options.silent) console.log("Installing "+target+"...");
var packageName = path.basename(target);
npmInstall([packageName],function(err){
if (err) return done(err);
done();
});
};
var unpack = function() {
if (!options.silent) console.log("Unpacking "+target+"...");
tarExtract(source,".",function(err){
if (err) return done(err);
install();
});
};
var init = function () {
options.cache = cache;
options.loglevel = options.verbose ? "verbose" : "silent";
options["cache-min"] = 99999;
options["fetch-retries"] = 0;
options["fetch-retry-factor"] = 0;
options["fetch-retry-mintimeout"] = 1;
options["fetch-retry-maxtimeout"] = 2;
npmInit(options,function(err){
if (err) return done(err);
unpack();
});
};
init();
};
module.exports = {
box: box,
unbox: unbox
};
})();