-
-
Notifications
You must be signed in to change notification settings - Fork 784
Expand file tree
/
Copy pathrender.ts
More file actions
31 lines (25 loc) · 761 Bytes
/
render.ts
File metadata and controls
31 lines (25 loc) · 761 Bytes
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
import _ from 'lodash'
import { file } from './core'
import { Context } from './types'
/**
* Render file if template.
*/
export default async (ctx: Context): Promise<void> => {
// interpolate
// https://github.com/lodash/lodash/blob/master/.internal/reEvaluate.js
const regexp = /<%(.+?)%>|\${(.+?)}/
const imports = {
...ctx.config.metadata,
...ctx.config.helpers
}
ctx.files.forEach(item => {
// ignore binary files
if (file.isBinary(item.contents)) return
const text = item.contents.toString()
// ignore files without interpolate
if (!regexp.test(text)) return
const compiled = _.template(text, { imports })
const newContents = compiled(ctx.answers)
item.contents = Buffer.from(newContents)
})
}