Remove cleanOldFiles function and unlink dist directory at build start

This commit is contained in:
Seth 2025-01-17 19:42:19 -05:00
parent e9b90eabc4
commit ae63818fe1
No known key found for this signature in database
GPG key ID: 8B7A2C60CDF65CAC

View file

@ -1,14 +1,10 @@
import { unlink } from "node:fs/promises"; import { unlink } from "node:fs/promises";
import { watch } from "node:fs"; 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 NOWATCH = process.env.NOWATCH === "1";
const build = async () => { const build = async () => {
try { await unlink("dist"); } catch {}
const start = Date.now(); const start = Date.now();
const built = await Bun.build({ const built = await Bun.build({
entrypoints: ["./src/index.html"], entrypoints: ["./src/index.html"],
@ -20,29 +16,10 @@ const build = async () => {
...(NOWATCH ? { minify: true } : {}), ...(NOWATCH ? { minify: true } : {}),
}); });
const end = Date.now(); const end = Date.now();
if (!NOWATCH) await cleanOldFiles(built.outputs);
console.log("Did build", built.success, built.logs); console.log("Did build", built.success, built.logs);
console.log("Build time:", end - start, "ms"); 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) { if (!NOWATCH) {
watch(import.meta.dir, { recursive: true }, async (_, file) => { watch(import.meta.dir, { recursive: true }, async (_, file) => {
if (!file?.startsWith("dist")) return await build(); if (!file?.startsWith("dist")) return await build();