|
| 1 | +import { existsSync } from 'node:fs' |
| 2 | +import path from 'node:path' |
| 3 | + |
| 4 | +import { safeDelete } from '@socketsecurity/lib-stable/fs/safe' |
| 5 | +import { getDefaultLogger } from '@socketsecurity/lib-stable/logger/default' |
| 6 | + |
| 7 | +import { gitSync } from '../fleet/git/exec.mts' |
| 8 | +import { parseCleanArgs, resolveCleanPlan, runClean } from '../fleet/clean.mts' |
| 9 | +import { REPO_ROOT } from '../fleet/paths.mts' |
| 10 | +import { getScriptArgs } from '../fleet/process/script-output.mts' |
| 11 | +import { isMainModule } from '../fleet/process/is-main-module.mts' |
| 12 | +import { runMain } from '../fleet/process/run-main.mts' |
| 13 | + |
| 14 | +import type { ScriptMeta } from '../fleet/process/run-main.mts' |
| 15 | + |
| 16 | +const logger = getDefaultLogger() |
| 17 | +const OBSOLETE_PACKAGES_DIR = path.join(REPO_ROOT, 'packages') |
| 18 | + |
| 19 | +export function trackedObsoletePackageFiles(repoRoot = REPO_ROOT): string[] { |
| 20 | + const result = gitSync(['ls-files', '--', 'packages'], { cwd: repoRoot }) |
| 21 | + return String(result.stdout).split(/\r?\n/).filter(Boolean) |
| 22 | +} |
| 23 | + |
| 24 | +export async function cleanSocketCli( |
| 25 | + options: { |
| 26 | + dryRun?: boolean | undefined |
| 27 | + fleetArgs?: readonly string[] | undefined |
| 28 | + } = {}, |
| 29 | +): Promise<void> { |
| 30 | + const { dryRun = false, fleetArgs = [] } = options |
| 31 | + const tracked = trackedObsoletePackageFiles() |
| 32 | + if (tracked.length) { |
| 33 | + throw new Error( |
| 34 | + `Obsolete packages cleanup refused. Where: ${OBSOLETE_PACKAGES_DIR}. Saw: ${tracked.length} tracked file(s); wanted an untracked flattened-layout residue. Fix: move tracked source into src/ or scripts/repo/ before cleaning.`, |
| 35 | + ) |
| 36 | + } |
| 37 | + const fleetOptions = parseCleanArgs(fleetArgs).options |
| 38 | + const plan = resolveCleanPlan(REPO_ROOT, fleetOptions) |
| 39 | + if (dryRun) { |
| 40 | + if (existsSync(OBSOLETE_PACKAGES_DIR)) { |
| 41 | + logger.log(`Would remove ${OBSOLETE_PACKAGES_DIR}`) |
| 42 | + } |
| 43 | + return |
| 44 | + } |
| 45 | + await runClean(plan) |
| 46 | + if (existsSync(OBSOLETE_PACKAGES_DIR)) { |
| 47 | + await safeDelete(OBSOLETE_PACKAGES_DIR, { |
| 48 | + allowedDirs: [REPO_ROOT], |
| 49 | + recursive: true, |
| 50 | + }) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +async function main(): Promise<void> { |
| 55 | + const args = getScriptArgs() |
| 56 | + await cleanSocketCli({ |
| 57 | + dryRun: args.includes('--dry-run'), |
| 58 | + fleetArgs: args, |
| 59 | + }) |
| 60 | +} |
| 61 | + |
| 62 | +const SCRIPT_META: ScriptMeta = { |
| 63 | + describe: 'removes Socket CLI build output and obsolete package-tree residue', |
| 64 | + help: 'Usage: pnpm run clean [--dry-run] [--cache] [--node-modules] [--all]', |
| 65 | + json: 'native', |
| 66 | +} |
| 67 | + |
| 68 | +if (isMainModule(import.meta.url)) { |
| 69 | + runMain(main, SCRIPT_META) |
| 70 | +} |
0 commit comments