-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
184 lines (152 loc) · 4.67 KB
/
index.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
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
/* ATTENTION!!!
This code looks bad and will be rewritten (the functionality will not change),
but it is usable now.
If you find any errors, please report them in issues :) */
const fs = require("fs");
const path = require("path");
const obj_to_str = require("./lib/obj_to_str");
const props_to_str = require("./lib/props_to_str");
const parse_template = require("./lib/parse_template");
const comps_structure = require("./lib/comps_structure");
const HELPERS = require("./lib/HELPERS");
// render function
async function render(FILE = "index.html", DATA = {}, OPT = {}) {
let HTML;
try {
HTML = fs.readFileSync(
path.join(process.cwd(), FILE),
OPT.encoding || "utf8"
);
let structure = comps_structure(process.cwd(), HTML, DATA, OPT);
function traverse_comps(component, parent = null) {
if ( !component || !component.components || component.components.length === 0 ) {
handle_comp(component, parent);
return;
}
for (const nestedComponent of component.components) {
traverse_comps(nestedComponent, component);
}
handle_comp(component, parent);
}
let is_replacing;
function handle_comp(comp, parent) {
if (!is_replacing) {
// Compiling components
comp.content = parse_template(comp.content);
// --------------------
} else {
// Replacing components
if (!parent) return;
parent.content = parent.content.replace(comp.initiator, () => {
return `await(async($slot,$prop)=>{
${comp.content}
return ''
})(\`${comp.slot || ""}\`,${props_to_str(comp.props)})`;
});
// --------------------
}
}
traverse_comps(structure);
is_replacing = true;
traverse_comps(structure);
HTML = structure.content;
// fs.writeFileSync(path.join(process.cwd(), "struct.json"), JSON.stringify(structure))
// console.dir(structure, { depth: null });
let RAW = `
let {${Object.keys(HELPERS)}} = $helpers;
let {${Object.keys(DATA)}} = $data;
${HTML}
`
RAW = RAW.replace(/className\s*=\s*(["'`])/g, "class=$1");
RAW = RAW.replaceAll("\\<", "<");
RAW = RAW.replaceAll("\\>", ">");
// Don't format inside back quotes
let btc;
RAW = RAW.replace(/`|^\s+/gm, (match) =>
"`" === match ? ((btc = !btc), match) : btc ? match : ""
);
// -------------------------------
const AsyncFunction = Object.getPrototypeOf(
async function () {}
).constructor;
if (OPT?.mode === "client") {
HTML = `<script>
let $html = "";
(async function($helpers,$data){
document.currentScript.remove()
${RAW}
document.write($html)
})(
${obj_to_str(HELPERS)},
${obj_to_str(DATA)}
)
</script>`;
} else {
if (OPT?.raw !== "all") {
HTML = await new AsyncFunction(
`let $html = "";
const $helpers = ${obj_to_str(HELPERS)};
const $data = ${obj_to_str(DATA)};
${RAW}
;return $html`
)();
}
if (OPT?.raw === "all") HTML = RAW;
if (OPT?.raw === "add") HTML += `\n<!--\n${RAW}\n-->`;
}
} catch (err) {
HTML = `<h2>${(err + "").replaceAll("<", "<")}</h2>
<p>${err.stack.replaceAll("<", "<")}</p>
<style>
body{
color:#fff;
background:#0e0808;
margin:30px
}
h2{
color:#ff5e5e;
font-family:sans-serif
}
</style>`;
if (OPT.raw && typeof RAW !== "undefined") HTML += `\n<!--\n${RAW}\n-->`;
console.error(err);
}
return HTML;
}
// router function
async function router(app, paths) {
let obj = {};
for (let [key, val] of Object.entries(paths)) {
app.get(key, async (req, res) => {
if (typeof val === "string") {
obj = { page: val };
}
if (typeof val === "function") {
obj = await val(req, res);
}
obj.data ||= {};
obj.data.$page = obj.$page || obj.page;
obj.data.$path = req.path;
obj.data.$slug = req.params;
res.send(await render(obj.file || "index.html", obj.data, obj.options));
});
}
}
function run_msg(p = {}) {
const port = p.port || 7500;
const open = p.open || false;
const host = `http://localhost:${port}`;
console.clear();
console.log("\x1b[33m%s\x1b[0m", "Wolt is running!");
console.log("\x1b[36m%s\x1b[0m", host);
if (open) {
const start =
process.platform == "darwin"
? "open"
: process.platform == "win32"
? "start"
: "xdg-open";
require("child_process").exec(start + " " + host);
}
}
module.exports = { render, router, run_msg };