-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile-template.js
53 lines (48 loc) · 2.06 KB
/
compile-template.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
const path = require('path')
const fs = require('fs')
let tsconfig_content = fs.readFileSync(path.resolve(__dirname, 'tsconfig.json'), 'utf8')
const tsconfig = eval(`(${tsconfig_content})`)
const rootDir = path.resolve(__dirname, tsconfig.compilerOptions.rootDir)
const outDir = path.resolve(__dirname, tsconfig.compilerOptions.outDir)
function escapeHtml(str) {
return str.replace(/[&<>"' \n]/g, function (tag) {
const lookup = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": "'",
// " ": " ",
};
return lookup[tag] || tag;
});
}
// create template output directory
const templateOutDir = path.resolve(outDir, 'template')
if (!fs.existsSync(templateOutDir)) {
fs.mkdirSync(templateOutDir, { recursive: true })
} else if (fs.statSync(templateOutDir).isFile()) {
throw new Error(`"${templateOutDir}" is a file, not a directory`)
}
// get EJS file under template
const templateRootDir = path.resolve(rootDir, 'template')
const templateFiles = fs.readdirSync(templateRootDir).filter(file => file.endsWith('.ejs'))
for (let fn of templateFiles) {
// let content = fs.readFileSync(path.resolve(templateRootDir, fn), 'utf8')
// // <style inject="css/spectre.min.css" escape="false"></style>
// content = content.replace(/<([a-zA-Z\-]+)\s+inject="([^"]+)"\s+escape="([^"]+)"\s*><\/([a-zA-Z\-]+)>/g,
// function (substring, tag, inject, escape, closeTag) {
// let injectFilePath = path.resolve(templateRootDir, inject)
// let injectContent = fs.readFileSync(injectFilePath, 'utf8')
// escape = escape === 'true' || escape === '1'
// if (escape) {
// injectContent = escapeHtml(injectContent)
// }
// return `<${tag}>${injectContent}</${closeTag}>`
// }
// )
// write to outDir
let outFilePath = path.resolve(templateOutDir, fn)
// fs.writeFileSync(outFilePath, content, 'utf8')
fs.copyFileSync(path.resolve(templateRootDir, fn), outFilePath)
}