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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
const fs = require('fs')
const path = require('path')
const logger = require('./logger')()
const template = require('../template/template')
const routesConfig = require('../config/routes')
class BuildPages {
constructor(x) {
this.newTemplate = new Map()
this.template = template({baseURL: x.baseURL})
this.pages = typeof this.GetAllPages() !== 'undefined' ? this.GetAllPages() : new Map()
this.path = []
this.pages.forEach((value, key) => {
this.path.push(key)
})
this.routes = typeof x.routes !== 'undefined' ? x.routes : new Map()
this.#AddTemplate({
name: 'start',
template: `
<!DOCTYPE html>
<html>
`
})
this.html = {
start: `
<!DOCTYPE html>
<html>
`,
end: `
</html>
`
}
}
GetSitemap() {return this.path}
Build(pageData) {
const split = pageData.path.split('/')
const string = `/${split[1]}`
if (false) {
} else if (this.pages.has(string)) {
if (split[2] !== undefined && split[2] !== '') {
const newPath = `/${split[1]}/${split[2]}`
} else {
const pages = this.pages.get(string)
const data = pages (pageData)
this.map = new Map()
for (let i = 0; i < data.page.length; i++) {
this.object = {}
this.object.data = data.page[i]
this.object.name = this.object.data.name
if (pageData.path === '/') {
if (this.object.name === 'index') {
this.data = this.object.data()
this.output = {
html: this.data.html,
css: this.data.css,
js: this.data.js
}
this.output.js.push('loading.js')
return this.output
}
} else {
this.data = this.object.data()
this.output = {
html: this.data.html,
css: this.data.css,
js: this.data.js
}
this.output.js.push('loading.js')
return this.output
}
}
}
} else {
// ! 404 Not Found
const notFound = this.template.NotFound()
this.output = {
html: {
header: this.template.Header(),
main: notFound.html,
footer: this.template.Footer()
},
css: [],
js: ['loading.js'],
pages: this.pages
}
notFound.css.forEach(file => this.output.css.push(file))
notFound.js.forEach(file => this.output.js.push(file))
return this.output
}
return false
}
Template(pageData) {
const split = pageData.path.split('/')
const string = `/${split[1]}`
if (this.routes.has(pageData.path)) {
} else if (this.pages.has(string)) {
if (split[2] !== undefined && split[2] !== '') {
}
}
const html = {
start: `
<!DOCTYPE html>
<html>
`,
head: `
<head>
<title>website</title>
<link rel="stylesheet" type="text/css" href="/css/loading.css">
</head>
`,
headerStart: `
<body>
<header>
`,
headerEnd: `
</header>
`,
mainStart: `
<main>
`,
mainEnd: `
</main>
`,
footerStart: `
<footer>
`,
footerEnd: `
</footer>
<script src='/js/page-init.js'></script>
<script src='/js/forge.min.js'></script>
</body>
`,
end: `
</html>
`
}
html.headerEnd = this.template.Loading() + html.headerEnd
let fullHTML = ''
Object.entries(html).forEach(([key, value]) => {
fullHTML += value
})
return this.#RemoveSpaces(fullHTML)
}
FixRoutes() {
this.pages = typeof this.GetAllPages() !== 'undefined' ? this.GetAllPages() : new Map()
this.routes = typeof x.routes !== 'undefined' ? x.routes : new Map()
}
IsAsync (fn) {fn.constructor.name === 'AsyncFunction'}
#AddTemplate() {this.newTemplate.set()}
#RemoveSpaces(html) {return html.replace(/(\n)\s+/g, '$1')}
GetRoutes() {}
GetAllPages() {
const object = {
modulesDir: path.resolve(`${__dirname}/../pages`),
map: new Map()
}
object.files = fs.readdirSync(object.modulesDir).filter(file => file.endsWith('.js'))
object.files.forEach(file => {
try {
const name = file.replace('.js', '')
const modulePath = path.join(object.modulesDir, file)
const moduleObject = require(modulePath)
this.path = '/'
if (name !== 'home') this.path = `/${name}`
if (!object.map.has(this.path)) object.map.set(this.path, moduleObject)
} catch (err) {
logger.Error(err)
}
})
return object.map
}
}
module.exports = (x) => {
const build = new BuildPages(x)
const routes = build.GetAllPages()
return {
build: build,
routes: routes
}
}
|