forked from baconjs/bacon.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-deps.js
82 lines (65 loc) · 2.41 KB
/
build-deps.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
var _ = require("lodash");
var fs = require("fs");
var path = require("path");
var assert = require("assert");
var dependenciesRegex = /(?:#|\/\/)\s+build\-dependencies\s*:?\s*([a-zA-Z_, \t]*)/g;
function readDeps(contents) {
var deps = [];
var depsRegex = new RegExp(dependenciesRegex);
var match;
while (match = depsRegex.exec(contents)) {
deps = deps.concat(match[1].split(/\s*[, \t]\s*/).map(function (x) {
return x.trim().toLowerCase()
}))
}
return deps
}
function readPiece(pieceName, dir, pieceCache) {
if (!pieceCache[pieceName]) {
var jsPath = path.join(dir, pieceName + ".js");
var coffeePath = path.join(dir, pieceName + ".coffee");
var type = fs.existsSync(jsPath) ? "js" : "coffee";
var filepath = type === "js" ? jsPath : coffeePath;
var contents = fs.readFileSync(filepath, "utf-8");
// Put in cache
pieceCache[pieceName] = {
name: pieceName,
deps: readDeps(contents),
contents: contents,
type: type,
};
}
return pieceCache[pieceName];
}
function resolve(pieceNames, dir, resolving, pieceCache, options) {
resolving = resolving || [];
return _.uniq(_.flatten(pieceNames.map(function(pieceName) {
var piece = readPiece(pieceName, dir, pieceCache);
if (_.contains(resolving, pieceName)) {
throw new Error("circular dependency resolving " + piece + "; stack: " + resolving.join(""));
}
var deps = _.chain(piece.deps)
.map(function (x) {
if (options.recursive)
return resolve([x], dir, resolving.concat([pieceName]), pieceCache, options)
else
return readPiece(x, dir, pieceCache)
})
.flatten()
.value();
return deps.concat([piece]);
})))
}
module.exports.resolvePieces = function(pieceNames, dir, options) {
if (!options) options = { recursive: true };
return resolve(pieceNames, dir, [], {}, options);
}
var isCoffeePiece = (piece) => piece.type === "coffee";
var isJsPiece = (piece) => piece.type === "js";
module.exports.splitPieces = function (pieces) {
var coffeePieces = _.chain(pieces).takeWhile(isCoffeePiece).value();
var jsPieces = _.chain(pieces).dropWhile(isCoffeePiece).takeWhile(isJsPiece).value();
var restPieces = _.chain(pieces).dropWhile(isCoffeePiece).dropWhile(isJsPiece).value();
assert(restPieces.length === 0, "There shouldn't be coffee pieces after js ones.");
return [coffeePieces, jsPieces];
};