-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
87 lines (75 loc) · 2.42 KB
/
main.ts
File metadata and controls
87 lines (75 loc) · 2.42 KB
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
import { WebUI } from "jsr:@webui/deno-webui@^2.5.8";
import { exists } from "jsr:@std/fs/exists";
// MIME types for file serving
const MIME_TYPES: Record<string, string> = {
'html': 'text/html',
'js': 'application/javascript',
'css': 'text/css',
'json': 'application/json',
'png': 'image/png',
'jpg': 'image/jpeg',
'jpeg': 'image/jpeg',
'gif': 'image/gif',
'svg': 'image/svg+xml',
'ico': 'image/x-icon',
'txt': 'text/plain',
'pdf': 'application/pdf',
'zip': 'application/zip',
'woff': 'font/woff',
'woff2': 'font/woff2',
'ttf': 'font/ttf',
'otf': 'font/otf',
} as const;
const myWindow = new WebUI();
myWindow.setSize(816, 639); // Window size needs to account of excess title bar and window border size
async function startWatchMode(): Promise<void> {
console.log('👀 Starting watch mode...');
let buildTimeout: number | null = null;
// Watch for file changes
const watcher = Deno.watchFs(['./static'], { recursive: true });
for await (const event of watcher) {
if (event.kind == 'modify') {
// Debounce
if (buildTimeout) {
clearTimeout(buildTimeout);
}
buildTimeout = setTimeout(() => {
myWindow.showBrowser("index.html", WebUI.Browser.AnyBrowser);
}, 300);
}
}
}
const getFile = async (
contentType: string,
filename: string,
): Promise<Uint8Array> => {
const content = await Deno.readFile(import.meta.dirname + filename);
const header = `HTTP/1.1 200 OK\r\nContent-Type: ${contentType}\r\n\r\n`;
const headerBytes = new TextEncoder().encode(header);
const response = new Uint8Array(headerBytes.length + content.length);
response.set(headerBytes);
response.set(content, headerBytes.length);
return response;
};
async function myFileHandler(myUrl: URL) {
if(myUrl.pathname.includes('com.chrome.devtools.json')) return "HTTP/1.1 404 Not Found";
const extension = myUrl.pathname.split('.').pop();
if (extension == undefined) return "HTTP/1.1 404 Not Found";
if(MIME_TYPES[extension]) {
return await getFile(MIME_TYPES[extension], "/static" + myUrl.pathname)
}
return "HTTP/1.1 404 Not Found";
}
// Bind Exit
myWindow.bind("exit", () => {
// Close all windows and exit
WebUI.exit();
});
myWindow.setFileHandler(myFileHandler);
await myWindow.showBrowser("index.html", WebUI.Browser.AnyBrowser);
if( await exists("./static", { isDirectory: true })) {
startWatchMode();
}
await WebUI.wait();
console.log("Exiting...");
Deno.exit(0);