52 lines
1.3 KiB
TypeScript
52 lines
1.3 KiB
TypeScript
import { existsSync, readdirSync, rmSync } from "node:fs";
|
|
import { join, resolve } from "node:path";
|
|
|
|
export interface ResetDevCacheOptions {
|
|
rootDir?: string;
|
|
cacheDir?: string;
|
|
enabled?: boolean;
|
|
}
|
|
|
|
function removeLegacyProcessCaches(rootDir: string): void {
|
|
if (!existsSync(rootDir)) return;
|
|
for (const entry of readdirSync(rootDir, { withFileTypes: true })) {
|
|
if (!entry.isDirectory() || !/^\.wrnexus-\d+$/.test(entry.name)) continue;
|
|
rmSync(join(rootDir, entry.name), {
|
|
recursive: true,
|
|
force: true,
|
|
maxRetries: 3,
|
|
retryDelay: 100,
|
|
});
|
|
}
|
|
}
|
|
|
|
export function resetDevCache(options: ResetDevCacheOptions = {}): string {
|
|
const rootDir = resolve(options.rootDir ?? process.cwd());
|
|
|
|
const cacheDir = resolve(rootDir, options.cacheDir ?? ".wrnexus");
|
|
|
|
if (options.enabled === false) {
|
|
return cacheDir;
|
|
}
|
|
|
|
removeLegacyProcessCaches(rootDir);
|
|
|
|
// Safety: never allow deleting the project root.
|
|
if (cacheDir === rootDir) {
|
|
throw new Error("Refusing to remove the project root as the WRNexus cache directory.");
|
|
}
|
|
|
|
if (existsSync(cacheDir)) {
|
|
rmSync(cacheDir, {
|
|
recursive: true,
|
|
force: true,
|
|
maxRetries: 3,
|
|
retryDelay: 100,
|
|
});
|
|
|
|
console.log(`[wrnexus] cleared generated cache: ${cacheDir}`);
|
|
}
|
|
|
|
return cacheDir;
|
|
}
|