import { existsSync, readdirSync, rmSync, statSync } from "node:fs";
import { join } from "node:path";

const outDir = join(process.cwd(), "out");
if (!existsSync(outDir)) process.exit(0);

for (const name of readdirSync(outDir)) {
  if (name.startsWith("_") || name === "_next" || name === "downloads") continue;

  const dirPath = join(outDir, name);
  if (!statSync(dirPath).isDirectory()) continue;

  const htmlPath = join(outDir, `${name}.html`);
  const indexPath = join(dirPath, "index.html");
  if (existsSync(htmlPath) && !existsSync(indexPath)) {
    rmSync(dirPath, { recursive: true, force: true });
  }
}
