forked from rrweb-io/rrweb
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathbump-version.js
More file actions
88 lines (74 loc) · 2.82 KB
/
bump-version.js
File metadata and controls
88 lines (74 loc) · 2.82 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
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.
* Replicates the behavior of:
* lerna version --force-publish --exact --no-git-tag-version --no-push --yes <newVersion>
*
* - Updates `version` in every workspace package.json to newVersion
* - Updates all internal workspace dependency references to the new exact version
* - No git tags, commits, or pushes are made
*/
function bumpVersions(rootDir, newVersion) {
const rootPkgPath = path.join(rootDir, 'package.json');
const rootPkg = readJson(rootPkgPath);
const workspaceGlobs = rootPkg.workspaces;
if (!workspaceGlobs || !Array.isArray(workspaceGlobs)) {
throw new Error('Could not find workspaces array in root package.json');
}
// Resolve workspace globs to actual directories
const workspaceDirs = [];
for (const glob of workspaceGlobs) {
// Handle simple globs like "packages/*" and "packages/plugins/*"
const baseDir = glob.replace(/\/\*$/, '');
const fullBase = path.join(rootDir, baseDir);
if (!fs.existsSync(fullBase)) continue;
const entries = fs.readdirSync(fullBase, { withFileTypes: true });
for (const entry of entries) {
if (entry.isDirectory()) {
const pkgJsonPath = path.join(fullBase, entry.name, 'package.json');
if (fs.existsSync(pkgJsonPath)) {
workspaceDirs.push(path.join(baseDir, entry.name));
}
}
}
}
// Read all workspace package.json files upfront to fail early
const workspacePackages = [];
const workspaceNames = new Set();
for (const dir of workspaceDirs) {
const pkgPath = path.join(rootDir, dir, '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;
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 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);
console.log(`Updated ${updatedCount} packages to version ${newVersion}`);
}
module.exports = { bumpVersions };