|
| 1 | +import { spawn } from "node:child_process"; |
| 2 | +import { appendFileSync } from "node:fs"; |
| 3 | +import { pathToFileURL } from "node:url"; |
| 4 | + |
| 5 | +const REGISTRY = "https://registry.npmjs.org"; |
| 6 | +const PACKAGE_INPUTS = [ |
| 7 | + ["CLEANUP_CLI", "bailian-cli"], |
| 8 | + ["CLEANUP_CORE", "bailian-cli-core"], |
| 9 | + ["CLEANUP_RUNTIME", "bailian-cli-runtime"], |
| 10 | + ["CLEANUP_COMMANDS", "bailian-cli-commands"], |
| 11 | + ["CLEANUP_KSCLI", "knowledge-studio-cli"], |
| 12 | + ["CLEANUP_DSH", "bailian-kb-dsh"], |
| 13 | +]; |
| 14 | + |
| 15 | +function booleanInput(environment, name) { |
| 16 | + const value = environment[name] ?? "true"; |
| 17 | + if (value !== "true" && value !== "false") throw new Error(`${name} must be true or false`); |
| 18 | + return value === "true"; |
| 19 | +} |
| 20 | + |
| 21 | +function validateSelection({ packages, tags, dryRun }) { |
| 22 | + const allowedPackages = new Set(PACKAGE_INPUTS.map(([, packageName]) => packageName)); |
| 23 | + if (!packages.length || packages.some((packageName) => !allowedPackages.has(packageName))) |
| 24 | + throw new Error("Select at least one supported package"); |
| 25 | + if (!tags.length) throw new Error("Enter at least one tag"); |
| 26 | + for (const tag of tags) { |
| 27 | + if (tag === "latest") throw new Error("Deleting latest is forbidden"); |
| 28 | + if (!/^[a-z][a-z0-9-]*$/.test(tag)) throw new Error(`Invalid tag: ${tag}`); |
| 29 | + } |
| 30 | + if (typeof dryRun !== "boolean") throw new Error("dryRun must be a boolean"); |
| 31 | +} |
| 32 | + |
| 33 | +export function parseCleanupInputs(environment = process.env) { |
| 34 | + const selection = { |
| 35 | + packages: PACKAGE_INPUTS.filter(([input]) => booleanInput(environment, input)).map( |
| 36 | + ([, packageName]) => packageName, |
| 37 | + ), |
| 38 | + tags: [...new Set((environment.CLEANUP_TAGS ?? "").split(/[\s,]+/).filter(Boolean))], |
| 39 | + dryRun: booleanInput(environment, "CLEANUP_DRY_RUN"), |
| 40 | + }; |
| 41 | + validateSelection(selection); |
| 42 | + return selection; |
| 43 | +} |
| 44 | + |
| 45 | +async function readRegistryTags(packageName) { |
| 46 | + const response = await fetch(`${REGISTRY}/-/package/${packageName}/dist-tags`, { |
| 47 | + cache: "no-store", |
| 48 | + signal: AbortSignal.timeout(30_000), |
| 49 | + }); |
| 50 | + if (!response.ok) throw new Error(`Cannot read ${packageName} tags: HTTP ${response.status}`); |
| 51 | + const tags = await response.json(); |
| 52 | + if ( |
| 53 | + !tags || |
| 54 | + typeof tags !== "object" || |
| 55 | + Array.isArray(tags) || |
| 56 | + Object.values(tags).some((version) => typeof version !== "string") |
| 57 | + ) |
| 58 | + throw new Error(`Invalid registry tag response for ${packageName}`); |
| 59 | + return tags; |
| 60 | +} |
| 61 | + |
| 62 | +function removeRegistryTag(packageName, tag) { |
| 63 | + // npm >=11.21 exchanges the workflow's OIDC identity; no long-lived token is supplied. |
| 64 | + return new Promise((resolve, reject) => { |
| 65 | + const child = spawn("npm", ["dist-tag", "rm", packageName, tag, "--registry", REGISTRY], { |
| 66 | + stdio: "inherit", |
| 67 | + shell: false, |
| 68 | + timeout: 60_000, |
| 69 | + }); |
| 70 | + child.once("error", reject); |
| 71 | + child.once("exit", (code, signal) => { |
| 72 | + if (code === 0) resolve(); |
| 73 | + else reject(new Error(`npm dist-tag rm ${packageName} ${tag} failed (${code ?? signal})`)); |
| 74 | + }); |
| 75 | + }); |
| 76 | +} |
| 77 | + |
| 78 | +function reportEntry({ packageName, tag, version, status }) { |
| 79 | + const message = `${status}: ${packageName} / ${tag} / ${version ?? "not present"}`; |
| 80 | + console.log(message); |
| 81 | + if (process.env.GITHUB_STEP_SUMMARY) |
| 82 | + appendFileSync(process.env.GITHUB_STEP_SUMMARY, `- ${message}\n`); |
| 83 | +} |
| 84 | + |
| 85 | +export async function cleanupNpmTags( |
| 86 | + selection, |
| 87 | + { readTags = readRegistryTags, removeTag = removeRegistryTag, report = reportEntry } = {}, |
| 88 | +) { |
| 89 | + validateSelection(selection); |
| 90 | + const { packages, tags, dryRun } = selection; |
| 91 | + const plan = []; |
| 92 | + // Read every selected package before any deletion; request failures are not "tag absent". |
| 93 | + for (const packageName of packages) { |
| 94 | + const currentTags = await readTags(packageName); |
| 95 | + for (const tag of tags) |
| 96 | + plan.push({ |
| 97 | + packageName, |
| 98 | + tag, |
| 99 | + version: Object.hasOwn(currentTags, tag) ? currentTags[tag] : null, |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + const results = []; |
| 104 | + for (const entry of plan) { |
| 105 | + let status = entry.version === null ? "absent" : "preview"; |
| 106 | + if (entry.version !== null && !dryRun) { |
| 107 | + const currentTags = await readTags(entry.packageName); |
| 108 | + if (!Object.hasOwn(currentTags, entry.tag)) { |
| 109 | + status = "absent"; |
| 110 | + } else { |
| 111 | + if (currentTags[entry.tag] !== entry.version) |
| 112 | + throw new Error(`Tag changed since preflight: ${entry.packageName} / ${entry.tag}`); |
| 113 | + await removeTag(entry.packageName, entry.tag); |
| 114 | + const afterTags = await readTags(entry.packageName); |
| 115 | + if (Object.hasOwn(afterTags, entry.tag)) |
| 116 | + throw new Error(`Tag still exists after deletion: ${entry.packageName} / ${entry.tag}`); |
| 117 | + status = "deleted"; |
| 118 | + } |
| 119 | + } |
| 120 | + const result = { ...entry, status }; |
| 121 | + results.push(result); |
| 122 | + report(result); |
| 123 | + } |
| 124 | + return results; |
| 125 | +} |
| 126 | + |
| 127 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 128 | + try { |
| 129 | + const selection = parseCleanupInputs(); |
| 130 | + console.log( |
| 131 | + selection.dryRun ? "Preview only; no tags will be deleted." : "Deleting selected tags.", |
| 132 | + ); |
| 133 | + await cleanupNpmTags(selection); |
| 134 | + } catch (error) { |
| 135 | + console.error(error instanceof Error ? error.message : String(error)); |
| 136 | + process.exitCode = 1; |
| 137 | + } |
| 138 | +} |
0 commit comments