-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
93 lines (82 loc) · 2.5 KB
/
build.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
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
86
87
88
89
90
91
92
93
const fs = require('fs')
const basePath = './src/html/'
const indexFilePath = basePath + 'index.html'
const pagePath = basePath + 'pages/'
const controlsPath = basePath + 'controls/'
const publicPath = './public/'
async function readFile(filePath) {
return await fs.promises.readFile(filePath, 'utf8')
}
async function writeFile(filePath, content) {
return await fs.promises.writeFile(filePath, content, err => {
if (err) {
console.error(err)
return
}
})
}
function getPageVariables(fileContent) {
return fileContent.match(/\{\{page\..*\}\}/g)
}
function getPagePathsFromPageVariables(pageVariables) {
let pagePaths = {}
for (let key in pageVariables) {
let variable = pageVariables[key].replace(/\{|\}/g, '')
let path = pagePath + variable + '.html'
var shortKey = variable.split('\.')[1]
pagePaths[shortKey] = path
}
return pagePaths
}
function print(content) {
console.log(content)
}
async function getPageTemplates(pageFilePaths) {
let templates = {}
for (let key in pageFilePaths) {
let promise = readFile(pageFilePaths[key])
let content = await promise
templates[key] = content
}
return templates
}
async function loadControls() {
let filenames = await fs.promises.readdir(controlsPath)
let result = {}
for (let filename of filenames) {
let content = await readFile(controlsPath + filename)
let controlname = filename.split('.')[0]
result[controlname] = content
}
return result
}
async function insertControls(pageTemplates) {
let controls = await loadControls()
for (let control in controls) {
for (let key in pageTemplates) {
const pattern = new RegExp('\{\{controls\.' + control + '\}\}', 'g')
pageTemplates[key] = pageTemplates[key].replace(pattern, controls[control])
}
}
return pageTemplates
}
async function insertPages(pageTemplates) {
let indexFile = await readFile(indexFilePath)
for (let key in pageTemplates) {
indexFile = indexFile.replace(new RegExp('\{\{page\.' + key + '\}\}', 'g'), pageTemplates[key])
}
return indexFile
}
async function writePages(pages) {
for (let key in pages) {
}
return []
}
readFile(indexFilePath)
.then(result => getPageVariables(result))
.then(pageVariables => getPagePathsFromPageVariables(pageVariables))
.then(pagePaths => getPageTemplates(pagePaths))
.then(pageTemplates => insertControls(pageTemplates))
.then(result => insertPages(result))
.then(result => writeFile(publicPath + 'index.html', result))
//.then(result => print(result))