-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathtemplate-utils.mjs
More file actions
29 lines (24 loc) · 915 Bytes
/
template-utils.mjs
File metadata and controls
29 lines (24 loc) · 915 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
import Handlebars from 'handlebars'
import debugModule from '../debug.mjs'
import { processFile, readFile, writeFile } from './fs-utils.mjs'
const debug = debugModule.errors
export async function compileTemplate (filePath) {
const indexTemplateSource = readFile(filePath)
return Handlebars.compile(indexTemplateSource)
}
export async function processHandlebarFile (filePath, substitutions) {
return processFile(filePath, (rawSource) => processHandlebarTemplate(rawSource, substitutions))
}
function processHandlebarTemplate (source, substitutions) {
try {
const template = Handlebars.compile(source)
return template(substitutions)
} catch (error) {
debug(`Error processing template: ${error}`)
return source
}
}
export function writeTemplate (filePath, template, substitutions) {
const source = template(substitutions)
writeFile(filePath, source)
}