-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathbump-version.js
More file actions
136 lines (114 loc) · 4.44 KB
/
bump-version.js
File metadata and controls
136 lines (114 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const fs = require("fs");
const path = require("path");
function readJson(filePath) {
return JSON.parse(fs.readFileSync(filePath, "utf-8"));
}
/**
* 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 <newVersion>
*
* 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 workspacePackages = [];
const workspaceNames = new Set();
for (const workspace of workspaces) {
const pkgPath = path.join(rootDir, workspace, "package.json");
const pkg = readJson(pkgPath);
workspaceNames.add(pkg.name);
workspacePackages.push({ pkgPath, pkg });
}
// Apply version bumps
for (const { pkgPath, pkg } of workspacePackages) {
pkg.version = newVersion;
// Update internal workspace dependency versions (exact, no ^)
// This covers dependencies, devDependencies, and peerDependencies
for (const depType of ["dependencies", "devDependencies", "peerDependencies"]) {
if (!pkg[depType]) {
continue;
}
for (const [dep, ver] of Object.entries(pkg[depType])) {
// Update all workspace dependencies to the new exact version,
// matching lerna's --force-publish --exact behavior
if (workspaceNames.has(dep) && !ver.startsWith("workspace:")) {
pkg[depType][dep] = newVersion;
}
}
}
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + "\n");
}
return workspacePackages.length;
}
/**
* The test fixtures have their own pnpm installs with overrides that point to the local tarballs.
* We need to update those overrides to point to the correct version's tarball.
*/
function bumpTestFixtureVersions(rootDir, newVersion) {
const fixturesDir = path.join(rootDir, "packages", "integration-tests-next", "fixtures");
const entries = fs.readdirSync(fixturesDir, { withFileTypes: true });
for (const entry of entries) {
if (!entry.isDirectory()) {
continue;
}
const pkgPath = path.join(fixturesDir, entry.name, "package.json");
if (!fs.existsSync(pkgPath)) {
continue;
}
const pkg = readJson(pkgPath);
if (!pkg.dependencies) {
continue;
}
for (const dep of Object.keys(pkg.dependencies)) {
if (dep.startsWith("@sentry/")) {
pkg.dependencies[dep] = newVersion;
}
}
if (pkg?.pnpm?.overrides) {
for (const dep of Object.keys(pkg.pnpm.overrides)) {
if (dep.startsWith("@sentry/")) {
const orig = pkg.pnpm.overrides[dep];
pkg.pnpm.overrides[dep] = orig.replace(/-\d*\.\d*\..+?\.tgz/, `-${newVersion}.tgz`);
}
}
}
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + "\n");
}
}
// CLI entry point
if (require.main === module) {
const newVersion = process.argv[2];
if (!newVersion) {
console.error("Usage: node scripts/bump-version.js <new-version>");
process.exit(1);
}
const rootDir = path.join(__dirname, "..");
const updatedCount = bumpVersions(rootDir, newVersion);
bumpTestFixtureVersions(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 };