fix(dev): consolidate generated cache directories
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { existsSync, rmSync } from "node:fs";
|
||||
import { resolve } from "node:path";
|
||||
import { existsSync, readdirSync, rmSync } from "node:fs";
|
||||
import { join, resolve } from "node:path";
|
||||
|
||||
export interface ResetDevCacheOptions {
|
||||
rootDir?: string;
|
||||
@@ -7,6 +7,19 @@ export interface ResetDevCacheOptions {
|
||||
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());
|
||||
|
||||
@@ -16,6 +29,8 @@ export function resetDevCache(options: ResetDevCacheOptions = {}): string {
|
||||
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.");
|
||||
|
||||
@@ -288,10 +288,9 @@ export async function startServer(opts: ServeOptions): Promise<RunningServer> {
|
||||
|
||||
const pluginContributions = await pluginRunner.contributions();
|
||||
const virtualModules = new Map<string, string>();
|
||||
// A live dev server and a test process can share an app root. Keeping the
|
||||
// generated artifacts in a process-specific directory prevents a test boot
|
||||
// from deleting the running server's client modules.
|
||||
const cacheDir = join(appRoot, `.wrnexus-${process.pid}`);
|
||||
// Keep generated artifacts under the single framework state directory.
|
||||
// Startup clears this cache and removes legacy PID-suffixed cache folders.
|
||||
const cacheDir = join(appRoot, ".wrnexus", "cache");
|
||||
const virtualDir = join(cacheDir, "virtual");
|
||||
mkdirSync(virtualDir, { recursive: true });
|
||||
for (const [index, module] of pluginContributions.virtualModules.entries()) {
|
||||
|
||||
Reference in New Issue
Block a user