import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

const scriptDir = path.dirname(fileURLToPath(import.meta.url));
const webRoot = path.resolve(scriptDir, '..');
const nextDir = path.resolve(webRoot, '.next');

if (!nextDir.startsWith(`${webRoot}${path.sep}`)) {
  throw new Error(`Refusing to remove path outside web app: ${nextDir}`);
}

if (fs.existsSync(nextDir)) {
  fs.rmSync(nextDir, { recursive: true, force: true });
  console.log('[web dev] removed stale .next cache');
}
