Skip to content

Commit bde950b

Browse files
committed
async functions to make watch more responsive
1 parent 549d592 commit bde950b

File tree

7 files changed

+79
-50
lines changed

7 files changed

+79
-50
lines changed

tools/lib/build.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ export default async () => {
1515

1616
console.log(`Building ${sport}...`);
1717

18-
reset();
19-
copyFiles();
18+
await reset();
19+
await copyFiles();
2020

2121
const jsonSchema = generateJSONSchema(sport);
2222
await fs.mkdir("build/files", { recursive: true });

tools/lib/buildFuncs.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import browserslist from "browserslist";
33
import { Buffer } from "node:buffer";
44
import crypto from "node:crypto";
55
import fs from "node:fs";
6+
import fsp from "node:fs/promises";
67
import fse from "fs-extra";
78
import * as htmlmin from "html-minifier-terser";
89
import * as sass from "sass";
@@ -231,7 +232,7 @@ const setSport = () => {
231232
});
232233
};
233234

234-
const copyFiles = (watch: boolean = false) => {
235+
const copyFiles = async (watch: boolean = false) => {
235236
const foldersToIgnore = [
236237
"baseball",
237238
"basketball",
@@ -240,7 +241,7 @@ const copyFiles = (watch: boolean = false) => {
240241
"hockey",
241242
];
242243

243-
fse.copySync("public", "build", {
244+
await fse.copy("public", "build", {
244245
filter: filename => {
245246
// Loop through folders to ignore.
246247
for (const folder of foldersToIgnore) {
@@ -263,31 +264,31 @@ const copyFiles = (watch: boolean = false) => {
263264
sport = "basketball";
264265
}
265266

266-
fse.copySync(path.join("public", sport), "build", {
267+
await fse.copy(path.join("public", sport), "build", {
267268
filter: filename => !filename.includes(".gitignore"),
268269
});
269270

270271
// Remove the empty folders created by the "filter" function.
271272
for (const folder of foldersToIgnore) {
272-
fs.rmSync(`build/${folder}`, { recursive: true, force: true });
273+
await fsp.rm(`build/${folder}`, { recursive: true, force: true });
273274
}
274275

275276
const realPlayerFilenames = ["real-player-data", "real-player-stats"];
276277
for (const filename of realPlayerFilenames) {
277278
const sourcePath = path.join("data", `${filename}.${sport}.json`);
278279
if (fs.existsSync(sourcePath)) {
279-
fse.copySync(sourcePath, `build/gen/${filename}.json`);
280+
await fse.copy(sourcePath, `build/gen/${filename}.json`);
280281
}
281282
}
282283

283-
fse.copySync("data/names.json", "build/gen/names.json");
284-
fse.copySync("data/names-female.json", "build/gen/names-female.json");
284+
await fse.copy("data/names.json", "build/gen/names.json");
285+
await fse.copy("data/names-female.json", "build/gen/names-female.json");
285286

286-
fse.copySync("node_modules/flag-icons/flags/4x3", "build/img/flags");
287+
await fse.copy("node_modules/flag-icons/flags/4x3", "build/img/flags");
287288
const flagHtaccess = `<IfModule mod_headers.c>
288289
Header set Cache-Control "public,max-age=31536000"
289290
</IfModule>`;
290-
fs.writeFileSync("build/img/flags/.htaccess", flagHtaccess);
291+
await fsp.writeFile("build/img/flags/.htaccess", flagHtaccess);
291292

292293
setSport();
293294
};
@@ -305,9 +306,9 @@ const genRev = () => {
305306
return `${year}.${month}.${day}.${minutes}`;
306307
};
307308

308-
const reset = () => {
309-
fs.rmSync("build", { recursive: true, force: true });
310-
fs.mkdirSync("build/gen", { recursive: true });
309+
const reset = async () => {
310+
await fsp.rm("build", { recursive: true, force: true });
311+
await fsp.mkdir("build/gen", { recursive: true });
311312
};
312313

313314
const setTimestamps = (rev: string, watch: boolean = false) => {

tools/watch/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ const updateError = (filename: string, error: Error) => {
8989
};
9090

9191
// Needs to run first, to create output folder
92-
watchFiles(updateStart, updateEnd, updateError);
92+
await watchFiles(updateStart, updateEnd, updateError);
9393

9494
// Schema is needed for JS bunlde, and watchJSONSchema is async
9595
watchJSONSchema(updateStart, updateEnd, updateError).then(() => {

tools/watch/watchCSS.ts

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
1-
import { watch } from "chokidar";
2-
import { buildCSS } from "../lib/buildFuncs.ts";
1+
import path from "node:path";
2+
import { Worker } from "node:worker_threads";
33

44
const watchCSS = async (
55
updateStart: (filename: string) => void,
66
updateEnd: (filename: string) => void,
77
updateError: (filename: string, error: Error) => void,
88
) => {
9-
const watcher = watch("public/css", {});
9+
const worker = new Worker(
10+
path.join(import.meta.dirname, "watchCSSWorker.ts"),
11+
);
1012

11-
const filenames = ["build/gen/light.css", "build/gen/dark.css"];
12-
13-
const myBuildCSS = async () => {
14-
filenames.map(updateStart);
15-
try {
16-
await buildCSS(true);
17-
filenames.map(updateEnd);
18-
} catch (error) {
19-
for (const filename of filenames) {
20-
updateError(filename, error);
21-
}
13+
worker.on("message", message => {
14+
if (message.type === "start") {
15+
updateStart(message.filename);
2216
}
23-
};
24-
25-
await myBuildCSS();
26-
27-
watcher.on("change", myBuildCSS);
17+
if (message.type === "end") {
18+
updateEnd(message.filename);
19+
}
20+
if (message.type === "error") {
21+
updateError(message.filename, message.error);
22+
}
23+
});
2824
};
2925

3026
export default watchCSS;

tools/watch/watchCSSWorker.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { watch } from "chokidar";
2+
import { parentPort } from "node:worker_threads";
3+
import { buildCSS } from "../lib/buildFuncs.ts";
4+
5+
const filenames = ["build/gen/light.css", "build/gen/dark.css"];
6+
7+
const myBuildCSS = async () => {
8+
for (const filename of filenames) {
9+
parentPort!.postMessage({
10+
type: "start",
11+
filename,
12+
});
13+
}
14+
15+
try {
16+
await buildCSS(true);
17+
for (const filename of filenames) {
18+
parentPort!.postMessage({
19+
type: "end",
20+
filename,
21+
});
22+
}
23+
} catch (error) {
24+
for (const filename of filenames) {
25+
parentPort!.postMessage({
26+
type: "error",
27+
filename,
28+
error,
29+
});
30+
}
31+
}
32+
};
33+
34+
await myBuildCSS();
35+
36+
const watcher = watch("public/css", {});
37+
watcher.on("change", myBuildCSS);

tools/watch/watchFiles.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,18 @@ import { copyFiles, genRev, reset, setTimestamps } from "../lib/buildFuncs.ts";
33

44
// Would be better to only copy individual files on update, but this is fast enough
55

6-
const watchFiles = (
6+
const watchFiles = async (
77
updateStart: (filename: string) => void,
88
updateEnd: (filename: string) => void,
99
updateError: (filename: string, error: Error) => void,
1010
) => {
11-
const watcher = watch(["public", "data", "node_modules/flag-icons"], {});
12-
1311
const outFilename = "static files";
1412

15-
const buildWatchFiles = () => {
13+
const buildWatchFiles = async () => {
1614
try {
1715
updateStart(outFilename);
1816

19-
copyFiles(true);
17+
await copyFiles(true);
2018

2119
const rev = genRev();
2220
setTimestamps(rev, true);
@@ -28,9 +26,10 @@ const watchFiles = (
2826
}
2927
};
3028

31-
reset();
32-
buildWatchFiles();
29+
await reset();
30+
await buildWatchFiles();
3331

32+
const watcher = watch(["public", "data", "node_modules/flag-icons"], {});
3433
watcher.on("change", buildWatchFiles);
3534
};
3635

tools/watch/watchJSONSchema.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ const watchJSONSchema = async (
1717

1818
const sport = getSport();
1919

20-
const watcher = watch("tools/lib/generateJSONSchema.ts", {});
21-
2220
const outFilename = "build/files/league-schema.json";
2321

2422
const buildJSONSchema = async () => {
2523
try {
24+
updateStart(outFilename);
25+
2626
// Dynamically reload generateJSONSchema, cause that's what we're watching!
2727
const generateJSONSchema = await importFresh(
2828
"../lib/generateJSONSchema.ts",
@@ -38,14 +38,10 @@ const watchJSONSchema = async (
3838
}
3939
};
4040

41-
updateStart(outFilename);
42-
43-
watcher.on("change", async () => {
44-
updateStart(outFilename);
45-
await buildJSONSchema();
46-
});
47-
4841
await buildJSONSchema();
42+
43+
const watcher = watch("tools/lib/generateJSONSchema.ts", {});
44+
watcher.on("change", buildJSONSchema);
4945
};
5046

5147
// watchJSONSchema((filename) => console.log('updateStart', filename), (filename) => console.log('updateEnd', filename), (filename, error) => console.log('updateError', filename, error));

0 commit comments

Comments
 (0)