-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.js
48 lines (41 loc) · 1.4 KB
/
build.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
// configs
var configs = {
project_name: 'Handlebars Cookbook',
out_dir: 'generated/',
data_dir: 'bookdata/',
partial_dir: 'partials/'
};
// required libs
var fs = require('fs');
var handlebars = require('handlebars');
var yaml = require('js-yaml');
module.exports = function () {
delete require.cache[require.resolve('./helpers')];
// all book data
var data = fs.readdirSync(configs.data_dir).reduce(function (O, fn) {
if (fn.match(/.yaml$/)) {
O.push(Object.assign(yaml.load(fs.readFileSync(configs.data_dir + fn, 'utf8'), {filename: fn}), {pagename: O.length ? fn.replace(/.yaml/, '') : 'index'}));
}
return O;
}, []);
var partials = fs.readdirSync(configs.partial_dir).reduce(function (O, fn) {
O[fn] = fs.readFileSync(configs.partial_dir + fn, 'utf8');
return O;
}, {});
if (!fs.existsSync(configs.out_dir)) {
fs.mkdirSync(configs.out_dir);
}
handlebars.compile('{{#book_writer .}}{{> template}}{{/book_writer}}', {
preventIndent: true
})(data, {
data: {
datetime: new Date().toISOString(),
samples: ['lightncandy', 'handlebars.js', 'mustache'],
configs: configs,
package: require('./package.json'),
composer: require('./composer.json')
},
partials: partials,
helpers: require('./helpers')
});
};