// Publishes the package for a branch: `main` → `latest`, `next` → `next`, any other branch → // `dev-`. Usage: node scripts/publish.mjs [--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, `-.`, // 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 [--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' })