Skip to content

Commit 5b970ec

Browse files
authored
impr: use typescript for font-preview and fontawesome scripts (@fehmer) (#5613)
1 parent f729b9e commit 5b970ec

4 files changed

Lines changed: 56 additions & 23 deletions

File tree

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import * as fs from "fs";
22
import * as path from "path";
33
import { fileURLToPath } from "url";
4-
import Fonts from "../static/fonts/_list.json" assert { type: "json" };
4+
import Fonts from "../static/fonts/_list.json";
55
import subsetFont from "subset-font";
66

77
const __filename = fileURLToPath(import.meta.url);
88
const __dirname = path.dirname(__filename);
99

10-
export async function generatePreviewFonts(debug) {
10+
export async function generatePreviewFonts(
11+
debug: boolean = false
12+
): Promise<void> {
1113
const srcDir = __dirname + "/../static/webfonts";
1214
const targetDir = __dirname + "/../static/webfonts-preview";
1315
fs.mkdirSync(targetDir, { recursive: true });
@@ -17,7 +19,7 @@ export async function generatePreviewFonts(debug) {
1719
for (const font of Fonts) {
1820
if (font.systemFont) continue;
1921

20-
const display = (font.display || font.name) + "Fontfamily";
22+
const display = (font.display ?? font.name) + "Fontfamily";
2123

2224
const fileNames = srcFiles.filter((it) =>
2325
it.startsWith(font.name.replaceAll(" ", "") + "-")
@@ -29,7 +31,7 @@ export async function generatePreviewFonts(debug) {
2931
);
3032
const fileName = fileNames[0];
3133

32-
generateSubset(
34+
await generateSubset(
3335
srcDir + "/" + fileName,
3436
targetDir + "/" + fileName,
3537
display
@@ -42,7 +44,7 @@ export async function generatePreviewFonts(debug) {
4244
}
4345
}
4446

45-
async function generateSubset(source, target, name, debug) {
47+
async function generateSubset(source, target, name): Promise<void> {
4648
const font = fs.readFileSync(source);
4749
const subset = await subsetFont(font, name, {
4850
targetFormat: "woff2",
@@ -51,5 +53,5 @@ async function generateSubset(source, target, name, debug) {
5153
}
5254
//detect if we run this as a main
5355
if (import.meta.url.endsWith(process.argv[1])) {
54-
generatePreviewFonts(true);
56+
await generatePreviewFonts(true);
5557
}
Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
import * as fs from "fs";
22
import * as path from "path";
33

4+
type FontawesomeConfig = {
5+
/* used regular icons without `fa-` prefix*/
6+
regular: string[];
7+
/* used solid icons without `fa-` prefix*/
8+
solid: string[];
9+
/* used brands icons without `fa-` prefix*/
10+
brands: string[];
11+
};
12+
13+
type FileObject = { name: string; isDirectory: boolean };
14+
415
const iconSet = {
516
solid: parseIcons("solid"),
617
regular: parseIcons("regular"),
@@ -39,20 +50,13 @@ const modules2 = {
3950
stacked: ["stack", "stack-1x", "stack-2x", "inverse"],
4051
};
4152

42-
/**
43-
* fontawesome icon config
44-
* @typedef {Object} FontawesomeConfig
45-
* @property {string[]} solid - used solid icons without `fa-` prefix
46-
* @property {string[]} brands - used brands icons without `fa-` prefix
47-
*/
48-
4953
/**
5054
* Detect used fontawesome icons in the directories `src/**` and `static/**{.html|.css}`
5155
* @param {boolean} debug - Enable debug output
5256
* @returns {FontawesomeConfig} - used icons
5357
*/
5458

55-
export function getFontawesomeConfig(debug = false) {
59+
export function getFontawesomeConfig(debug = false): FontawesomeConfig {
5660
const time = Date.now();
5761
const srcFiles = findAllFiles(
5862
"./src",
@@ -66,7 +70,7 @@ export function getFontawesomeConfig(debug = false) {
6670
);
6771

6872
const allFiles = [...srcFiles, ...staticFiles];
69-
const usedClassesSet = new Set();
73+
const usedClassesSet: Set<string> = new Set();
7074

7175
const regex = /\bfa-[a-z0-9-]+\b/g;
7276

@@ -130,12 +134,15 @@ if (import.meta.url.endsWith(process.argv[1])) {
130134
getFontawesomeConfig(true);
131135
}
132136

133-
function toFileAndDir(dir, file) {
137+
function toFileAndDir(dir, file): FileObject {
134138
const name = path.join(dir, file);
135139
return { name, isDirectory: fs.statSync(name).isDirectory() };
136140
}
137141

138-
function findAllFiles(dir, filter = (filename) => true) {
142+
function findAllFiles(
143+
dir: string,
144+
filter: (filename: string) => boolean = (_it): boolean => true
145+
): string[] {
139146
return fs
140147
.readdirSync(dir)
141148
.map((it) => toFileAndDir(dir, it))
@@ -147,11 +154,12 @@ function findAllFiles(dir, filter = (filename) => true) {
147154
}, []);
148155
}
149156

150-
function parseIcons(iconSet) {
151-
const file = fs
157+
function parseIcons(iconSet: string): string[] {
158+
const file: string | null = fs
152159
.readFileSync(`node_modules/@fortawesome/fontawesome-free/js/${iconSet}.js`)
153160
.toString();
161+
154162
return file
155-
.match(/\"(.*)\"\: \[.*\],/g)
156-
.map((it) => it.substring(1, it.indexOf(":") - 1));
163+
?.match(/"(.*)": \[.*\],/g)
164+
?.map((it) => it.substring(1, it.indexOf(":") - 1)) as string[];
157165
}

frontend/scripts/tsconfig.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"compilerOptions": {
3+
"incremental": true,
4+
"module": "ESNext",
5+
"target": "ESNext",
6+
"sourceMap": false,
7+
"allowJs": true,
8+
"checkJs": true,
9+
"outDir": "build",
10+
"moduleResolution": "node",
11+
"resolveJsonModule": true,
12+
"allowSyntheticDefaultImports": true,
13+
"esModuleInterop": true,
14+
"strictNullChecks": true,
15+
"skipLibCheck": true,
16+
"noEmit": true
17+
},
18+
"ts-node": {
19+
"files": true
20+
},
21+
"files": ["../src/ts/types/types.d.ts"],
22+
"include": ["./**/*.ts"]
23+
}

frontend/vite.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import Inspect from "vite-plugin-inspect";
99
import autoprefixer from "autoprefixer";
1010
import "dotenv/config";
1111
import { fontawesomeSubset } from "fontawesome-subset";
12-
import { getFontawesomeConfig } from "./scripts/fontawesome.mjs";
13-
import { generatePreviewFonts } from "./scripts/font-preview.mjs";
12+
import { getFontawesomeConfig } from "./scripts/fontawesome";
13+
import { generatePreviewFonts } from "./scripts/font-preview";
1414

1515
function pad(numbers, maxLength, fillString) {
1616
return numbers.map((number) =>

0 commit comments

Comments
 (0)