-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
70 lines (54 loc) · 1.59 KB
/
main.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
import {
bold,
cyan,
green,
yellow,
} from "https://deno.land/std@0.156.0/fmt/colors.ts";
import { fetchCss, FontFace } from "./common.ts";
declare global {
interface String {
colour(fn: (arg0: string) => string): string;
heavy(): string;
}
}
String.prototype.colour = function (fn) {
return fn(this as string);
};
String.prototype.heavy = function () {
return bold(this as string);
};
try {
Deno.mkdirSync("fonts");
} catch (e) {
if (e.code !== "EEXIST") {
throw e;
}
}
const prefix = "fonts";
if (Deno.args.length < 1) Deno.exit(1);
const css = await fetchCss(new URL(Deno.args.at(0)!));
console.log("Received this css:".colour(green).heavy());
console.log(css.colour(yellow));
const fontFaceRegex = /(\/\*.*\*\/\s@font-face {[^}]*})/g;
const fonts = [];
for (const face of css.matchAll(fontFaceRegex)) {
const fontFaceDecl = face.pop()!;
console.log("Parsing this @font-face declaration:".colour(green).heavy());
console.log(fontFaceDecl.colour(yellow));
const parsed = FontFace.fromCssDecl(fontFaceDecl);
console.log("Parsing successful:".colour(cyan));
console.log(parsed);
fonts.push(parsed);
}
const out = fonts.reduce(
(acc, font) => acc.replace(font.url, `./${font.fileName}`),
css,
);
fonts.map((font) => {
return { fileName: font.fileName, blob: font.fetchFont() };
}).map(async ({ fileName, blob }) => {
const blobResolved = await blob;
const arr = new Uint8Array(await blobResolved.arrayBuffer());
await Deno.writeFile(`${prefix}/${fileName}`, arr, { create: true });
});
Deno.writeTextFileSync(`${prefix}/fonts.css`, out, { create: true });