const fs = require('fs'); const path = require('path'); function readJson(filePath) { return JSON.parse(fs.readFileSync(filePath, 'utf-8')); } function updateWorkspaceDeps(pkgPath, workspaceNames, newVersion, { bumpVersion } = {}) { const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8')); if (bumpVersion) { pkg.version = newVersion; } for (const depType of ['dependencies', 'devDependencies', 'peerDependencies']) { if (!pkg[depType]) continue; for (const [dep, ver] of Object.entries(pkg[depType])) { if (workspaceNames.has(dep) && !ver.startsWith('workspace:')) { pkg[depType][dep] = newVersion; } } } fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n'); return pkg; } function updateTarballVersions(filePath, newVersion) { const content = fs.readFileSync(filePath, 'utf-8'); fs.writeFileSync(filePath, content.replace(/(?<=sentry-[\w-]+-)\d+\.\d+\.\d+(-[\w.]+)?(?=\.tgz)/g, newVersion)); } /** * Bumps the version of all workspace packages and their internal dependencies. * This replicates the behavior of: * lerna version --force-publish --exact --no-git-tag-version --no-push --include-merged-tags --yes * * Specifically: * - Updates `version` in every workspace package.json to newVersion * - Updates all internal workspace dependency references (dependencies, devDependencies, peerDependencies) * to the new exact version (no ^ or ~ prefix), matching lerna's --exact flag * - --force-publish: all packages are updated regardless of whether they changed * - No git tags, commits, or pushes are made */ function bumpVersions(rootDir, newVersion) { const rootPkgPath = path.join(rootDir, 'package.json'); const rootPkg = JSON.parse(fs.readFileSync(rootPkgPath, 'utf-8')); const workspaces = rootPkg.workspaces; if (!workspaces || !Array.isArray(workspaces)) { throw new Error('Could not find workspaces in root package.json'); } // Read all workspace package.json files upfront. // This ensures we fail early if any workspace is unreadable, // before writing any changes (no partial updates). const workspaceNames = new Set(); const workspacePkgPaths = []; for (const workspace of workspaces) { const pkgPath = path.join(rootDir, workspace, 'package.json'); const pkg = readJson(pkgPath); workspaceNames.add(pkg.name); workspacePkgPaths.push(pkgPath); } // Apply version bumps for (const pkgPath of workspacePkgPaths) { updateWorkspaceDeps(pkgPath, workspaceNames, newVersion, { bumpVersion: true }); } // Update bundler-plugin integration test fixtures. // These are standalone pnpm projects (not workspaces) that reference local tarballs // with version-stamped filenames, so they need explicit patching. const fixturesDir = path.join(rootDir, 'dev-packages', 'bundler-plugin-integration-tests', 'fixtures'); if (fs.existsSync(fixturesDir)) { for (const entry of fs.readdirSync(fixturesDir, { withFileTypes: true })) { if (!entry.isDirectory()) continue; const fixturePkgPath = path.join(fixturesDir, entry.name, 'package.json'); if (!fs.existsSync(fixturePkgPath)) continue; updateWorkspaceDeps(fixturePkgPath, workspaceNames, newVersion); const fixtureWorkspacePath = path.join(fixturesDir, entry.name, 'pnpm-workspace.yaml'); if (fs.existsSync(fixtureWorkspacePath)) { updateTarballVersions(fixtureWorkspacePath, newVersion); } } } return workspacePkgPaths.length; } // CLI entry point if (require.main === module) { const newVersion = process.argv[2]; if (!newVersion) { console.error('Usage: node scripts/bump-version.js '); process.exit(1); } const rootDir = path.join(__dirname, '..'); const updatedCount = bumpVersions(rootDir, newVersion); // Write a .version file used by the gitflow sync workflow to detect version bumps const versionFile = { _comment: 'Auto-generated by scripts/bump-version.js. Used by the gitflow sync workflow to detect version bumps. Do not edit manually.', version: newVersion, }; fs.writeFileSync(path.join(rootDir, '.version.json'), JSON.stringify(versionFile, null, 2) + '\n'); console.log(`Updated ${updatedCount} packages to version ${newVersion}`); } module.exports = { bumpVersions };