|
| 1 | +/** |
| 2 | + * @file Guard against a package.json version pre-bumped MORE than one release |
| 3 | + * ahead of what actually published — the skip-risk state that shipped the |
| 4 | + * 1.4.3 → 1.4.4 gap (package.json pre-bumped to 1.4.3, then the release |
| 5 | + * bumped 1.4.3 → 1.4.4, so 1.4.3 was never published). The release workflow |
| 6 | + * OWNS the bump; the manifest should sit at the last published version (or at |
| 7 | + * most one pending bump / a `-prerelease` hint above it). Reads the |
| 8 | + * registry's `dist-tags.latest` and fails only when the manifest is ahead by |
| 9 | + * more than a single valid bump. Fail-OPEN: no published version (first |
| 10 | + * release / registry unreachable), a private package, or any crash skips |
| 11 | + * rather than false-fails, so a lint/type CI lane offline never trips it. |
| 12 | + * Usage: node scripts/fleet/check/version-is-not-ahead-of-published.mts. |
| 13 | + */ |
| 14 | + |
| 15 | +import { existsSync, readFileSync } from 'node:fs' |
| 16 | +import path from 'node:path' |
| 17 | +import process from 'node:process' |
| 18 | + |
| 19 | +import { getDefaultLogger } from '@socketsecurity/lib-stable/logger/default' |
| 20 | +import { lte } from '@socketsecurity/lib-stable/versions/compare' |
| 21 | + |
| 22 | +import { computeNextVersion } from '../lib/changelog.mts' |
| 23 | +import { REPO_ROOT } from '../paths.mts' |
| 24 | +import { fetchLatestPublishedVersion } from '../publish-infra/npm/registry.mts' |
| 25 | +import { isMainModule } from '../_shared/is-main-module.mts' |
| 26 | + |
| 27 | +const logger = getDefaultLogger() |
| 28 | + |
| 29 | +interface PackageJsonShape { |
| 30 | + name?: string | undefined |
| 31 | + private?: boolean | undefined |
| 32 | + version?: string | undefined |
| 33 | +} |
| 34 | + |
| 35 | +export interface VersionAheadInput { |
| 36 | + manifestVersion: string |
| 37 | + publishedVersion: string | undefined |
| 38 | +} |
| 39 | + |
| 40 | +export interface VersionAheadResult { |
| 41 | + ok: boolean |
| 42 | + reason: string |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Decide whether the manifest version is safely close to the published version. |
| 47 | + * Pure — the test drives it directly. OK when: nothing published yet |
| 48 | + * (fail-open), the manifest is at or behind published (equal/behind is not a |
| 49 | + * skip), or the manifest is EXACTLY one valid bump (patch/minor/major) above |
| 50 | + * published (a single pending release or a `-prerelease` hint). FAIL only when |
| 51 | + * the manifest is ahead by MORE than one release — the versions between it and |
| 52 | + * published get skipped. |
| 53 | + */ |
| 54 | +export function evaluateVersionAhead( |
| 55 | + input: VersionAheadInput, |
| 56 | +): VersionAheadResult { |
| 57 | + const opts = { __proto__: null, ...input } as VersionAheadInput |
| 58 | + const published = opts.publishedVersion |
| 59 | + if (!published) { |
| 60 | + return { |
| 61 | + ok: true, |
| 62 | + reason: |
| 63 | + 'no published version (first release or registry unreachable) — nothing to compare', |
| 64 | + } |
| 65 | + } |
| 66 | + const core = opts.manifestVersion.split('-')[0]!.split('+')[0]! |
| 67 | + if (lte(core, published)) { |
| 68 | + return { |
| 69 | + ok: true, |
| 70 | + reason: `manifest ${core} is at or behind published ${published}`, |
| 71 | + } |
| 72 | + } |
| 73 | + const pending = [ |
| 74 | + computeNextVersion(published, 'patch'), |
| 75 | + computeNextVersion(published, 'minor'), |
| 76 | + computeNextVersion(published, 'major'), |
| 77 | + ] |
| 78 | + if (pending.includes(core)) { |
| 79 | + return { |
| 80 | + ok: true, |
| 81 | + reason: `manifest ${core} is a single pending bump above published ${published}`, |
| 82 | + } |
| 83 | + } |
| 84 | + return { |
| 85 | + ok: false, |
| 86 | + reason: |
| 87 | + `manifest ${core} is more than one release ahead of published ${published} — ` + |
| 88 | + `the version(s) between get skipped (a pre-bump). The release workflow owns ` + |
| 89 | + `the bump; restore package.json to ${published} (or a single pending bump / ` + |
| 90 | + `a ${published}-derived X.Y.Z-prerelease hint).`, |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +async function main(): Promise<void> { |
| 95 | + const pkgPath = path.join(REPO_ROOT, 'package.json') |
| 96 | + if (!existsSync(pkgPath)) { |
| 97 | + return |
| 98 | + } |
| 99 | + let pkg: PackageJsonShape |
| 100 | + try { |
| 101 | + pkg = JSON.parse(readFileSync(pkgPath, 'utf8')) as PackageJsonShape |
| 102 | + } catch { |
| 103 | + return |
| 104 | + } |
| 105 | + if (!pkg.version || !pkg.name || pkg.private === true) { |
| 106 | + return |
| 107 | + } |
| 108 | + const published = await fetchLatestPublishedVersion(pkg.name) |
| 109 | + const result = evaluateVersionAhead({ |
| 110 | + manifestVersion: pkg.version, |
| 111 | + publishedVersion: published, |
| 112 | + }) |
| 113 | + if (!result.ok) { |
| 114 | + logger.fail(`version-is-not-ahead-of-published: ${result.reason}`) |
| 115 | + process.exitCode = 1 |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +if (isMainModule(import.meta.url)) { |
| 120 | + main().catch((e: unknown) => { |
| 121 | + logger.error(e) |
| 122 | + // Fail-open: a crash in the check must not block an otherwise-valid push. |
| 123 | + process.exitCode = 0 |
| 124 | + }) |
| 125 | +} |
0 commit comments