From ae63818fe1a84865453d2563560b1b7294177585 Mon Sep 17 00:00:00 2001 From: wont-stream Date: Fri, 17 Jan 2025 19:42:19 -0500 Subject: [PATCH] Remove cleanOldFiles function and unlink dist directory at build start --- index.ts | 25 +------------------------ 1 file changed, 1 insertion(+), 24 deletions(-) diff --git a/index.ts b/index.ts index f26b717..82ade5d 100644 --- a/index.ts +++ b/index.ts @@ -1,14 +1,10 @@ import { unlink } from "node:fs/promises"; import { watch } from "node:fs"; -import { Glob } from "bun"; -import type { BuildArtifact } from "bun"; -import { join } from "node:path"; - -const glob = new Glob("**/*"); const NOWATCH = process.env.NOWATCH === "1"; const build = async () => { + try { await unlink("dist"); } catch {} const start = Date.now(); const built = await Bun.build({ entrypoints: ["./src/index.html"], @@ -20,29 +16,10 @@ const build = async () => { ...(NOWATCH ? { minify: true } : {}), }); const end = Date.now(); - if (!NOWATCH) await cleanOldFiles(built.outputs); console.log("Did build", built.success, built.logs); console.log("Build time:", end - start, "ms"); }; -const cleanOldFiles = async (outputs: BuildArtifact[]) => { - let distFiles: string[] = []; - for await (const file of glob.scan("./dist")) { - distFiles.push(file); - } - - for await (const output of outputs) { - const arr = output.path.split("\\"); - const file = arr[arr.length - 1]; - - distFiles = distFiles.filter((item) => item !== file); - } - - return await Promise.all( - Array.from(distFiles).map((file) => unlink(join("dist", file))), - ); -}; - if (!NOWATCH) { watch(import.meta.dir, { recursive: true }, async (_, file) => { if (!file?.startsWith("dist")) return await build();