doxa-maps/internal/clean.mjs
doxa-maps d5ba21fa76 doxa-maps — portable bundler for embeddable JAMstack maps
Standalone repo that builds embeddable JAMstack map bundles (web components),
composes them into a lazy-loaded SPA via layouts/, and optionally wraps a
Capacitor Android/iOS app — one build, choose your output/CDN. Diátaxis docs.

History reset to a single clean commit; no PII, IPs, or internal terms.
2026-09-10 01:53:07 +00:00

49 lines
2.3 KiB
JavaScript

#!/usr/bin/env node
/**
* clean.mjs — wipe + recreate the default output tree root before a build.
*
* The output folder name is configurable (vite.doxa-maps-build-config.json `name`, default
* "doxa-maps"), so this reads the same config instead of hardcoding the name —
* otherwise renaming the tree would leave a stale folder behind. Runs BEFORE the
* per-bundle vite builds (which emit into public/<name>/); generate-tree.mjs runs after.
*/
import { readFileSync, rmSync, mkdirSync } from 'node:fs'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
const __dirname = dirname(fileURLToPath(import.meta.url))
function config() {
try {
const cfg = JSON.parse(readFileSync(resolve(__dirname, '..', 'vite.doxa-maps-build-config.json'), 'utf8'))
const name = (typeof cfg?.name === 'string' && cfg.name.trim()) ? cfg.name.trim() : 'doxa-maps'
const paths = Array.isArray(cfg?.paths) ? cfg.paths.filter(p => typeof p === 'string' && p.trim()) : []
return { name, paths }
} catch { return { name: 'doxa-maps', paths: [] } }
}
const { name, paths } = config()
// ⭐ BUG FIXED 2026-07-30 — CLEAN EVERY DESTINATION, NOT JUST app/.
// This used to reset ONLY app/<name>/, leaving each `paths` destination untouched.
// Result: a RECURSIVE SELF-NESTING pollution that grew with every build —
// public/doxa-maps/<bundle>/doxa-maps/<bundle>/doxa-maps/...
// because generate-tree copies app/<name>/ into <path>/<name>/, and on the next
// build the (never-cleaned) nested copies were still there to be copied again.
// It shipped 7 duplicate copies of the same 576 KB bundle into git before it was
// caught by a pre-push file-size audit.
// LESSON: a clean step must clear EVERY output destination the build writes to.
// Cleaning only the canonical outDir is a silent, compounding leak.
// The build writes into public/<name>/ directly (public/ is the committed served
// web-root). Only that ONE subfolder is cleaned — never public/ itself, which also
// holds the shell and the pre-generated posters that are committed source.
const roots = [
resolve(__dirname, '..', 'public', name),
...paths.map(p => resolve(__dirname, '..', p, name)),
]
for (const root of roots) {
rmSync(root, { recursive: true, force: true })
mkdirSync(root, { recursive: true })
console.log(`[clean] reset ${root}`)
}