-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
110 lines (89 loc) · 2.63 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env node
/*eslint-env node */
"use strict";
let fs = require("fs"),
FS = require("q-io/fs"),
path = require("path"),
glob = require("glob"),
_ = require("underscore"),
app = require("commander"),
Handlebars = require("handlebars"),
appInfo = require("./package.json");
app.version(appInfo.version)
.option("-o, --output [output]", "File to output results", "docs")
.option("-d, --debug [debug]", "See debug output", false)
.option("-e, --extension [extension]", "Handlebars file extension", "hbs")
.parse(process.argv);
let filePath = app.args[0];
let debug = app.debug;
let compiledTemplates = {};
let rMustache = /{{(#[\w]*)?\s*([\w\.]*)\s*}}/g;
let log = function() {
console.log.apply(console, arguments);
};
log.debug = function() {
if (debug) {
log.apply(log, arguments);
}
};
let getFile = function(file) {
return new Promise(function(resolve, reject) {
FS.read(file)
.fail(function(err) {
reject(err);
})
.then(function(contents) {
let fileName = path.basename(file, ".hbs");
log.debug("Processing template: " + fileName);
compiledTemplates[fileName] = {
name: fileName,
path: file,
data: []
};
let template = compiledTemplates[fileName];
let myArray;
let str = contents.toString();
while ((myArray = rMustache.exec(str)) !== null) {
if (myArray[2]) {
template.data.push(myArray[2]);
}
}
template.data = _.uniq(template.data).sort();
resolve();
});
});
};
glob(filePath + "/**/*." + app.extension, function(err, files) {
if (err) {
throw err;
}
let promises = [];
files.forEach(function(file) {
promises.push(getFile(file));
});
Promise.all(promises).then(function() {
FS.read("templates/docbars.hbs")
.then(function(contents) {
if (err) {
throw err;
}
let template = Handlebars.compile(contents.toString())({
templates: compiledTemplates
});
FS.makeDirectory(app.output)
.fail(function() {
return Promise.all([]);
})
.then(function() {
log.debug("Making directory");
return FS.write(path.join(app.output, "docbars.json"), JSON.stringify(compiledTemplates, null, 4));
})
.then(function() {
return FS.write(path.join(app.output, "docbars.html"), template);
})
.then(function() {
log("Generated docs for " + Object.keys(compiledTemplates).length + " templates!");
});
});
});
});