-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.js
490 lines (427 loc) · 19.8 KB
/
plugin.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
const PATH = require("path");
const FS = require("graceful-fs");
const SPAWN = require("child_process").spawn;
const COPY = require("ncp").ncp;
exports.for = function(API, plugin) {
plugin.resolveLocator = function(locator, options) {
var self = this;
if (!/^https?:\/\//.test(locator.descriptor.pointer)) {
console.log("locator", locator);
throw new Error("TODO: Resolve pinf-style uris (github.com/sourcemint/loader/~0.1.0).");
}
return self.node.getPlugin("archive").then(function(pm) {
return pm.resolveLocator(locator, options);
});
}
plugin.install = function(packagePath, options) {
return API.Q.call(function() {
if (!PATH.existsSync(PATH.join(packagePath, "package.json"))) {
return;
}
function install() {
// Don't use NPM to call 'install' script and populate ENV with all typical SM ENV variables.
return callNPM(packagePath, [
"run-script",
"install"
], options);
}
if (packagePath === plugin.node.path) {
return install();
}
return API.SM_CORE.for(packagePath).install(options).then(function() {
return install();
});
});
}
plugin.test = function(node, options) {
if (!node.descriptors.package.scripts || !node.descriptors.package.scripts.test) {
API.TERM.stdout.writenl("\0yellow(No `scripts.test` property found in package descriptor for package '" + node.path + "'.\0)");
return API.Q.resolve();
}
var testCommand = node.descriptors.package.scripts.test;
if (options.cover) {
if (/^(?:node\s*)?(\S*\.js)$/.test(testCommand)) {
// TODO: Support other test coverage tools via config and relocate impl into separate plugins.
// Relocate this to `freedom-platform/dev`.
var coverageTestCommand = testCommand.replace(/^node\s*/, "");
coverageTestCommand = coverageTestCommand.replace(/\.js$/, "") + ".js";
if (PATH.existsSync(PATH.join(node.path, coverageTestCommand))) {
testCommand = "istanbul cover --dir .sm/coverage -- " + coverageTestCommand;
}
} else {
API.TERM.stdout.writenl("\0yellow(Cannot cover tests for '" + node.path + "' as `scripts.test` does not point to a javascript file.\0)");
return API.Q.resolve();
}
}
var opts = API.UTIL.copy(options);
opts.cwd = node.path;
opts.env = {
PATH: API.OS.getEnvPath([
PATH.join(node.path, "mapped_packages/.bin"),
PATH.join(node.path, "node_modules/.bin"),
PATH.join(__dirname, "node_modules/.bin")
])
// TODO: Populate ENV with all typical SM ENV variables.
}
testCommand = testCommand.split(" ");
return API.OS.spawnInline(testCommand.shift(), testCommand, opts);
}
plugin.export = function(path, options) {
return API.Q.call(function() {
if (PATH.existsSync(path)) {
if (!options.delete) {
API.TERM.stdout.writenl("\0red(" + "Export target directory '" + path + "' already exists. Use --delete." + "\0)");
throw true;
}
API.FS_RECURSIVE.rmdirSyncRecursive(path);
}
API.FS_RECURSIVE.mkdirSyncRecursive(path);
var ignoreRules = {
// Rules that match the top of the tree (i.e. prefixed with `/`).
top: {},
// Rules that apply to every level.
every: {},
filename: null
};
var stats = {
ignoreRulesCount: 0,
totalFiles: 0,
ignoredFiles: 0,
totalSize: 0
};
function loadIgnoreRules(callback) {
function insert(rule) {
var key = rule.split("*")[0];
var scope = (/^\//.test(rule)) ? "top" : "every";
if (!ignoreRules[scope][key]) {
ignoreRules[scope][key] = [];
}
var re = new RegExp(rule.replace("*", "[^\\/]*?"));
ignoreRules[scope][key].push(function applyRule(path) {
if (path === rule || re.test(path)) return true;
return false;
});
stats.ignoreRulesCount += 1;
}
[
".distignore",
".npmignore",
".gitignore"
].forEach(function(basename) {
if (!PATH.existsSync(PATH.join(plugin.node.path, basename))) return;
if (ignoreRules.filename !== null) return;
ignoreRules.filename = basename;
FS.readFileSync(PATH.join(plugin.node.path, basename)).toString().split("\n").forEach(function(rule) {
if (!rule) return;
insert(rule);
});
});
if (ignoreRules.filename === null) {
// Default rules.
insert(".git/");
insert(".DS_Store");
}
return callback(null);
}
function walkTree(subPath, callback) {
var list = {};
var c = 0;
FS.readdir(PATH.join(plugin.node.path, subPath), function(err, files) {
if (err) return callback(err);
if (files.length === 0) {
return callback(null, list);
}
function error(err) {
c = -1;
return callback(err);
}
function done() {
if (c !== 0) return;
c = -1;
return callback(null, list);
}
files.forEach(function(basename) {
if (c === -1) return;
function ignore(type) {
function select(ruleGroups, path) {
var rules = null;
if (ruleGroups[path]) {
rules = ruleGroups[path];
} else {
for (var prefix in ruleGroups) {
if (path.substring(0, prefix.length) === prefix) {
rules = ruleGroups[prefix];
break;
}
}
}
if (!rules && ruleGroups[""]) {
rules = ruleGroups[""];
}
if (rules) {
for (var i=0 ; i<rules.length ; i++) {
if (rules[i](path)) {
return true;
}
}
return false;
}
}
if (select(ignoreRules.top, subPath + "/" + basename + ((type === "dir") ? "/" : ""))) {
return true;
}
// All deeper nodes.
return select(ignoreRules.every, basename + ((type === "dir") ? "/" : ""));
}
c += 1;
FS.lstat(PATH.join(plugin.node.path, subPath, basename), function(err, stat) {
if (err) return error(err);
c -= 1;
if (stat.isSymbolicLink()) {
c += 1;
FS.readlink(PATH.join(plugin.node.path, subPath, basename), function(err, val) {
if (err) return error(err);
c -= 1;
// TODO: Detect circular links.
var linkDir = null;
try {
linkDir = FS.realpathSync(PATH.resolve(PATH.join(plugin.node.path, subPath), val));
} catch(err) {
if (err.code === "ENOENT") return done();
throw err;
}
c += 1;
FS.lstat(linkDir, function(err, linkStat) {
if (err) return error(err);
c -= 1;
stats.totalFiles += 1;
if (!ignore( linkStat.isDirectory() ? "dir" : "file")) {
list[subPath + "/" + basename] = {
mtime: stat.mtime.getTime(),
dir: linkStat.isDirectory() || false,
symlink: val,
symlinkReal: linkDir
};
} else {
stats.ignoredFiles += 1;
}
if (linkStat.isDirectory()) {
c += 1;
walkTree(subPath + "/" + basename, function(err, subList) {
if (err) return error(err);
c -= 1;
for (var key in subList) {
list[key] = subList[key];
}
done();
});
} else {
done();
}
});
});
} else
if (stat.isDirectory()) {
if (!ignore("dir")) {
list[subPath + "/" + basename] = {
dir: true
};
c += 1;
walkTree(subPath + "/" + basename, function(err, subList) {
if (err) return error(err);
c -= 1;
for (var key in subList) {
list[key] = subList[key];
}
done();
});
}
} else
if (stat.isFile()) {
stats.totalFiles += 1;
if (!ignore("file")) {
list[subPath + "/" + basename] = {
mtime: (stats.totalSize += stat.mtime.getTime()),
size: stat.size
};
} else {
stats.ignoredFiles += 1;
}
}
done();
});
});
done();
});
}
function copyFiles(fromPath, toPath, list, callback) {
var roots = [
[fromPath, toPath, false]
];
function copy() {
try {
var paths = roots.shift();
if (paths[2]) {
FS.unlinkSync(paths[1]);
}
COPY(paths[0], paths[1], {
filter: function(path) {
if (path === fromPath) return true;
path = ((paths[2])?paths[2]:"") + path.substring(paths[0].length);
if (list[path]) {
if (typeof list[path].symlink !== "undefined" && paths[0] != list[path].symlinkReal) {
roots.push([list[path].symlinkReal, PATH.join(toPath, path), path]);
}
return true;
}
return false;
}
}, function(err) {
if (err) return callback(err);
if (roots.length === 0) return callback(null);
return copy();
});
} catch(err) {
return callback(err);
}
}
copy();
}
function updatePackageDescriptors(callback) {
var c = 0;
function error(err) {
c = -1;
return callback(err);
}
function done() {
if (c !== 0) return;
c = -1;
return callback(null);
}
plugin.node.traverse(function(node) {
if (node.circular) return false;
// TODO: Always write a package descriptor even if original package did not have one?
if (!node.descriptors.package) return false;
var descriptorPath = PATH.join(path, node.summary.relpath, "package.json");
var descriptor = API.UTIL.deepCopy(node.descriptors.package);
// Remove properties that are not needed at runtime.
delete descriptor.version;
delete descriptor.pm;
delete descriptor.bugs;
delete descriptor.homepage;
delete descriptor.description;
delete descriptor.repository;
delete descriptor.scripts;
delete descriptor.name;
delete descriptor.license;
delete descriptor.author;
delete descriptor.maintainers;
delete descriptor.contributors;
delete descriptor.readme;
delete descriptor.dist;
delete descriptor.keywords;
delete descriptor.readmeFilename;
// sm specific.
delete descriptor.devMappings;
// npm specific.
delete descriptor.devDependencies;
delete descriptor._id;
delete descriptor._from;
// Set some required properties.
descriptor.uid = node.summary.repositoryUri || node.summary.homepageUri || (node.summary.pm.install + "-" + node.summary.name);
if (node.summary.rev) {
descriptor.rev = node.summary.rev;
}
descriptor.name = node.summary.name;
if (node.summary.version) {
descriptor.version = node.summary.version;
}
descriptor.pm = node.summary.pm.install;
// TODO: Order properties in standard sequence.
c += 1;
PATH.exists(descriptorPath, function(exists) {
c -= 1;
if (!exists) return done();
c += 1;
FS.writeFile(descriptorPath, JSON.stringify(descriptor, null, 4), function(err) {
if (err) return error(err);
c -= 1;
done();
});
});
});
done();
}
var deferred = API.Q.defer();
options.logger.info("Exporting source files from '" + plugin.node.path + "' to '" + path + "' based on '.distignore|.npmignore|.gitignore' rules.");
loadIgnoreRules(function(err) {
if (err) return deferred.reject(err);
walkTree("", function(err, list) {
if (err) return deferred.reject(err);
options.logger.info("Found '" + (stats.totalFiles - stats.ignoredFiles) + "' files (size: " + stats.totalSize + " bytes) while ignoring '" + stats.ignoredFiles + "' files based on '" + stats.ignoreRulesCount + "' ignore rules.");
copyFiles(plugin.node.path, path, list, function(err) {
if (err) return deferred.reject(err);
options.logger.info("Exported files to: " + path);
updatePackageDescriptors(function(err) {
if (err) return deferred.reject(err);
options.logger.info("Updated package descriptors in exported packages.");
return deferred.resolve();
});
});
});
});
return deferred.promise;
});
}
function callNPM(basePath, args, options) {
options = options || {};
var deferred = API.Q.defer();
if (options.verbose) {
API.TERM.stdout.writenl("\0cyan(Running: npm " + args.join(" ") + " (cwd: " + basePath + ")\0)");
}
var opts = {
cwd: basePath
};
if (options.env) {
opts.env = UTIL.copy(process.env);
for (var key in options.env) {
opts.env[key] = options.env[key];
}
}
var proc = SPAWN("npm", args, opts);
var buffer = "";
proc.on("error", function(err) {
deferred.reject(err);
});
proc.stdout.on("data", function(data) {
if (options.verbose) {
API.TERM.stdout.write(data.toString());
}
buffer += data.toString();
});
proc.stderr.on("data", function(data) {
if (options.verbose) {
API.TERM.stderr.write(data.toString());
}
buffer += data.toString();
});
proc.on("exit", function(code) {
if (code !== 0) {
API.TERM.stdout.writenl("\0red(" + buffer + "\0)");
deferred.reject(new Error("NPM error"));
return;
}
if (/npm ERR!/.test(buffer)) {
// WORKAROUND: NPM sometimes gives this error but all seems to be ok.
if (/cb\(\) never called!/.test()) {
TERM.stdout.writenl("\0red(IGNORING NPM EXIT > 0 AND HOPING ALL OK!\0)");
} else {
deferred.reject(new Error("NPM error: " + buffer));
return;
}
}
deferred.resolve();
});
return deferred.promise;
}
}