forked from buschtoens/broccoli-flatiron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (45 loc) · 1.55 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
var fs = require("fs"),
path = require("path"),
mkdirp = require("mkdirp"),
Writer = require("broccoli-writer"),
Promise = require("rsvp").Promise;
Flatiron.prototype = Object.create(Writer.prototype);
Flatiron.prototype.constructor = Flatiron;
function Flatiron (inputTree, options) {
if (!(this instanceof Flatiron)) return new Flatiron(inputTree, options);
this.inputTree = inputTree;
this.options = options;
}
Flatiron.prototype.write = function (readTree, destDir) {
var _this = this;
return readTree(this.inputTree).then(function(srcDir) {
var obj = readDirectory(srcDir),
output;
function readDirectory (srcDir) {
var obj = {},
entries = fs.readdirSync(srcDir);
Array.prototype.forEach.call(entries, function(entry) {
if (fs.lstatSync(path.join(srcDir, entry)).isDirectory()) {
obj[entry] = readDirectory(path.join(srcDir, entry));
}
else {
try {
obj[_this.options.trimExtensions ? entry.split(".")[0] : entry] =
fs.readFileSync(path.join(srcDir, entry), { encoding: "utf8" });
}
catch(e) {
try {
obj[entry] = readDirectory(path.join(srcDir, entry));
}
catch(e) {}
}
}
});
return obj;
}
output = "export default " + JSON.stringify(obj, null, 2);
mkdirp.sync(path.join(destDir, path.dirname(_this.options.outputFile)));
fs.writeFileSync(path.join(destDir, _this.options.outputFile), output);
});
}
module.exports = Flatiron;