Skip to content

Commit 9a1f59b

Browse files
authored
chore: update bump version script (#46)
1 parent c7e9057 commit 9a1f59b

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

.github/workflows/bump-version.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@ jobs:
3232
run: pnpm install --frozen-lockfile
3333

3434
- name: Bump version
35-
run: pnpm run bump-version
35+
id: bump
36+
run: pnpm run bump-version >> "$GITHUB_OUTPUT"
3637

3738
- name: Create PR
3839
uses: peter-evans/create-pull-request@v7
3940
with:
40-
commit-message: 'chore: bump version'
41-
title: '[CI] Bump version'
41+
commit-message: 'chore: bump version ${{ steps.bump.outputs.new_version }}'
42+
title: '[CI] Bump version ${{ steps.bump.outputs.new_version }}'
4243
body: Automated changes for bumping version
4344
branch: chore/ci-bump-version
4445
branch-suffix: timestamp

scripts/bump-version.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function getWorkspacePackageJsonFiles(workspaceFile: string): string[] {
1414
cwd: rootDir,
1515
absolute: true,
1616
});
17-
matches.forEach((f) => files.add(f));
17+
matches.filter((f) => !f.includes('node_modules')).forEach((f) => files.add(f));
1818
}
1919
return Array.from(files);
2020
}
@@ -28,16 +28,27 @@ function incrementVersion(version: string): string {
2828
return parts.join('.');
2929
}
3030

31+
// find all package.json files in the workspace
3132
const workspaceFile = path.resolve(__dirname, '../pnpm-workspace.yaml');
3233
const packageFiles = getWorkspacePackageJsonFiles(workspaceFile);
3334

35+
// get version from root package.json
36+
const rootPackageJson = path.resolve(__dirname, '../package.json');
37+
const rootPkg = JSON.parse(fs.readFileSync(rootPackageJson, 'utf8')) as { version?: string };
38+
if (!rootPkg.version) throw new Error('No "version" key found in package.json');
39+
const rootVersion = rootPkg.version;
40+
const newVersion = incrementVersion(rootVersion);
41+
3442
for (const file of packageFiles) {
3543
const content = fs.readFileSync(file, 'utf8');
3644
const pkg = JSON.parse(content) as { version?: string };
3745
if (pkg.version) {
3846
const oldVersion = pkg.version;
39-
pkg.version = incrementVersion(pkg.version);
40-
fs.writeFileSync(file, JSON.stringify(pkg, null, 2) + '\n');
41-
console.log(`Updated ${file}: ${oldVersion} -> ${pkg.version}`);
47+
const newVersion = incrementVersion(pkg.version);
48+
// do a string replace from oldVersion to newVersion
49+
const newContent = content.replace(`"version": "${oldVersion}"`, `"version": "${newVersion}"`);
50+
fs.writeFileSync(file, newContent);
4251
}
4352
}
53+
54+
console.log(`new_version=${newVersion}`);

0 commit comments

Comments
 (0)