|
| 1 | +const fs = require("fs"); |
| 2 | +const path = require("path"); |
| 3 | + |
| 4 | +function readJson(filePath) { |
| 5 | + return JSON.parse(fs.readFileSync(filePath, "utf-8")); |
| 6 | +} |
| 7 | + |
| 8 | +/** |
| 9 | + * Bumps the version of all workspace packages and their internal dependencies. |
| 10 | + * This replicates the behavior of: |
| 11 | + * lerna version --force-publish --exact --no-git-tag-version --no-push --include-merged-tags --yes <newVersion> |
| 12 | + * |
| 13 | + * Specifically: |
| 14 | + * - Updates `version` in every workspace package.json to newVersion |
| 15 | + * - Updates all internal workspace dependency references (dependencies, devDependencies, peerDependencies) |
| 16 | + * to the new exact version (no ^ or ~ prefix), matching lerna's --exact flag |
| 17 | + * - --force-publish: all packages are updated regardless of whether they changed |
| 18 | + * - No git tags, commits, or pushes are made |
| 19 | + */ |
| 20 | +function bumpVersions(rootDir, newVersion) { |
| 21 | + const rootPkgPath = path.join(rootDir, "package.json"); |
| 22 | + const rootPkg = JSON.parse(fs.readFileSync(rootPkgPath, "utf-8")); |
| 23 | + const workspaces = rootPkg.workspaces; |
| 24 | + |
| 25 | + if (!workspaces || !Array.isArray(workspaces)) { |
| 26 | + throw new Error("Could not find workspaces in root package.json"); |
| 27 | + } |
| 28 | + |
| 29 | + // Read all workspace package.json files upfront. |
| 30 | + // This ensures we fail early if any workspace is unreadable, |
| 31 | + // before writing any changes (no partial updates). |
| 32 | + const workspacePackages = []; |
| 33 | + const workspaceNames = new Set(); |
| 34 | + for (const workspace of workspaces) { |
| 35 | + const pkgPath = path.join(rootDir, workspace, "package.json"); |
| 36 | + const pkg = readJson(pkgPath); |
| 37 | + workspaceNames.add(pkg.name); |
| 38 | + workspacePackages.push({ pkgPath, pkg }); |
| 39 | + } |
| 40 | + |
| 41 | + // Apply version bumps |
| 42 | + for (const { pkgPath, pkg } of workspacePackages) { |
| 43 | + pkg.version = newVersion; |
| 44 | + |
| 45 | + // Update internal workspace dependency versions (exact, no ^) |
| 46 | + // This covers dependencies, devDependencies, and peerDependencies |
| 47 | + for (const depType of ["dependencies", "devDependencies", "peerDependencies"]) { |
| 48 | + if (!pkg[depType]) { |
| 49 | + continue; |
| 50 | + } |
| 51 | + |
| 52 | + for (const [dep, ver] of Object.entries(pkg[depType])) { |
| 53 | + // Update all workspace dependencies to the new exact version, |
| 54 | + // matching lerna's --force-publish --exact behavior |
| 55 | + if (workspaceNames.has(dep) && !ver.startsWith("workspace:")) { |
| 56 | + pkg[depType][dep] = newVersion; |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + "\n"); |
| 62 | + } |
| 63 | + |
| 64 | + return workspacePackages.length; |
| 65 | +} |
| 66 | + |
| 67 | +// CLI entry point |
| 68 | +if (require.main === module) { |
| 69 | + const newVersion = process.argv[2]; |
| 70 | + |
| 71 | + if (!newVersion) { |
| 72 | + console.error("Usage: node scripts/bump-version.js <new-version>"); |
| 73 | + process.exit(1); |
| 74 | + } |
| 75 | + |
| 76 | + const rootDir = path.join(__dirname, ".."); |
| 77 | + const updatedCount = bumpVersions(rootDir, newVersion); |
| 78 | + |
| 79 | + // Write a .version file used by the gitflow sync workflow to detect version bumps |
| 80 | + const versionFile = { |
| 81 | + _comment: |
| 82 | + "Auto-generated by scripts/bump-version.js. Used by the gitflow sync workflow to detect version bumps. Do not edit manually.", |
| 83 | + version: newVersion, |
| 84 | + }; |
| 85 | + fs.writeFileSync( |
| 86 | + path.join(rootDir, ".version.json"), |
| 87 | + JSON.stringify(versionFile, null, 2) + "\n" |
| 88 | + ); |
| 89 | + |
| 90 | + console.log(`Updated ${updatedCount} packages to version ${newVersion}`); |
| 91 | +} |
| 92 | + |
| 93 | +module.exports = { bumpVersions }; |
0 commit comments