-
-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathbump-version.ts
More file actions
83 lines (68 loc) · 3.21 KB
/
bump-version.ts
File metadata and controls
83 lines (68 loc) · 3.21 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
import * as fs from 'node:fs';
import { glob } from 'glob';
import * as path from 'node:path';
import * as yaml from 'yaml';
import { fileURLToPath } from 'node:url';
const excludes: string[] = [];
const _dirname = typeof __dirname !== 'undefined' ? __dirname : path.dirname(fileURLToPath(import.meta.url));
function getWorkspacePackageJsonFiles(workspaceFile: string): string[] {
const workspaceYaml = fs.readFileSync(workspaceFile, 'utf8');
const workspace = yaml.parse(workspaceYaml) as { packages?: string[] };
if (!workspace.packages) throw new Error('No "packages" key found in pnpm-workspace.yaml');
const files = new Set<string>();
// include all package.json files in the workspace
const rootDir = path.dirname(workspaceFile);
for (const pattern of workspace.packages) {
const matches = glob.sync(path.join(pattern, 'package.json'), {
cwd: rootDir,
absolute: true,
});
matches.filter((f) => !f.includes('node_modules')).forEach((f) => files.add(f));
}
// include root package.json
files.add(path.resolve(_dirname, '../package.json'));
const result = Array.from(files).filter((f) => !excludes.some((e) => f.endsWith(e)));
return result;
}
function incrementVersion(version: string, type: 'patch' | 'minor' = 'patch'): string {
const parts = version.split('.');
if (parts.length !== 3) throw new Error(`Invalid version format: ${version}`);
const [major, minor, patch] = parts.map((p) => parseInt(p, 10));
if (isNaN(major) || isNaN(minor) || isNaN(patch)) {
throw new Error(`Invalid version: ${version}`);
}
if (type === 'minor') {
return `${major}.${minor + 1}.0`;
} else {
return `${major}.${minor}.${patch + 1}`;
}
}
// get version type from command line argument
const versionType = process.argv[2] as 'patch' | 'minor' | undefined;
if (versionType && versionType !== 'patch' && versionType !== 'minor') {
throw new Error(`Invalid version type: ${versionType}. Expected 'patch' or 'minor'.`);
}
// find all package.json files in the workspace
const workspaceFile = path.resolve(_dirname, '../pnpm-workspace.yaml');
const packageFiles = getWorkspacePackageJsonFiles(workspaceFile);
// get version from root package.json
const rootPackageJson = path.resolve(_dirname, '../package.json');
const rootPkg = JSON.parse(fs.readFileSync(rootPackageJson, 'utf8')) as { version?: string };
if (!rootPkg.version) throw new Error('No "version" key found in package.json');
const rootVersion = rootPkg.version;
const newVersion = incrementVersion(rootVersion, versionType || 'patch');
for (const file of packageFiles) {
const content = fs.readFileSync(file, 'utf8');
const pkg = JSON.parse(content) as { version?: string };
if (pkg.version) {
// do a string replace from oldVersion to newVersion
const oldVersion = pkg.version;
const newContent = content.replace(`"version": "${oldVersion}"`, `"version": "${newVersion}"`);
fs.writeFileSync(file, newContent);
console.log(`Updated ${file}: ${oldVersion} -> ${newVersion}`);
}
}
if (process.env.GITHUB_OUTPUT) {
// CI output
fs.appendFileSync(process.env.GITHUB_OUTPUT, `new_version=${newVersion}\n`);
}