import { resolve } from "node:path"; import { existsSync } from "node:fs"; import { rm } from "node:fs/promises"; import { spawn } from "node:child_process"; // OneDrive marks directories as Windows reparse points, which Next's Node // cleanup may mistake for links. Use native PowerShell for this build cache. const root = resolve("."), target = resolve(root, ".next"); if (target !== resolve(root, ".next") || !target.startsWith(root)) throw new Error("Unsafe build cache target"); if (existsSync(target)) { if (process.platform === "win32") await new Promise((accept, reject) => { const escaped = target.replaceAll("'", "''"); const child = spawn( "powershell.exe", [ "-NoProfile", "-NonInteractive", "-Command", `Remove-Item -LiteralPath '${escaped}' -Recurse -Force -ErrorAction Stop`, ], { stdio: "inherit", windowsHide: true }, ); child.on("exit", (code) => code === 0 ? accept() : reject( new Error( "Could not clear the Next build cache. Stop the dev server first.", ), ), ); child.on("error", reject); }); else await rm(target, { recursive: true, force: true }); }