Files
shared/scripts/media/clean-test-artifacts.mjs
T

154 lines
4.6 KiB
JavaScript

#!/usr/bin/env node
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const repoRoot = path.resolve(__dirname, '..', '..');
// After a test run we want certain scratch roots removed entirely.
// NOTE: These are repo-root directories.
const DELETE_DIRS = [
'avatars',
'thumbnails',
'downloads',
'logs',
'output',
'input',
'loras',
'copy',
'database',
];
// Some roots are expected to remain present but empty.
// Keep this list lean to avoid surprising destructive behavior.
const EMPTY_DIRS = [
// (intentionally empty)
];
function rimrafChildren(dir) {
if (!fs.existsSync(dir)) return { skipped: true };
let removed = 0;
for (const entry of fs.readdirSync(dir)) {
const full = path.join(dir, entry);
try {
fs.rmSync(full, { recursive: true, force: true });
removed++;
} catch (e) {
console.error('[CLEAN] Failed removing', full, e.message);
}
}
return { removed };
}
function rimrafDir(dir) {
if (!fs.existsSync(dir)) return { skipped: true };
try {
fs.rmSync(dir, { recursive: true, force: true });
return { deleted: true };
} catch (e) {
console.error('[CLEAN] Failed removing dir', dir, e.message);
return { deleted: false, error: e.message };
}
}
function ensureDir(dir) {
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
}
const results = [];
for (const rel of EMPTY_DIRS) {
const abs = path.join(repoRoot, rel);
ensureDir(abs);
const res = rimrafChildren(abs);
results.push({ dir: rel, mode: 'empty', ...res });
}
const deleted = [];
for (const rel of DELETE_DIRS) {
const abs = path.join(repoRoot, rel);
const res = rimrafDir(abs);
deleted.push({ dir: rel, mode: 'delete', ...res });
}
function removeFallbackDirs(baseDir) {
let removed = 0;
if (!fs.existsSync(baseDir)) return removed;
for (const entry of fs.readdirSync(baseDir)) {
if (entry.startsWith('tmp-fallback-')) {
const full = path.join(baseDir, entry);
try {
fs.rmSync(full, { recursive: true, force: true });
removed++;
} catch (e) {
console.error('[CLEAN] Failed removing fallback dir', full, e.message);
}
}
}
return removed;
}
// Remove tmp-fallback-* directories from known roots.
// Tests can create these in repo root.
let fallbackRemoved = 0;
fallbackRemoved += removeFallbackDirs(repoRoot);
// Sweep tests/ folder for orphaned ephemeral Postgres artifacts left behind by crashed runs
// or CWD-relative path.resolve('tests').
// Pattern: shared-media-embedded-pg-* dirs, shared-media-test-postgres-*.json state files.
// NOTE: no top-level tests/ dir currently exists and no embedded-postgres JS test tooling was
// found anywhere in this repo — this sweep is a harmless no-op today but kept for forward
// compatibility / parity with the source script. The former sweep of a nested shared/tests/
// was dropped: after the webui was folded directly into the shared repo (no more
// vendored shared/ copy), there is no longer a nested "shared" subdirectory to sweep.
const pgSweepDirs = [path.join(repoRoot, 'tests')];
let pgArtifactsRemoved = 0;
for (const sweepDir of pgSweepDirs) {
try {
for (const name of fs.readdirSync(sweepDir)) {
if (
name.startsWith('shared-media-embedded-pg-') ||
(name.startsWith('shared-media-test-postgres-') &&
(name.endsWith('.json') || name.endsWith('.json.lock'))) ||
name.startsWith('media-analyzer-test-')
) {
const full = path.join(sweepDir, name);
try {
fs.rmSync(full, { recursive: true, force: true });
pgArtifactsRemoved++;
} catch (e) {
console.error('[CLEAN] Failed removing artifact', full, e.message);
}
}
}
// If sweepDir is outside the canonical tests/ dir and now empty, remove it entirely.
if (sweepDir !== path.join(repoRoot, 'tests')) {
try {
const remaining = fs.readdirSync(sweepDir);
if (remaining.length === 0) {
fs.rmdirSync(sweepDir);
}
} catch {
/* ignore */
}
}
} catch {
/* ignore scan errors */
}
}
const summary = { ok: true, cleaned: results, fallbackRemoved, pgArtifactsRemoved };
summary.deleted = deleted;
console.log(JSON.stringify(summary, null, 2));
// Windows occasionally reports a native module handle crash (exit code 3221225477) after
// synchronous fs.rmSync bursts. Yield event loop once and exit 0 explicitly to stabilize CI.
setTimeout(() => {
try {
process.exitCode = 0; // be explicit
} catch {
/* noop */
}
}, 10);