forked from baconjs/bacon.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assemble.js
executable file
·104 lines (80 loc) · 2.42 KB
/
assemble.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
#!/usr/bin/env node
"use strict";
var _ = require("lodash");
var fs = require("fs");
var path = require("path");
// Manifest to build
var manifest = "main";
var defaultOutput = path.join(__dirname, "dist", "Bacon.coffee");
// Boilerplate: *header* and *footer*
var header = fs.readFileSync(path.join(__dirname, "src", "boilerplate", "object.coffee"), "utf-8");
var footer = fs.readFileSync(path.join(__dirname, "src", "boilerplate", "exports.coffee"), "utf-8");
var pieceCache = {};
var dependenciesRegex = /#\s+build\-dependencies\s*:?\s*([a-zA-Z_, \t]*)/g;
function readPiece(pieceName) {
if (!pieceCache[pieceName]) {
var contents = fs.readFileSync(path.join(__dirname, "src", pieceName + ".coffee"), "utf-8");
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(); }))
}
// Put in cache
pieceCache[pieceName] = {
name: pieceName,
deps: deps,
contents: contents,
};
}
return pieceCache[pieceName];
}
function resolve(pieceName, resolving) {
resolving = resolving || [];
if (_.contains(resolving, pieceName)) {
throw new Error("circular dependency resolving " + piece + "; stack: " + resolving.join(""));
}
// read piece
var piece = readPiece(pieceName);
// recursively resolve dependencies
var recResolving = [pieceName].concat(recResolving)
var deps = _.chain(piece.deps)
.map(function (x) { return resolve(x, recResolving); })
.flatten()
.value();
return _.uniq(deps.concat([piece]));
}
// 16 spaces
var padding = " ";
var main = function(options){
options = options || {};
var pieces = resolve(manifest);
if (options.verbose) {
console.info("Linearised dependency graph:")
_.each(pieces, function (p) {
if (p.deps.length === 0) {
console.info(" ", p.name);
} else {
console.info(" ", p.name + padding.substr(p.name.length), "←", p.deps.join(", "));
}
});
}
var output = [
header,
_.pluck(pieces, "contents").join("\n"),
footer,
].join("\n");
if (options.output) {
try {fs.mkdirSync("dist")} catch (e) {}
fs.writeFileSync(options.output, output);
} else {
console.log(output);
}
}
if (require.main === module) {
main({
verbose: true,
output: defaultOutput,
});
}
exports.main = main;