-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplates.mjs
85 lines (74 loc) · 2.4 KB
/
templates.mjs
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { resolve, dirname } from "path"
import { readdir, readFile, lstat } from "fs/promises"
const reInclude = new RegExp('{%-?\\s+include\\s+("|\')(\\./\\S+?)\\1\\s+-?%}', 'g')
/**
*
* @param {string} folder
* @returns {Promise<Record<string, string>>}
*/
export async function resolveTemplates (folder) {
/** @type {Record<string, string>} */
const _cached = {}
const loadTemplates = async (baseDir) => {
const names = await readdir(baseDir)
await Promise.all(names.map(async (name) => {
const filepath = resolve(baseDir, name)
const stat = await lstat(filepath)
if (stat.isFile()) {
const content = await readFile(filepath, { encoding: 'utf-8' })
_cached[filepath] = content
} else if (stat.isDirectory()) {
await loadTemplates(filepath)
}
}))
}
const resolveIncludes = (filepath) => {
let content = _cached[filepath]
if (content === undefined) {
const filename = filepath.replace(resolve(folder) + '/', '')
throw new Error(`Template "${filename}" does not exist`)
}
const found = content.match(reInclude)
if (found) {
found.forEach(str => {
const name = str.replace(/^{%-?\s+include\s+("|')/, '').replace(/("|')\s+-?%}$/, '')
content = content.replace(str, resolveIncludes(resolve(dirname(filepath), name)))
})
}
return content.trim()
}
await loadTemplates(folder)
/** @type {Record<string, string>} */
const templates = {}
Object.keys(_cached).forEach(filepath => {
const name = filepath.replace(resolve(folder) + '/', '')
// exclude files whose name startswith _
// exclude files in subfolder
if (/\.j2$/.test(name) && !/^[_.]/.test(name) && !/\//.test(name)) {
templates[name] = resolveIncludes(filepath)
}
})
return templates
}
/**
*
* @param {Record<string, string>} templates
* @param {Record<string, string>} context
* @returns {Record<string, string>}
*/
export function updateTemplatesContext (templates, context) {
/** @param {string} content */
const updateContext = (content) => {
Object.keys(context).forEach(key => {
if (/^_/.test(key)) {
const reVar = new RegExp('\\{\\{\\s*' + key + '\\s*\\}\\}', 'g')
content = content.replace(reVar, context[key])
}
})
return content
}
Object.keys(templates).forEach(key => {
templates[key] = updateContext(templates[key])
})
return templates
}