From 5de11a91f1d6ff6cee5a4c90666f33a6dd7a4f64 Mon Sep 17 00:00:00 2001 From: seth Date: Sun, 12 Nov 2023 20:07:55 -0500 Subject: feat: use all images from teawie-archive --- build.ts | 57 ++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 19 deletions(-) (limited to 'build.ts') diff --git a/build.ts b/build.ts index 3c6a66c..95e29fd 100644 --- a/build.ts +++ b/build.ts @@ -1,44 +1,63 @@ import { BuildOptions, build } from "esbuild"; import { existsSync } from "node:fs"; import { constants, copyFile, mkdir, readdir, rm } from "node:fs/promises"; -import { join } from "node:path"; +import { basename, join } from "node:path"; const distDir = "dist"; -const contentDir = join(distDir, "static"); -const teawieArchiveDir = "Teawie-Archive/teawie-media/Original Teawies"; +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)) { - await rm(join(dir, f), { recursive: true, force: true }); + for (const f of await readdir(dir, { withFileTypes: true })) { + await rm(f.path, { recursive: true, force: true }); } } }; -await checkAndCreate(distDir); -await checkAndCreate(contentDir); -await checkAndCreate(join(contentDir, "teawie")); +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 wies = (await readdir(teawieArchiveDir)).filter((wie) => { - const fileExt = wie.split(".").pop(); - return !["ini", "txt"].includes(fileExt ?? ""); -}); +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( - join(teawieArchiveDir, f), - join(contentDir, "teawie", f), + f, + join(distDir, teawieContentDir, basename(f)), constants.COPYFILE_FICLONE, ); } -const define = { - WIES: JSON.stringify(wies.map((wie) => wie.replaceAll(" ", "%20"))), - WIE_DIR: JSON.stringify("static/teawie"), -}; - const options = { entryPoints: ["src/index.ts"], outfile: join(distDir, "_worker.js"), -- cgit v1.2.3