-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathbump-version.js
More file actions
90 lines (75 loc) · 3.17 KB
/
bump-version.js
File metadata and controls
90 lines (75 loc) · 3.17 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
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;
}
// 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);
// 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 };