-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhbs2html.js
More file actions
47 lines (39 loc) · 1.33 KB
/
hbs2html.js
File metadata and controls
47 lines (39 loc) · 1.33 KB
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
#!/usr/bin/env node
var fs = require('fs'),
path = require('path'),
args = require('optimist').argv,
hbs = require('handlebars');
var files = args._;
var partialsDir = __dirname + (args.p ? '/' + args.p : 'partials');
var outputDir = __dirname + (args.o ? '/' + args.o : 'html');
if (!fs.existsSync(outputDir)){
fs.mkdirSync(outputDir);
}
console.log('### Registering partials in', partialsDir);
var filenames = fs.readdirSync(partialsDir);
filenames.forEach(function (filename) {
var matches = /^([^.]+).hbs$/.exec(filename);
if (!matches) {
return;
}
var name = matches[1];
var template = fs.readFileSync(partialsDir + '/' + filename, 'utf8');
hbs.registerPartial(name, template);
console.log("> partial " + filename + " was found");
});
console.log('### Compiling templates to', outputDir);
files.forEach(function (filename) {
fs.readFile(filename, 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
var template = hbs.compile(data)();
var outputName = path.basename(filename, '.hbs') + '.html';
fs.writeFile(outputDir + '/' + outputName, template, function(err) {
if(err) {
return console.log(err);
}
console.log("> " + outputName + " was compiled!");
});
});
});