-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
restore-ejs-tag.js
50 lines (46 loc) · 1.23 KB
/
restore-ejs-tag.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
const fs = require('fs')
const util = require('util')
const path = require('path')
const ejsTagsMap = {
'<%': '<%',
'<%=': '<%=',
'<%-': '<%-',
'%>': '%>',
}
const { format } = require('prettier')
function restoreEjsTags(htmlStr) {
for (let key in ejsTagsMap) {
let regex = new RegExp(key, 'g')
htmlStr = htmlStr.replace(regex, ejsTagsMap[key])
}
return htmlStr
}
const readdir = util.promisify(fs.readdir)
const readFile = util.promisify(fs.readFile)
const writeFile = util.promisify(fs.writeFile)
const outDir = './out'
fs.mkdirSync('./ejs', {
recursive: true,
})
readdir(outDir)
.then((files) => {
files.forEach((file) => {
if (path.extname(file) === '.html') {
const filePath = path.join(outDir, file)
readFile(filePath, 'utf-8')
.then((data) => {
const restoredHtml = restoreEjsTags(data)
const writtenPath =
'./ejs/' + path.basename(file, 'html') + 'template.ejs'
return writeFile(
writtenPath,
format(restoredHtml, {
parser: 'html',
})
)
})
.catch((err) => console.error(err))
}
})
})
.catch((err) => console.error(err))