From 861c4ffd0a06d9e792bc682da15814d8d3836d24 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Fri, 4 Dec 2026 14:58:14 +0100 Subject: [PATCH] fix(release): publish prereleases to latest for packages without a stable release npm clears a package page README when a version is published to any tag other than latest. Packages with only prereleases now publish to latest and move their channel tag afterwards through trusted publishing, which needs npm 11.21.0 or newer. Co-Authored-By: Claude Opus 5.5 --- scripts/publish-release.test.ts | 54 ++++++++++++++++++++++++++++++++- scripts/publish-release.ts | 33 ++++++++++++++++++-- scripts/release-registry.ts | 13 ++++++++ 3 files changed, 97 insertions(+), 3 deletions(-) diff --git a/scripts/publish-release.test.ts b/scripts/publish-release.test.ts index abb69d0f..d78f0c59 100644 --- a/scripts/publish-release.test.ts +++ b/scripts/publish-release.test.ts @@ -73,6 +73,57 @@ test('a prerelease publishes every package to its channel tag', () => { ) }) +test('a prerelease of packages without a stable release publishes to latest and then tags its channel', () => { + const result = runPublication( + { PUBLISH_TEST_PRERELEASE_LATEST: '11.0.0-alpha.0' }, + '11.0.0-alpha.1', + ) + assert.equal(result.status, 0, result.stderr) + const npm = result.calls.filter((line) => line.startsWith('npm:')) + for (const name of reactNativePackages) + assert.ok( + npm.includes( + `npm:${name}:publish --access public --provenance --tag alpha --workspaces=false --ignore-scripts`, + ), + name, + ) + for (const name of nitroUIPackages) + assert.ok( + npm.includes( + `npm:${name}:publish --access public --provenance --tag latest --workspaces=false --ignore-scripts`, + ), + name, + ) + assert.deepEqual( + npm.filter((line) => line.includes(':dist-tag ')), + nitroUIPackages.map( + (name) => + `npm:${name}:dist-tag add ${name}@11.0.0-alpha.1 alpha --workspaces=false`, + ), + ) + assert.ok( + npm + .slice(-nitroUIPackages.length) + .every((line) => line.includes(':dist-tag ')), + ) +}) + +test('an npm without trusted dist-tag support prevents publishing a prerelease to latest', () => { + const result = runPublication( + { + PUBLISH_TEST_PRERELEASE_LATEST: '11.0.0-alpha.0', + PUBLISH_TEST_NPM_VERSION: '11.19.1', + }, + '11.0.0-alpha.1', + ) + assert.notEqual(result.status, 0) + assert.match(result.stderr, /npm 11\.19\.1 cannot move dist-tags/) + assert.equal( + result.calls.some((line) => line.startsWith('npm:')), + false, + ) +}) + test('a failed TypeORM prepublish build prevents every npm upload', () => { const name = 'react-native-nitro-sqlite-typeorm' const result = runPublication({ PUBLISH_TEST_FAIL_BUILD: name }) @@ -280,6 +331,7 @@ function runPublication( join(bin, command), [ '#!/bin/bash', + 'if [ "$*" = "--version" ]; then echo "${PUBLISH_TEST_NPM_VERSION:-11.21.3}"; exit 0; fi', 'package_name=$(node -p "require(process.cwd() + \'/package.json\').name")', 'printf "%s:%s:%s\\n" "' + command + @@ -315,7 +367,7 @@ function runPublication( " if (name === process.env.PUBLISH_TEST_FAIL_OIDC) return new Response('', { status: 403 })", " return Response.json({ token: 'fixture-exchange' })", ' }', - " if (url.endsWith('/latest')) return Response.json({ version: '0.9.0' })", + " if (url.endsWith('/latest')) return Response.json({ version: process.env.PUBLISH_TEST_PRERELEASE_LATEST && name.startsWith('nitro-sqlite') ? process.env.PUBLISH_TEST_PRERELEASE_LATEST : '0.9.0' })", " if (url.endsWith('/' + process.env.PUBLISH_TEST_VERSION)) {", " const calls = existsSync(process.env.PUBLISH_TEST_LOG) ? readFileSync(process.env.PUBLISH_TEST_LOG, 'utf8') : ''", ' if (name === process.env.PUBLISH_TEST_EXISTING_PACKAGE) return Response.json({ gitHead: process.env.PUBLISH_TEST_EXISTING_COMMIT ?? process.env.PUBLISH_TEST_COMMIT })', diff --git a/scripts/publish-release.ts b/scripts/publish-release.ts index 761a6e72..1a4e386c 100644 --- a/scripts/publish-release.ts +++ b/scripts/publish-release.ts @@ -2,10 +2,15 @@ import { execFileSync, spawnSync } from 'node:child_process' import { readFileSync } from 'node:fs' import { resolve } from 'node:path' import { fileURLToPath } from 'node:url' -import { git, validateReleaseVersions } from './release-plan.ts' +import { + compareVersions, + git, + validateReleaseVersions, +} from './release-plan.ts' import { checkTrustedPublishers, distributionTag, + hasStableRelease, pendingPackages, releasePackages, verifyPublication, @@ -42,6 +47,18 @@ export async function publishRelease(version: string): Promise { const pending = await pendingPackages(version, commit) await checkTrustedPublishers(pending) const tag = await distributionTag(version) + // npm shows a package page README only from a version published to latest, so packages without a stable release publish prereleases there. + const prereleaseOnly = new Set() + if (tag !== 'latest') + for (const name of releasePackages) + if (!(await hasStableRelease(name))) prereleaseOnly.add(name) + const npmVersion = execFileSync('npm', ['--version'], { + encoding: 'utf8', + }).trim() + if (prereleaseOnly.size && compareVersions(npmVersion, '11.21.0') < 0) + throw new Error( + `npm ${npmVersion} cannot move dist-tags with trusted publishing. Use npm 11.21.0 or newer.`, + ) for (const name of pending) { // npm's CLI exchanges GitHub OIDC credentials and records provenance without a publishing token. const result = spawnSync( @@ -52,7 +69,7 @@ export async function publishRelease(version: string): Promise { 'public', '--provenance', '--tag', - tag, + prereleaseOnly.has(name) ? 'latest' : tag, '--workspaces=false', // Every package's prepublishOnly hook ran before the first registry write. '--ignore-scripts', @@ -72,6 +89,18 @@ export async function publishRelease(version: string): Promise { } // Verify after publishing every package, so npm's processing delays overlap. for (const name of pending) await verifyPublication(name, version, commit) + // Retries tag already published versions too. Each package's trusted publisher needs "Allow npm dist-tag". + for (const name of prereleaseOnly) { + const result = spawnSync( + 'npm', + ['dist-tag', 'add', `${name}@${version}`, tag, '--workspaces=false'], + { cwd: packageDirectory(name), stdio: 'inherit' }, + ) + if (result.status !== 0) + throw new Error(`${name}: npm dist-tag ${tag} failed.`, { + cause: result.error, + }) + } } /** Require every published manifest to name this repository and its own directory. diff --git a/scripts/release-registry.ts b/scripts/release-registry.ts index e2614d68..ab0899b9 100644 --- a/scripts/release-registry.ts +++ b/scripts/release-registry.ts @@ -51,6 +51,19 @@ export async function distributionTag(version: string): Promise { return 'latest' } +/** Check whether a package's latest npm tag points to a stable version. + * @param name Package name. + * @returns False when the package has no latest tag or it points to a prerelease. + * @throws On unavailable or malformed npm registry metadata. + */ +export async function hasStableRelease(name: string): Promise { + const latest = await registry(`${name}/latest`, true) + return ( + typeof latest?.version === 'string' && + !prereleaseIdentifiers(latest.version).length + ) +} + /** Find packages that still need publication after checking existing versions. * Existing versions must record the selected release commit in their gitHead metadata. * @returns Unpublished package names, in dependency order.