-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
37 lines (34 loc) · 871 Bytes
/
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
var pug = require('pug')
var path = require('path')
var fs = require('fs')
function compile(dir, defaults) {
return fs.readdirSync(dir).map(file => {
var f = path.join(dir, file)
var stat = fs.statSync(f)
if (stat && stat.isDirectory()) {
var obj = {}
obj[file] = compile(f, defaults)
return obj
} else {
return {
name: path.basename(file, path.extname(file)),
path: f
}
}
}).reduce((acc, val) => {
if (typeof(val.name) === 'string'
&& typeof(val.path) === 'string') {
var template = pug.compileFile(val.path)
acc[val.name] = locals => {
var all = Object.assign({}, defaults, locals)
return template(all)
}
} else {
Object.assign(acc, val)
}
return acc
}, {})
}
module.exports = (dir, defaults) => {
return compile(dir, defaults)
}