-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_colors.js
executable file
·55 lines (44 loc) · 1.59 KB
/
generate_colors.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
#!/usr/bin/env node
const fs = require("fs-extra");
const path = require("path");
const Handlebars = require("handlebars");
const colorConvert = require("color-convert");
const klaw = require("klaw");
const TEMPLATE_DATA = {
GENERATED_BLURB: "This is generated! Don't update manually!",
colors: require("./colors.json")
}
/**
* Convert the string into a usable programming identifier
*/
Handlebars.registerHelper("identifier", rawString => {
// prepend x, because x is cool
// e.g. 20 => x20
return /^[a-z]/.test(rawString) ? rawString : `x${rawString}`;
});
Handlebars.registerHelper("ternary", (predicate, yes, no) => predicate ? yes : no);
Handlebars.registerHelper("join", (sep, array) => array.join(sep));
Handlebars.registerHelper("keys", obj => Object.keys(obj));
Handlebars.registerHelper("rgb", hex => colorConvert.hex.rgb(hex));
function files(path) {
return new Promise((resolve, reject) => {
const paths = [];
klaw(path)
.on("data", item => item.stats.isFile() && paths.push(item.path))
.on("error", reject)
.on("end", () => resolve(paths));
});
}
async function processTemplate(templatePath) {
const source = await fs.readFile(templatePath, { encoding: "utf8" });
const template = Handlebars.compile(source, { noEscape: true });
const result = template(TEMPLATE_DATA);
const outPath = templatePath.replace("tmpl", "dist").replace(/.hbs$/, "");
await fs.mkdirp(path.dirname(outPath));
return fs.writeFile(outPath, result);
}
async function main() {
const paths = await files("./tmpl");
await Promise.all(paths.map(processTemplate));
}
main();