-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathembedder.ts
80 lines (68 loc) · 1.98 KB
/
embedder.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
import * as base64 from "https://deno.land/[email protected]/encoding/base64.ts";
import * as path from "https://deno.land/[email protected]/path/mod.ts";
const dirs = [
{
src: "static",
dest: "static",
},
{
src: "views",
dest: "views",
},
{
src: "styles/out.min.css",
dest: "styles/out.css",
},
];
await run();
if (Deno.args[0] === "watch") {
const watcher = Deno.watchFs(dirs.map((x) => x.src));
for await (const _event of watcher) {
await run();
}
}
async function run() {
const out: { [key: string]: string } = {};
async function processDir(dirPath: string, destPath: string) {
for await (const sub of Deno.readDir(dirPath)) {
if (sub.isFile) {
out[path.join(destPath, sub.name)] = base64.encode(
await Deno.readFile(path.join(dirPath, sub.name)),
);
} else {
await processDir(
path.join(dirPath, sub.name),
path.join(destPath, sub.name),
);
}
}
}
for (const dir of dirs) {
if ((await Deno.stat(dir.src)).isFile) {
out[dir.dest] = base64.encode(await Deno.readFile(dir.src));
} else {
await processDir(dir.src, dir.dest);
}
}
const outJs = `
// Generated ${new Date().toLocaleString()}
import * as base64 from "https://deno.land/[email protected]/encoding/base64.ts";
const textFromB64 = (s: string) => atob(s);
const bytesFromB64 = (s: string) => base64.decode(s);
export const files: Record<string, {_v: string, text: () => string, bytes: () => ArrayBuffer, }> = {
${
Object.entries(out).map(([k, v]) => {
return `${JSON.stringify(k)}: { _v: ${
JSON.stringify(v)
}, text: function() {return textFromB64(this._v)}, bytes: function () {return bytesFromB64(this._v) }, },`;
}).map((x) => `\t${x}`).join("\n")
}
};
export function getFile(p: string) {
return files[p];
}
export default files;
`.trim();
await Deno.writeTextFile("embedded.ts", outJs);
console.log("Generated embedded.ts");
}