Skip to content

Commit 23c0629

Browse files
committed
chore(clean): remove obsolete package residue
1 parent f62fd60 commit 23c0629

2 files changed

Lines changed: 71 additions & 1 deletion

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"check:paths": "node scripts/fleet/check/paths-are-valid.mts",
4747
"check:test-budget-headroom": "node scripts/fleet/check/test-budget-headroom.mts",
4848
"ci:local": "node scripts/fleet/ci/local/run.mts run --all --quiet --pause-on-failure --github-token",
49-
"clean": "node scripts/fleet/clean.mts",
49+
"clean": "node scripts/repo/clean.mts",
5050
"clean:cache": "node scripts/repo/clean-cache.mts",
5151
"cover": "node scripts/fleet/cover.mts",
5252
"cover:cli": "node scripts/repo/cli-build/test-wrapper.mts --coverage --all",

scripts/repo/clean.mts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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

Comments
 (0)