import fs from "node:fs/promises"; import path from "node:path"; import { fileURLToPath } from "node:url"; const projectRoot = path.resolve( path.dirname(fileURLToPath(import.meta.url)), "..", ); const distDirectory = path.join(projectRoot, "dist"); const manifest = JSON.parse( await fs.readFile(path.join(projectRoot, "package.json"), "utf8"), ); const currentVersion = String(manifest.version || "").trim(); if (!/^\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.-]+)?$/.test(currentVersion)) { throw new Error("package.json contains an invalid release version."); } const entries = await fs .readdir(distDirectory, { withFileTypes: true }) .catch((error) => { if (error.code === "ENOENT") return []; throw error; }); const removed = []; for (const entry of entries) { if (!entry.isFile() || !entry.name.startsWith("ForgeFlow-")) continue; if (entry.name.includes(`-${currentVersion}-`)) continue; await fs.rm(path.join(distDirectory, entry.name), { force: true }); removed.push(entry.name); } if (removed.length) { console.log(`Removed ${removed.length} obsolete dist artifact(s):`); for (const name of removed) console.log(`- ${name}`); } else { console.log( `No ForgeFlow dist artifacts older than ${currentVersion} found.`, ); }