-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.ts
185 lines (153 loc) · 4.7 KB
/
build.ts
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
import {serve as httpServer} from "https://deno.land/std@0.80.0/http/server.ts";
import {renderToString} from "https://deno.land/x/dejs@0.9.3/mod.ts";
import ws from "https://deno.land/x/deno_ws@0.1.4/mod.ts";
// const WASM_PATH = "./wasm/target/wasm32-unknown-unknown/release/wasm.wasm";
const serve = Deno.args[0] === "serve";
let indexTemplate = "";
let editorTemplate = "";
let css = "";
let js = "";
function compress(text: string, force = false): string{
return serve && !force ? text : text.split("\n").map((line) => line.trim()).join("");
}
async function loadIndexTemplate(){
indexTemplate = await Deno.readTextFile("./src/index.ejs");
}
async function loadEditorTemplate(){
editorTemplate = await Deno.readTextFile("./src/editor.ejs");
}
async function loadCSS(){
css = compress(await Deno.readTextFile("./src/style.css"), true).replace(/: /g, ":");
}
async function loadJS(){
try{
const {diagnostics, files} = await Deno.emit("src/app.ts", JSON.parse(await Deno.readTextFile("tsconfig.json")));
if(diagnostics.length > 0){
console.error(Deno.formatDiagnostics(diagnostics));
if(!serve){
Deno.exit(1);
}
}
// HACK: there shouldn't need to be any `replace` calls
js = compress(files["deno:///bundle.js"].replace(/Pyramid1/g, "Pyramid").replace(/Box1/g, "Box"));
}catch(err){
console.error(err);
}
}
async function build(): Promise<{index: string, editor: string}>{
while(indexTemplate === ""){
await loadIndexTemplate();
}
while(editorTemplate === ""){
await loadEditorTemplate();
}
while(css === ""){
await loadCSS();
}
while(js === ""){
await loadJS();
}
let version = "unknown";
if(serve){
const versionProcess = Deno.run({cmd: ["git", "describe", "--tags"], stdout: "piped", stderr: "piped"});
version = new TextDecoder().decode(await versionProcess.output()).split("-")[0];
versionProcess.close();
}else{
version = Deno.env.get("GIT_TAG") ?? "unknown";
}
return {
index: await renderToString(indexTemplate, {
css,
}),
editor: await renderToString(editorTemplate, {
version,
css,
js,
}),
}
}
await loadIndexTemplate();
await loadEditorTemplate();
await loadCSS();
await loadJS();
if(serve){
const WS_PORT = 8001;
const RELOAD_COMMAND = "reload";
const RELOAD_TIMEOUT = 50;
const RELOAD_HTML = `<script>var ssgs=new WebSocket("ws://localhost:${WS_PORT}");ssgs.onmessage=function(event){if(event.data==="${RELOAD_COMMAND}"){window.location.reload()}}</script>`;
const server = httpServer({port: 8000});
console.log("dev server running on http://localhost:8000/");
const wss = new ws.Server(undefined, WS_PORT);
const _reload = async (event: Deno.FsEvent): Promise<void> => {
if(event.paths[0].endsWith("index.ejs")){
console.log("index template changed");
await loadIndexTemplate();
}else if(event.paths[0].endsWith("editor.ejs")){
console.log("editor template changed");
await loadEditorTemplate();
}else if(event.paths[0].endsWith("style.css")){
console.log("css changed");
await loadCSS();
}else if(event.paths[0].endsWith(".ts")){
console.log("js changed");
await loadJS();
}
wss.clients.forEach((client) => client.send(RELOAD_COMMAND));
};
let timeoutId = 0;
const reload = (event: Deno.FsEvent) => {
if(timeoutId){
clearTimeout(timeoutId);
}
timeoutId = setTimeout(() => _reload(event), RELOAD_TIMEOUT);
};
setTimeout(async () => {
for await(const event of Deno.watchFs("src", {recursive: true})){
await reload(event);
}
});
for await(const req of server){
// if(req.url === "/wasm.wasm"){
// const headers = new Headers();
// headers.set("content-type", "application/wasm");
// req.respond({
// status: 200,
// headers,
// body: await Deno.readFile(WASM_PATH)
// });
// continue;
// }
try{
const pages = await build();
let html;
if(req.url === "/editor"){
html = pages.editor;
}else{
html = pages.index;
}
req.respond({
status: 200,
body: html + RELOAD_HTML,
});
}catch(err){
req.respond({
status: 500,
body: `<!DOCTYPE html><html><body><pre>${err}</pre>${RELOAD_HTML}</body></html>`,
});
}
}
}else{
try{
await Deno.lstat("build");
}catch(err){
if(err instanceof Deno.errors.NotFound){
await Deno.mkdir("build");
}else{
throw err;
}
}
// await Deno.copyFile(WASM_PATH, "./build/wasm.wasm");
const pages = await build();
await Deno.writeTextFile("build/index.html", pages.index);
await Deno.writeTextFile("build/editor.html", pages.editor);
}