-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate.ts
57 lines (46 loc) · 1.88 KB
/
generate.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
/// <reference lib="dom" />
import { DOMParser } from "npm:[email protected]";
import { svg2png, initialize } from 'npm:[email protected]';
import resources from './resources.json' with { type: "json" };
type Elem<T> = T extends Array<infer R> ? R : never;
type ThemeKey = keyof typeof resources.themes;
type ColorKey = keyof typeof resources.colors;
type Theme = Record<string, ColorKey>;
function applyTheme(doc: SVGElement, theme: Record<string, keyof typeof resources.colors>): void {
for (const [selector, color] of Object.entries(theme)) {
const elem = doc.querySelector(selector);
if (!elem) continue;
elem.setAttribute('fill', resources.colors[color] ?? 'none');
}
}
await initialize(Deno.readFileSync('./lib/svg2png_wasm_bg.wasm'));
/**
* Parse the file contents and generate a DOM.
*/
const filesWithDOM = resources.files.map(async (file) => {
const textFile = await Deno.readTextFile(file.path);
return ({ ...file, dom: (new DOMParser).parseFromString(textFile, "image/svg+xml") })
});
/**
* Prepare the folders.
*/
await Deno.remove('./svg', { recursive: true });
await Deno.remove('./png', { recursive: true });
await Deno.mkdir('./svg', { recursive: true });
await Deno.mkdir('./png', { recursive: true });
filesWithDOM.forEach(async (filePromise) => {
const file = await filePromise;
const fileName = file.path.match(/([\w-]+)\.\w+$/)?.[1];
if (!fileName) return;
for (const theme of file.themes) {
if (!(theme in resources.themes)) return;
applyTheme(file.dom as unknown as SVGElement, resources.themes[theme as ThemeKey] as Theme);
const domString = file.dom.toString();
await Deno.writeTextFile(`./svg/${fileName}-${theme}.svg`, domString);
await Promise.all(file.scale.flatMap(async (scale) => {
return [Deno.writeFile(`./png/${fileName}-${theme}-x${scale}.png`, await svg2png(domString, {
scale,
}))];
}));
}
});