import path from 'node:path' import * as proc from 'node:child_process' import { tmpdir } from 'node:os' import { join } from 'node:path' import { promisify } from 'node:util' import fs, { ensureDir, writeJson, writeJSON } from 'fs-extra' import pMap from 'p-map' import prompts from 'prompts' import { spawnify } from './spawnify' import { LocalBuildCancelled, runLocalBuild } from './release-build' import { ensureNpmAuthentication, isExactVersionPublishedOnNpm, publishPackagesWithAuthProbe, } from './release-publish' import { resolveBetaVersion, resolveCanaryVersion, resolvePublishTag, resolveStableVersion, skipBlockedVersions, } from './release-version' // avoid emitter error process.setMaxListeners(50) process.stderr.setMaxListeners(50) process.stdout.setMaxListeners(50) // --resume would be cool here where it stores the last failed step somewhere and tries resuming const exec = promisify(proc.exec) const execFile = promisify(proc.execFile) export const spawn = proc.spawn // for failed publishes that need to re-run const confirmFinalPublish = process.argv.includes('--confirm-final-publish') const reRun = process.argv.includes('--rerun') const rePublish = reRun || process.argv.includes('--republish') const finish = process.argv.includes('--finish') // convenience flags const skipAll = process.argv.includes('--skip-all') const undocumented = process.argv.includes('--undocumented') const canary = process.argv.includes('--canary') const isRC = process.argv.includes('--rc') const betaVersion = resolveBetaVersion(process.argv.slice(2)) const isBeta = betaVersion !== null const skipVersion = finish || rePublish || process.argv.includes('--skip-version') const releaseMode = process.argv.includes('--major') ? 'major' : process.argv.includes('--patch') ? 'patch' : 'minor' const dirty = finish || rePublish || undocumented || process.argv.includes('--dirty') const skipPublish = process.argv.includes('--skip-publish') const skipTest = canary || finish || rePublish || skipAll || process.argv.includes('--skip-test') || process.argv.includes('--skip-tests') const skipNativeTest = process.argv.includes('--skip-native-test') || process.argv.includes('--skip-native-tests') const skipBuild = finish || skipAll || process.argv.includes('--skip-build') const dryRun = process.argv.includes('--dry-run') const tamaguiGitUser = process.argv.includes('--tamagui-git-user') const isCI = finish || rePublish || undocumented || process.argv.includes('--ci') const skipFinish = canary || rePublish || skipAll || undocumented || process.argv.includes('--skip-finish') const skipPush = process.argv.includes('--skip-push') const curVersion = fs.readJSONSync('./packages/one/package.json').version // Check if current version is an RC (e.g., 1.3.0-rc.1) const rcMatch = curVersion.match(/^(\d+\.\d+\.\d+)-rc\.(\d+)$/) const isCurrentRC = !!rcMatch const currentRCBase = rcMatch ? rcMatch[1] : null const currentRCNumber = rcMatch ? Number.parseInt(rcMatch[2], 10) : 0 const nextVersion = (() => { if (isBeta) { return betaVersion } if (canary) { return resolveCanaryVersion(curVersion, { rePublish, baseVersion: '2.0.0' }) } if (rePublish) { return curVersion } // RC mode: bump existing RC or will prompt for new RC version if (isRC) { if (isCurrentRC) { // Already an RC, bump the RC number return `${currentRCBase}-rc.${currentRCNumber + 1}` } // Not an RC yet, return placeholder - will be set via prompt return null } return resolveStableVersion(curVersion, { mode: releaseMode, skipVersion }) })() if (!skipVersion) { console.info('Current:', curVersion) if (isRC) { if (isCurrentRC) { console.info(`RC mode: bumping RC ${currentRCNumber} → ${currentRCNumber + 1}`) } else { console.info('RC mode: will prompt for version to RC') } } console.info('') } else { console.info(`Releasing ${curVersion}`) } async function upgradeReproApp(newVersion: string) { const pkgJsonPath = path.join(process.cwd(), 'repro', 'package.json') const pkgJson = await fs.readJSON(pkgJsonPath) pkgJson.version = newVersion pkgJson.dependencies.one = newVersion await writeJSON(pkgJsonPath, pkgJson, { spaces: 2 }) } async function getWorkspacePackages() { const rootPkg = await fs.readJSON('package.json') const workspaceGlobs = rootPkg.workspaces || [] const packages: { name: string; location: string; publishable: boolean }[] = [] for (const pattern of workspaceGlobs) { const baseDir = pattern.replace(/\/\*$/, '').replace(/^\.\//, '') try { const entries = await fs.readdir(baseDir, { withFileTypes: true }) for (const entry of entries) { if (!entry.isDirectory()) continue const pkgPath = path.join(baseDir, entry.name, 'package.json') try { const pkg = await fs.readJSON(pkgPath) if (pkg.name) { packages.push({ name: pkg.name, location: path.join(baseDir, entry.name), publishable: !pkg.private && !pkg.skipPublish, }) } } catch { // Skip directories without package.json } } } catch { // Skip patterns that don't resolve } } return packages } async function run() { try { let version = curVersion // ensure we are up to date // ensure we are on main (skip for prereleases) if (!canary && !isRC && !isBeta && !process.env.CI) { if ((await exec(`git rev-parse --abbrev-ref HEAD`)).stdout.trim() !== 'main') { throw new Error(`Not on main`) } if (!dirty && !rePublish && !finish) { await spawnify(`git pull --rebase origin main`) } } const packagePaths = await getWorkspacePackages() const allPackageJsons = ( await Promise.all( packagePaths .filter((i) => i.location !== '.' && !i.name.startsWith('@takeout')) .flatMap(async ({ name, location }) => { const cwd = path.join(process.cwd(), location) const json = await fs.readJSON(path.join(cwd, 'package.json')) const item = { name, cwd, json, path: path.join(cwd, 'package.json'), directory: location, } if (json.alsoPublishAs) { console.info( ` ${name}: Also publishing as ${json.alsoPublishAs.join(', ')}` ) return [ item, ...json.alsoPublishAs.map((name: string) => ({ ...item, json: { ...json, name }, name, })), ] } return [item] }) ) ) .flat() .filter((x) => !x.json['skipPublish']) const packageJsons = allPackageJsons .filter((x) => { return !x.json.private }) // slow things last .sort((a, b) => { if (a.name.includes('font-') || a.name.includes('-icons')) { return 1 } return -1 }) if (!finish && !skipPublish && !dryRun) { await ensureNpmAuthentication({ env: process.env, whoami: async () => { await spawnify(`npm whoami`) }, login: async () => { console.info('npm is not authenticated. Opening npm login in the browser...') await spawnify(`npm login`, { interactive: true }) }, }) } if (!finish) { console.info( `Publishing in order:\n\n${packageJsons.map((x) => x.name).join('\n')}` ) } async function checkDistDirs() { await Promise.all( packageJsons.map(async ({ cwd, json }) => { const distDir = join(cwd, 'dist') if (!json.scripts || json.scripts.build === 'true') { return } if (!(await fs.pathExists(distDir))) { console.warn('no dist dir!', distDir) process.exit(1) } }) ) } if (tamaguiGitUser) { await spawnify(`git config --global user.name 'Tamagui'`) await spawnify(`git config --global user.email 'tamagui@users.noreply.github.com`) } if (!finish) { let answer: { version: string } if (isCI || skipVersion) { answer = { version: nextVersion! } } else if (isRC && !isCurrentRC) { // New RC - prompt for which version to RC const baseVersion = curVersion.replace(/-.*$/, '') // strip any existing prerelease const [major, minor, patch] = baseVersion.split('.').map(Number) const rcChoices = [ { title: `${major}.${minor + 1}.0-rc.1 (next minor)`, value: `${major}.${minor + 1}.0-rc.1`, }, { title: `${major}.${minor}.${patch + 1}-rc.1 (next patch)`, value: `${major}.${minor}.${patch + 1}-rc.1`, }, { title: `${major + 1}.0.0-rc.1 (next major)`, value: `${major + 1}.0.0-rc.1` }, ] const rcAnswer = await prompts({ type: 'select', name: 'version', message: 'Which version to release as RC?', choices: rcChoices, }) answer = rcAnswer } else { answer = await prompts({ type: 'text', name: 'version', message: 'Version?', initial: nextVersion, }) } version = skipVersion || isBeta || canary || isRC ? answer.version : skipBlockedVersions(answer.version, releaseMode) if (!skipVersion && !isBeta && !canary && !isRC) { while ( ( await pMap( packageJsons, ({ name }) => isExactVersionPublishedOnNpm({ name, version, attempt: 0 }), { concurrency: 8 } ) ).some(Boolean) ) { console.info( `${version} is already used by a release package; choosing the next patch` ) version = resolveStableVersion(version, { mode: 'patch' }) } } console.info('Next:', version, '\n') } console.info('install and build') if (!rePublish && !finish && !canary) { // frozen so we publish exactly what's locked, never a silently-resolved drift await spawnify(`bun install --frozen-lockfile`) } // security gate: never publish with a known high-severity advisory in the tree. // ci uses the same root audit script. if (!finish && !canary) { console.info('run security audit') await spawnify(`bun run audit`) } // run quick checks first to fail fast if (!finish && !skipTest) { console.info('run checks') await spawnify(`bun run lint`) await spawnify(`bun run check`) await spawnify(`bun run typecheck`) } if (!skipBuild && !finish) { if (canary) { await runLocalBuild(packageJsons.map(({ name }) => name)) } else { await spawnify('bun run build') } await checkDistDirs() } // run tests after build if (!finish && !skipTest) { console.info('run tests') await spawnify(`bun run test`) if (!skipNativeTest) { await spawnify(`bun run test-ios`) } } if (!dirty && !dryRun && !rePublish) { const out = await exec(`git status --porcelain`) if (out.stdout) { throw new Error(`Has unsaved git changes: ${out.stdout}`) } } if (!skipVersion && !finish && !canary) { await Promise.all( allPackageJsons.map(async ({ json, path }) => { // Skip packages that opt out of version bumping (e.g., test containers for native build caching) if (json.skipVersion) { return } const next = { ...json } next.version = version for (const field of [ 'dependencies', 'devDependencies', 'optionalDependencies', 'peerDependencies', ]) { const nextDeps = next[field] if (!nextDeps) continue for (const depName in nextDeps) { if (!nextDeps[depName].startsWith('workspace:')) { if (allPackageJsons.some((p) => p.name === depName)) { nextDeps[depName] = version } } } } await writeJSON(path, next, { spaces: 2 }) }) ) await upgradeReproApp(version) } if (!finish && dryRun) { console.info(`Dry run, exiting before publish`) return } if (!finish && !rePublish) { await spawnify(`git diff`) } if (!isCI) { const { confirmed } = await prompts({ type: 'confirm', name: 'confirmed', message: 'Ready to publish?', }) if (!confirmed) { process.exit(0) } } if (!finish && !skipPublish) { if (confirmFinalPublish) { const { confirmed } = await prompts({ type: 'confirm', name: 'confirmed', message: 'Ready to publish?', }) if (!confirmed) { console.info(`Not confirmed, can re-run with --republish to try again`) process.exit(0) } } } if (!finish && !skipPublish) { const tmpDir = await fs.mkdtemp(join(tmpdir(), 'one-publish-')) const publishTag = resolvePublishTag(version, { canary }) const publishOptions = [publishTag && `--tag ${publishTag}`] .filter(Boolean) .join(' ') const prepareOne = async ({ name, cwd }: { name: string; cwd: string }) => { // Copy to temp directory and replace workspace:* with versions const tmpPackageDir = join(tmpDir, name.replace('/', '_')) await fs.copy(cwd, tmpPackageDir, { filter: (src) => { // exclude node_modules to avoid symlink issues return !src.includes('node_modules') }, }) // replace workspace:* with version in temp copy const pkgJsonPath = join(tmpPackageDir, 'package.json') const pkgJson = await fs.readJSON(pkgJsonPath) pkgJson.version = version pkgJson.releaseSourceCommit = ( await execFile('git', ['rev-parse', 'HEAD']) ).stdout.trim() pkgJson.repository = { type: 'git', url: 'git+https://github.com/onejs/one.git', directory: path.relative(process.cwd(), cwd), } for (const field of [ 'dependencies', 'devDependencies', 'optionalDependencies', 'peerDependencies', ]) { if (!pkgJson[field]) continue for (const depName in pkgJson[field]) { if ( pkgJson[field][depName].startsWith('workspace:') || packageJsons.some((pkg) => pkg.name === depName) ) { pkgJson[field][depName] = version } } } await writeJSON(pkgJsonPath, pkgJson, { spaces: 2 }) return path.relative(tmpDir, tmpPackageDir) } const publishResult = await publishPackagesWithAuthProbe({ packages: packageJsons, isEntry: ({ name }) => name === 'one' || name === 'create-vxrn', isPublished: ({ name }) => isExactVersionPublishedOnNpm({ name, version, attempt: 0 }), verifyPublished: ({ name }, attempt) => isExactVersionPublishedOnNpm({ name, version, attempt }), publish: async (pending) => { const workspaces = await pMap(pending, prepareOne, { concurrency: 8 }) await writeJSON( join(tmpDir, 'package.json'), { name: 'one-release', private: true, workspaces, }, { spaces: 2 } ) if (canary && process.env.GITHUB_ACTIONS === 'true') { await pMap( workspaces, async (workspace) => { const result = await execFile( 'npm', [ 'publish', '--ignore-scripts', '--access', 'public', '--tag', 'canary', ], { cwd: join(tmpDir, workspace), maxBuffer: 10 * 1024 * 1024 } ) console.info(result.stdout) }, { concurrency: 6 } ) return } const webAuthCache = join(process.cwd(), 'scripts/cache-npm-webauth.cjs') const nodeOptions = [process.env.NODE_OPTIONS, `--require=${webAuthCache}`] .filter(Boolean) .join(' ') const publishCommand = [ 'npm publish --workspaces --ignore-scripts --access public', publishOptions, '--quiet', ] .filter(Boolean) .join(' ') console.info(`Publishing ${pending.map((pkg) => pkg.name).join(', ')}`) await spawnify(publishCommand, { cwd: tmpDir, env: { ...process.env, NODE_OPTIONS: nodeOptions }, interactive: true, }) }, }) if (publishResult.failed.length > 0) { throw new Error( `${publishResult.failed.length} of ${publishResult.published.length + publishResult.failed.length} packages are not on the registry at ${version}, either dropped after npm reported success or held back until those are there:\n${publishResult.failed.join('\n')}\n\nRe-run with --republish to retry.` ) } console.info(`✅ Published\n`) // restore package.json files for undocumented releases if (undocumented) { console.info('Restoring package.json files...') await spawnify(`git checkout -- **/package.json`) console.info(`✅ Restored package.json files (undocumented release)\n`) } } if (!skipFinish) { // then git tag, commit, push if (!finish) { await spawnify(`bun install`) } const tagPrefix = canary ? 'canary' : 'v' const gitTag = `${tagPrefix}${version}` await finishAndCommit() async function finishAndCommit(cwd = process.cwd()) { if (!rePublish || reRun || finish) { // stage only what a release actually changes. `git add -A` swept an // unrelated agent's uncommitted edit into two release commits in this // shared checkout before it was caught, and a release commit that // carries someone else's half-finished work is very hard to notice // afterwards. every v* commit in this repo's history touches nothing // but package.json files and the lockfile. await spawnify(`git add -- **/package.json package.json bun.lock`, { cwd }) await spawnify( `git commit -m ${gitTag} -- **/package.json package.json bun.lock`, { cwd, allowFail: finish, } ) if (!canary) { if (!dirty) { // pull once more before pushing so if there was a push in interim we get it await spawnify(`git pull --rebase origin HEAD`, { cwd }) } } await spawnify(`git tag ${gitTag}`, { cwd, allowFail: finish }) if (!canary && !skipPush) { await spawnify(`git push origin head`, { cwd, allowFail: finish }) await spawnify(`git push origin ${gitTag}`, { cwd }) } console.info( `✅ ${canary || skipPush ? 'Versioned locally' : 'Pushed and versioned'}\n` ) } } // console.info(`All done, cleanup up in...`) // await sleep(2 * 1000) // // then remove old prepub tag // await pMap( // packageJsons, // async ({ name, cwd }) => { // await spawnify(`npm dist-tag remove ${name}@${version} prepub`, { // cwd, // }).catch((err) => console.error(err)) // }, // { // concurrency: 20, // } // ) } console.info(`✅ Done\n`) } catch (err) { console.info('\nError:\n', err) process.exit(1) } } // --into : quick local release, packs each package and unpacks into target node_modules const intoIdx = process.argv.indexOf('--into') if (intoIdx !== -1) { const reuseUnchangedFiles = async (previous: string, replacement: string) => { if (!(await fs.pathExists(previous)) || !(await fs.lstat(previous)).isDirectory()) return for (const entry of await fs.readdir(replacement, { withFileTypes: true })) { const oldPath = join(previous, entry.name) const nextPath = join(replacement, entry.name) if (entry.isDirectory()) { await reuseUnchangedFiles(oldPath, nextPath) } else if (entry.isFile() && (await fs.pathExists(oldPath))) { const old = await fs.lstat(oldPath) const next = await fs.stat(nextPath) if ( old.isFile() && old.size === next.size && old.mode === next.mode && (await fs.readFile(oldPath)).equals(await fs.readFile(nextPath)) ) { await fs.unlink(nextPath) await fs.link(oldPath, nextPath) } } } } const targetArg = process.argv[intoIdx + 1] if (!targetArg || targetArg.startsWith('--')) { console.error('Missing directory argument for --into') process.exit(1) } const targetDir = path.resolve(targetArg.replace(/^~/, process.env.HOME!)) ;(async () => { const packages = (await getWorkspacePackages()).filter((pkg) => pkg.publishable) const targetPackages: Array<{ name: string location: string destDirs: string[] }> = [] const targetModulesDir = await fs.realpath(join(targetDir, 'node_modules')) const byName = new Map(packages.map((pkg) => [pkg.name, pkg])) const selected = new Set() const queue: string[] = [] const copies = new Map() const visit = async (modulesDir: string): Promise => { if (!(await fs.pathExists(modulesDir))) return for (const { name } of packages) { const dir = join(modulesDir, name) if (!(await fs.pathExists(join(dir, 'package.json')))) continue const resolvedDir = await fs.realpath(dir) const relativeDir = path.relative(targetModulesDir, resolvedDir) // retain package-manager links inside the consumer; never overwrite an external checkout. const destDir = relativeDir !== '..' && !relativeDir.startsWith(`..${path.sep}`) && !path.isAbsolute(relativeDir) ? resolvedDir : dir const found = copies.get(name) ?? [] if (!found.includes(destDir)) found.push(destDir) copies.set(name, found) } for (const entry of await fs.readdir(modulesDir, { withFileTypes: true })) { if (!entry.isDirectory()) continue const dir = join(modulesDir, entry.name) if (entry.name === '.bun' || entry.name === '.pnpm') { for (const stored of await fs.readdir(dir, { withFileTypes: true })) { if (stored.isDirectory()) await visit(join(dir, stored.name, 'node_modules')) } } else if (entry.name.startsWith('.')) { continue } else if (entry.name.startsWith('@')) { for (const scoped of await fs.readdir(dir, { withFileTypes: true })) { if (scoped.isDirectory()) await visit(join(dir, scoped.name, 'node_modules')) } } else { await visit(join(dir, 'node_modules')) } } } await visit(join(targetDir, 'node_modules')) for (const name of copies.keys()) { selected.add(name) queue.push(name) } while (queue.length > 0) { const pkg = byName.get(queue.pop()!)! const manifest = await fs.readJSON(join(pkg.location, 'package.json')) for (const name of Object.keys({ ...manifest.dependencies, ...manifest.optionalDependencies, })) { if (!byName.has(name) || selected.has(name)) continue selected.add(name) queue.push(name) } } for (const name of selected) { const pkg = byName.get(name)! targetPackages.push({ ...pkg, destDirs: copies.get(name) ?? [join(targetDir, 'node_modules', name)], }) } if (targetPackages.length === 0) { throw new Error('No upstream packages are installed in the target') } console.info( `Installing ${targetPackages.length} local packages:\n${targetPackages .map((pkg) => ` ${pkg.name}`) .join('\n')}` ) const tmpDir = await fs.mkdtemp(join(tmpdir(), 'one-release-into-')) const installationDirs: string[] = [] try { if (!skipBuild) { await runLocalBuild(targetPackages.map(({ name }) => name)) } let released = 0 const failures: string[] = [] await pMap( targetPackages, async ({ name, location, destDirs }, index) => { const cwd = path.resolve(location) try { const packageTmpDir = join(tmpDir, `package-${index}`) await ensureDir(packageTmpDir) await execFile( 'bun', ['pm', 'pack', '--ignore-scripts', '--destination', packageTmpDir], { cwd } ) const packedFiles = await fs.readdir(packageTmpDir) if (packedFiles.length !== 1 || !packedFiles[0].endsWith('.tgz')) { throw new Error(`npm pack produced ${packedFiles.length} files`) } const actualTgz = join(packageTmpDir, packedFiles[0]) const stagedDir = join(tmpDir, `staged-${index}`) await ensureDir(stagedDir) await execFile('tar', [ '-xzf', actualTgz, '-C', stagedDir, '--strip-components=1', ]) const manifestPath = join(stagedDir, 'package.json') const manifest = await fs.readJSON(manifestPath) for (const field of [ 'dependencies', 'optionalDependencies', 'peerDependencies', ]) { for (const name of Object.keys(manifest[field] ?? {})) { if (!manifest[field][name].startsWith('workspace:')) continue const dependency = byName.get(name) if (!dependency) throw new Error(`Unknown workspace dependency ${name}`) const dependencyManifest = await fs.readJSON( join(dependency.location, 'package.json') ) manifest[field][name] = dependencyManifest.version } } await writeJSON(manifestPath, manifest, { spaces: 2 }) for (const destDir of destDirs) { await ensureDir(path.dirname(destDir)) const copyDir = await fs.mkdtemp(join(path.dirname(destDir), '.one-release-')) installationDirs.push(copyDir) await fs.copy(stagedDir, copyDir) await reuseUnchangedFiles(destDir, copyDir) // preserve unrelated nested dependencies; external symlinks are replaced if ( (await fs.pathExists(destDir)) && !(await fs.lstat(destDir)).isSymbolicLink() ) { const nestedModules = join(destDir, 'node_modules') if (await fs.pathExists(nestedModules)) { await fs.move(nestedModules, join(copyDir, 'node_modules')) } } await fs.remove(destDir) await fs.move(copyDir, destDir) } released++ console.info(` ✓ ${name}`) } catch (err) { const message = err instanceof Error ? err.message : String(err) failures.push(`${name}: ${message}`) console.warn(` ✗ ${name}: ${message}`) } }, { concurrency: 1 } ) if (failures.length > 0 || released !== targetPackages.length) { throw new Error( `Local install failed for ${failures.length} of ${targetPackages.length} packages:\n${failures.join('\n')}` ) } console.info(`\n✅ Released ${released} packages into ${targetDir}`) } finally { await Promise.all(installationDirs.map((directory) => fs.remove(directory))) await fs.remove(tmpDir) } })() .then(() => process.exit(0)) .catch((err) => { console.error(err) process.exit(err instanceof LocalBuildCancelled ? err.exitCode : 1) }) } else { run() }