Files
Claude ad517965ee CI: publish every push to the staging registry
main → latest (the package.json version, skipped if already published), next → next,
other branches → dev-<branch> as <version>-<tag>.<timestamp> prereleases.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017Qxbkp4tA7G5SCWjzWuxmE
2026-10-23 13:43:51 +00:00

39 lines
1.8 KiB
JavaScript

// Publishes the package for a branch: `main` → `latest`, `next` → `next`, any other branch →
// `dev-<branch>`. Usage: node scripts/publish.mjs <branch> [--dry-run]
//
// `main` publishes the version in package.json as is, and skips if it is already published (bump
// the version to release). Other branches publish a prerelease, `<version>-<tag>.<timestamp>`,
// so every push gets its own version. The registry and its token come from `.npmrc` / npm config.
import { execFileSync } from 'node:child_process'
import { readFileSync, writeFileSync } from 'node:fs'
const [branch, ...flags] = process.argv.slice(2)
if (!branch) throw new Error('Usage: node scripts/publish.mjs <branch> [--dry-run]')
const dryRun = flags.includes('--dry-run')
const npm = (...args) => execFileSync('npm', args, { encoding: 'utf8', stdio: ['ignore', 'pipe', 'inherit'] }).trim()
const pkg = JSON.parse(readFileSync('package.json', 'utf8'))
const base = pkg.version.replace(/\+.*/, '')
const tag = branch === 'main' ? 'latest' : branch === 'next' ? 'next' : `dev-${branch.replace(/[^0-9A-Za-z-]/g, '-')}`
if (tag === 'latest') {
let published = ''
try {
published = npm('view', `${pkg.name}@${base}`, 'version')
} catch {
// Not found (or the package doesn't exist yet): publish it.
}
if (published) {
console.log(`${pkg.name}@${base} is already published; bump the version in package.json to release.`)
process.exit(0)
}
} else {
const timestamp = new Date().toISOString().replace(/\D/g, '')
pkg.version = `${base}${base.includes('-') ? '.' : '-'}${tag}.${timestamp}`
writeFileSync('package.json', `${JSON.stringify(pkg, null, 2)}\n`)
}
console.log(`Publishing ${pkg.name}@${pkg.version} with tag "${tag}"`)
execFileSync('npm', ['publish', '--tag', tag, ...(dryRun ? ['--dry-run'] : [])], { stdio: 'inherit' })