forked from BryanCrotaz/broccoli-npm-pull
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
79 lines (70 loc) · 2.13 KB
/
index.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
var Plugin = require('broccoli-plugin');
var path = require('path');
var fs = require('fs');
var RSVP = require('rsvp');
var required = require('required');
var symlinkOrCopySync = require('symlink-or-copy').sync;
// Create a subclass NodeImporter derived from Plugin
NodeImporter.prototype = Object.create(Plugin.prototype);
NodeImporter.prototype.constructor = NodeImporter;
function NodeImporter(inputNodes, options) {
options = options || {};
Plugin.call(this, inputNodes, {
annotation: options.annotation
});
this.options = options;
this.options.ignore = this.options.ignore || [];
this.options.mainFile = this.options.mainFile || 'index.js';
}
NodeImporter.prototype.build = function() {
this.npmPaths = [];
var indexPath = path.join(this.inputPaths[0], this.options.mainFile);
var output = "";
var nodeModulesOutPath = path.join(this.outputPath, 'node_modules');
fs.mkdirSync(nodeModulesOutPath);
var self = this;
return new RSVP.Promise(function(resolve, reject) {
required(indexPath, function(err, deps) {
if (err) {
reject(err);
}
else {
self.linkDeps(deps, nodeModulesOutPath);
resolve(null);
}
});
});
};
NodeImporter.prototype.linkDeps = function(deps, nodeModulesOutPath) {
const moduleFolderRegex = /(.*\\node_modules\\)([^\\]*)\\.*/;
if (deps) {
var len = deps.length;
for (var i = 0; i < len; i++) {
var dependency = deps[i];
if (!dependency.core &&
!this.options.ignore.contains(dependency.id))
{
var parts = moduleFolderRegex.exec(dependency.filename);
if (parts)
{
var folder = parts[2];
if (this.npmPaths.indexOf(folder) === -1)
{
// haven't seen this folder before
this.npmPaths.push(folder);
var modulePath = path.join(parts[1], folder);
this.linkNpmModule(modulePath, nodeModulesOutPath);
}
// recurse
this.linkDeps(dependency.deps, nodeModulesOutPath);
}
}
}
}
}
NodeImporter.prototype.linkNpmModule = function(folder, nodeModulesOutPath) {
var module = path.basename(folder);
moduleOutPath = path.join(nodeModulesOutPath, module);
symlinkOrCopySync(folder, moduleOutPath);
}
module.exports = NodeImporter;