-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
74 lines (63 loc) · 1.91 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
const promise = require('bluebird');
var Deptrace = module.exports = function Deptrace (opts) {
opts = opts || {};
if (opts.depsFor) {
this.depsFor = opts.depsFor;
} else {
throw new Error('You must provide a method to find dependencies.');
}
if (opts.setup) {
this.setup = opts.setup;
}
if (opts.resolve) {
this.resolve = opts.resolve;
}
if (opts.format) {
this.format = opts.format;
}
this.concurrency = opts.concurrency || require('os').cpus().length;
};
// extract an array of dependencies from some input
Deptrace.prototype.depsFor = function (input) {
throw new Error('Implement a method to return an array of dependencies.');
};
// resolve an individual dependency into a more detailed form
Deptrace.prototype.resolve = function (dep, parents) {
return promise.resolve(dep);
};
// format a node in a dependency graph after all dependencies are resolved
Deptrace.prototype.format = function (input, nodes) {
input.label = input.name;
input.nodes = nodes;
return promise.resolve(input);
};
Deptrace.prototype.graph = function (input, parents) {
var ready = promise.resolve();
var initialCall = (arguments.length === 1);
if (!parents) {
parents = [];
if (typeof this.setup === 'function') {
ready = promise.resolve(this.setup());
}
}
return ready.then(function () {
parents = parents.slice();
parents.push(input);
var recurse = this._recurse(input, parents);
if (initialCall) {
recurse = recurse.then(this.format.bind(this, input));
}
return recurse;
}.bind(this));
};
Deptrace.prototype._recurse = function (input, parents) {
return this.depsFor(input).
map(function (dep) {
return this.resolve(dep, parents);
}.bind(this)).
map(function (dep) {
return this.graph(dep, parents).
then(this.format.bind(this, dep));
}.bind(this));
};
Deptrace.packageJson = require('./lib/package_json');