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