-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (53 loc) · 2.45 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
var ejs = require('ejs');
var colors = require('colors');
var program = require('commander');
var path = require('path');
var co = require('co');
var fs = require('co-fs');
var mkdirp = require('mkdirp');
var list = function (val) {
return val.split(',');
};
program.version('0.0.6')
.usage('[options] <file ...>')
.option('-c, --configs <items>', 'Config files', list)
.option('-o, --output [dirname]', 'Output dirname')
.option('-e, --extname [extname]', 'Output extname')
.parse(process.argv);
// console.log(program.args);
var configFiles = program.configs || [];
var outputDir = program.output || './output';
var extname = program.extname || '.html';
co(function *() {
console.log(` Checking template files: ${'start ...'.green}`);
var tStats = yield program.args.map((file)=>fs.stat(file));
var templates = program.args.filter((file, i)=>tStats[i].isFile());
console.log(` Checking template files: ${'ok!'.green}`);
console.log(` Checking config files: ${'start ...'.green}`);
var cStats = yield configFiles.map((file)=>fs.stat(file));
var configs = configFiles.filter((file, i)=>cStats[i].isFile());
console.log(` Checking config files: ${'ok!'.green}`);
console.log(` Merging config datas: ${'start ...'.green}`);
var configContents = yield configs.map((file)=>fs.readFile(file));
var configData = configContents
.map((buffer)=>JSON.parse(buffer.toString('utf-8')))
.reduce((prev, next)=>Object.assign(prev, next));
console.log(` Merging config datas: ${'ok!'.green}`);
console.log(` Filling datas into templates: ${'start ...'.green}`);
var templateContents = yield templates.map((file)=>fs.readFile(file));
var templateContents = templateContents
.map((buffer)=>ejs.render(buffer.toString('utf-8'), configData, {}));
console.log(` Filling datas into templates: ${'ok!'.green}`);
console.log(` Making output directory -> [${outputDir.red}]: ${'start ...'.green}`);
mkdirp.sync(outputDir);
console.log(` Making output directory -> [${outputDir.red}]: ${'ok'.green}`);
console.log(` Writing files: ${'start ...'.green}`);
var result = yield templates.map((file, i)=> {
var target = path.join(outputDir, path.basename(file, '.ejs') + extname);
fs.writeFile(target, templateContents[i]);
});
console.log(` Writing files: ${'ok!'.green}`);
// console.log(result);
}).catch(function (err) {
console.log(err.stack)
});