summaryrefslogtreecommitdiff
path: root/build.ts
blob: 95e29fd63c36a9d1b298304db928d08dbc57b76c (plain)
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 { BuildOptions, build } from "esbuild";
import { existsSync } from "node:fs";
import { constants, copyFile, mkdir, readdir, rm } from "node:fs/promises";
import { basename, join } from "node:path";

const distDir = "dist";
const contentDir = "static";
const teawieArchiveDir = "Teawie-Archive/teawie-media";
const teawieContentDir = join(contentDir, "teawie");

const checkAndCreate = async (dir: string) => {
	if (!existsSync(dir)) {
		await mkdir(dir, { recursive: true });
	} else {
		for (const f of await readdir(dir, { withFileTypes: true })) {
			await rm(f.path, { recursive: true, force: true });
		}
	}
};

const findWies = async () => {
	// grab directories from archive
	const wieDirs = (await readdir(teawieArchiveDir, { withFileTypes: true }))
		.filter((file) => file.isDirectory())
		.map((file) => join(file.path, file.name));

	// map over directories until we (probably) only have the images we want and their path
	const collected = wieDirs.map(async (dir) =>
		(await readdir(dir, { withFileTypes: true }))
			.filter(async (file) => {
				const fileExt = file.name.split(",").pop() ?? "";
				return file.isFile() && !["ini", "txt"].includes(fileExt);
			})
			.map((file) => join(file.path, file.name)),
	);

	const res = await Promise.all(collected);

	return res.flat();
};

const wies = await findWies();

const define = {
	WIES: JSON.stringify(wies.map((wie) => basename(wie).replaceAll(" ", "%20"))),
	WIE_DIR: JSON.stringify(teawieContentDir),
};

await checkAndCreate(distDir);
await checkAndCreate(join(distDir, contentDir));
await checkAndCreate(join(distDir, teawieContentDir));

for (const f of wies) {
	await copyFile(
		f,
		join(distDir, teawieContentDir, basename(f)),
		constants.COPYFILE_FICLONE,
	);
}

const options = {
	entryPoints: ["src/index.ts"],
	outfile: join(distDir, "_worker.js"),
	define,
	format: "esm",
	bundle: true,
	minify: true,
} satisfies BuildOptions;

await build(options);