-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (44 loc) · 1.43 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
#! /usr/bin/env node
const fs = require('fs')
const program = require('commander')
const twig = require('twig').twig
const glob = require('glob')
const path = require('path')
const beautify = require('js-beautify').html
const pkgJSON = require('./package.json')
const render = (dir) => {
const option = {
ignore: ['node_modules/**']
}
glob(dir.source, option, (er, files) => {
files.forEach(source => {
const file = path.parse(source).base
const output = path.normalize(dir.output + file)
const dataSource = glob.sync(dir.data, [option])
const obj = {}
let data = ''
let parsed = ''
dataSource.forEach(file => {
parsed = JSON.parse(fs.readFileSync(file))
data = Object.assign(obj, parsed)
})
twig({
path: source,
load: template => {
const content = template.render(data)
fs.writeFileSync(output, beautify(content, { extra_liners: ' ', preserve_newlines: false }))
console.log('created', output);
}
})
})
})
}
program
.version(pkgJSON.version)
.description(pkgJSON.description)
.usage('--source [directory] --data [directory] --output [directory]')
.option('-s, --source <directory>', 'define the data path [optional]')
.option('-d, --data <path>', 'define the data path [optional]')
.option('-o, --output <directory>', 'define the output directory')
.action(render)
.parse(process.argv)