Change `dateCreated` property to `datePublished`
[GitHub/WoltLab/WCF.git] / extra / update-font-awesome-metadata.ts
1 import * as fs from "fs";
2
3 // This script expects you to write the output to
4 // ts/WoltLabSuite/WebComponent/fa-metadata.js
5
6 if (process.argv.length !== 4) {
7 throw new Error(
8 "Expected the path to the `icons.json` metadata as argument #1 and the output format (js, php) as argument #2.",
9 );
10 }
11
12 const iconsJson = process.argv[2];
13 const outputFormat = process.argv[3];
14
15 if (!fs.existsSync(iconsJson)) {
16 throw new Error(`The path '${iconsJson}' does not exist.`);
17 }
18
19 const content = fs.readFileSync(iconsJson, { encoding: "utf8" });
20 let json: MetadataIcons;
21 try {
22 json = JSON.parse(content);
23 } catch (e) {
24 throw new Error(`Unable to parse the metadata file: ${e.message}`);
25 }
26
27 const values: IconData[] = [];
28 const aliases: IconAlias[] = [];
29 Object.entries(json).forEach(([name, icon]) => {
30 // Skip brand icons, because those are only supported as SVG
31 // through the `{icon}` template helper.
32 if (icon.styles.includes("brands")) {
33 return;
34 }
35
36 const codepoint = String.fromCharCode(parseInt(icon.unicode, 16));
37 values.push([name, [codepoint, icon.styles.includes("regular")]]);
38
39 if (icon.aliases && Array.isArray(icon.aliases.names)) {
40 icon.aliases.names.forEach((alias) => {
41 aliases.push([alias, name]);
42 });
43 }
44 });
45
46 let output;
47 switch (outputFormat) {
48 case "js":
49 output = `(() => {
50 const aliases = new Map(
51 ${JSON.stringify(aliases)}
52 );
53 const metadata = new Map(
54 ${JSON.stringify(values)}
55 );
56
57 window.getFontAwesome6Metadata = () => {
58 return new Map(metadata);
59 };
60
61 window.getFontAwesome6IconMetadata = (name) => {
62 return metadata.get(aliases.get(name) || name);
63 };
64 })();\n`;
65 break;
66 case "php":
67 output = `<?php
68
69 return [
70 ${values
71 .map(([name]) => name)
72 .sort()
73 .map((name) => ` '${name}' => true,`)
74 .join("\n")}
75
76 /* Aliases */
77 ${aliases
78 .map(([alias]) => alias)
79 .sort()
80 .map((name) => ` '${name}' => true,`)
81 .join("\n")}
82 ];\n`;
83 break;
84 default:
85 throw new Error("Invalid output format");
86 }
87
88 process.stdout.write(output);
89
90 type MetadataIcons = {
91 [key: string]: MetadataIcon;
92 };
93
94 type MetadataIconAliases = {
95 names?: string[];
96 };
97
98 type MetadataIcon = {
99 aliases?: MetadataIconAliases;
100 styles: string[];
101 unicode: string;
102 };
103
104 type Codepoint = string;
105 type HasRegularVariant = boolean;
106
107 type IconAlternateName = string;
108 type IconName = string;
109 type IconAlias = [IconAlternateName, IconName];
110
111 type IconData = [string, IconMetadata];
112
113 type IconMetadata = [Codepoint, HasRegularVariant];