Skip to content

Commit f87aea1

Browse files
authored
build(*): update version scripts (#487)
This pull request introduces a new automated version bumping workflow for the monorepo, replacing the previous `lerna`-based approach with a custom script and updating related configuration. The main changes include the addition of a new script for version management, updates to the CI workflow to use this script, and the removal of explicit version fields from several package manifests to streamline version handling. **Monorepo versioning improvements:** * Added a new script `scripts/version.mjs` to automate version bumps for the workspace root and packages, update transitive dependencies and dev-dependencies, and refresh the lockfile. This script replaces `lerna` for version management and supports all semver bump types. * Updated the `.github/workflows/version-monorepo.yml` workflow to use the new `release:version` npm script for version bumps, and switched environment variable references from `lerna.json` to `package.json`. * Added the `release:version` script to the root `package.json`, invoking the new versioning script. **Package manifest cleanup:** * Removed the explicit `"version"` field from multiple `package.json` files across example, test, and website packages, delegating version management to the workspace tooling. [[1]](diffhunk://#diff-fbd9a08e45065b46e7af624fdab631f5ba1a42efd369864776452f1f1c3d1462L4) [[2]](diffhunk://#diff-4b5616cba814f4c7ca4ea49c77e8456efcc7931852306a3d899597507ad137b6L4) [[3]](diffhunk://#diff-4e07d4465f4aea89f454870fe7bea0a8ca165d3b2ca4ba7b48de814ef242b79aL4) [[4]](diffhunk://#diff-91dcc9f3a55785a33dc03839c1b76f0e835964d86579d0dc314082e94aecfc54L4) [[5]](diffhunk://#diff-77cab3613ec619b938abd65b4a94a82764a5f08c9b65358e4282bfa0120441d2L4) [[6]](diffhunk://#diff-fae242fbf77a8a9d52625664bc36ea12316586f8a716b41137d897a2b7e3df76L4)
1 parent e67bc22 commit f87aea1

File tree

10 files changed

+162
-18
lines changed

10 files changed

+162
-18
lines changed

.github/workflows/version-monorepo.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,18 @@ jobs:
6060
- name: Set up environment variables
6161
run: |
6262
echo "SHORT_SHA=${GITHUB_SHA:0:7}" >> $GITHUB_ENV
63-
echo "OLD_VERSION=$(node -p "require('./lerna.json').version")" >> $GITHUB_ENV
63+
echo "OLD_VERSION=$(node -p "require('./package.json').version")" >> $GITHUB_ENV
6464
65-
- name: Version bump (patch, minor, major) # `--no-git-tag-version` includes `--no-push`
65+
- name: Version bump (patch, minor, major)
6666
if: ${{ inputs.semver == 'patch' || inputs.semver == 'minor' || inputs.semver == 'major' }}
67-
run: npx lerna version ${{ inputs.semver }} --no-git-tag-version --yes
67+
run: npm run release:version -- ${{ inputs.semver }}
6868

69-
- name: Version bump (prerelease, prepatch, preminor, premajor) # `--no-git-tag-version` includes `--no-push`
69+
- name: Version bump (prerelease, prepatch, preminor, premajor)
7070
if: ${{ inputs.semver == 'prerelease' || inputs.semver == 'prepatch' || inputs.semver == 'preminor' || inputs.semver == 'premajor' }}
71-
run: npx lerna version ${{ inputs.semver }} --no-git-tag-version --yes --preid ${{ inputs.preid }}
71+
run: npm run release:version -- ${{ inputs.semver }} ${{ inputs.preid }}
7272

7373
- name: Set up environment variables
74-
run: echo "NEW_VERSION=$(node -p "require('./lerna.json').version")" >> $GITHUB_ENV
74+
run: echo "NEW_VERSION=$(node -p "require('./package.json').version")" >> $GITHUB_ENV
7575

7676
- name: Draft release (patch, minor, major)
7777
if: ${{ inputs.semver == 'patch' || inputs.semver == 'minor' || inputs.semver == 'major' }}

examples/clang-format/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"private": true,
33
"name": "examples-clang-format",
4-
"version": "2.0.2",
54
"scripts": {
65
"formatted:c": "npx clang-format src/formatted.c",
76
"formatted:c:dry-run": "npx clang-format -Werror -n src/formatted.c",

examples/git-clang-format/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"private": true,
33
"name": "examples-git-clang-format",
4-
"version": "2.0.2",
54
"scripts": {
65
"add-a-space-to-line-9-of-main-c-file": "node -e \"require('fs').copyFileSync('src/main_overwrite.txt','src/main.c')\"",
76
"git-add": "git add src/main.c && git status",

package-lock.json

Lines changed: 0 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"scripts": {
1616
"prepare": "husky",
1717
"release:publish": "node -e \"require('child_process').execSync('npm publish -w packages ' + (require('./package.json').version.includes('-') ? '--tag next' : ''), { stdio: 'inherit' })\"",
18+
"release:version": "node ./scripts/version.mjs",
1819
"coverage": "npx c8 --reporter=lcov npm run test",
1920
"test": "npm run test -ws --if-present",
2021
"test:pkg:cfg": "npm run test -w packages/clang-format-git",

scripts/version.mjs

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/**
2+
* @fileoverview Script to bump versions of packages in the monorepo.
3+
* Usage: `node path/to/version.mjs <semver> [preid]`
4+
*/
5+
6+
/* eslint-disable no-console -- CLI */
7+
// @ts-check
8+
9+
// --------------------------------------------------------------------------------
10+
// Import
11+
// --------------------------------------------------------------------------------
12+
13+
import { execSync } from 'node:child_process';
14+
import { styleText } from 'node:util';
15+
16+
// --------------------------------------------------------------------------------
17+
// Helper
18+
// --------------------------------------------------------------------------------
19+
20+
const semver = process.argv[2];
21+
const preid = process.argv[3] ?? '';
22+
23+
/** @param {Buffer<ArrayBufferLike>} buffer @returns {Record<string, any>} */
24+
function stringifyBuffer(buffer) {
25+
return JSON.parse(buffer.toString());
26+
}
27+
28+
/** @param {string} text */
29+
function bgCyan(text) {
30+
return styleText('bgCyan', text);
31+
}
32+
33+
/** @param {string} text */
34+
function cyan(text) {
35+
return styleText('cyan', text);
36+
}
37+
38+
/** @param {string} text */
39+
function green(text) {
40+
return styleText('green', text);
41+
}
42+
43+
/** @param {string} text */
44+
function magenta(text) {
45+
return styleText('magenta', text);
46+
}
47+
48+
// --------------------------------------------------------------------------------
49+
// Script: Bump workspace root and package versions
50+
// --------------------------------------------------------------------------------
51+
52+
console.log(`
53+
${bgCyan('Bump workspace root and package versions')}
54+
55+
> semver: ${cyan(semver)}
56+
> preid: ${cyan(preid)}
57+
`);
58+
59+
execSync(
60+
`npm version ${semver} --preid ${preid} -w packages --no-workspaces-update --include-workspace-root --no-git-tag-version`,
61+
{
62+
stdio: 'inherit',
63+
},
64+
);
65+
66+
console.log(`
67+
${green('Successfully bumped workspace root and package versions')}
68+
`);
69+
70+
// --------------------------------------------------------------------------------
71+
// Script: Bump transitive dependency and dev-dependency versions
72+
// --------------------------------------------------------------------------------
73+
74+
console.log(`
75+
${bgCyan('Bump transitive dependency and dev-dependency versions')}
76+
`);
77+
78+
const packages = stringifyBuffer(execSync('npm pkg get -ws'));
79+
const bumpedPackages = stringifyBuffer(execSync('npm pkg get -w packages'));
80+
81+
/** @type {Map<string, string>} */
82+
const bumpedPackagesMap = new Map(
83+
Object.entries(bumpedPackages).map(([packageName, packageJson]) => [
84+
packageName,
85+
`^${packageJson.version}`,
86+
]),
87+
);
88+
89+
for (const [packageName, packageJson] of Object.entries(packages)) {
90+
console.log('Workspace:', magenta(packageName));
91+
92+
// Step 1: Check dependencies.
93+
if (packageJson.dependencies) {
94+
for (const [depName, oldDepVersion] of Object.entries(packageJson.dependencies)) {
95+
if (!bumpedPackagesMap.has(depName)) continue;
96+
97+
const newDepVersion = String(bumpedPackagesMap.get(depName));
98+
99+
console.log(
100+
'> Bump transitive dependency:',
101+
cyan(depName),
102+
'from',
103+
cyan(oldDepVersion),
104+
'to',
105+
cyan(newDepVersion),
106+
);
107+
108+
execSync(
109+
`npm pkg set dependencies.${depName}="${newDepVersion}" -w ${packageName}`,
110+
);
111+
}
112+
}
113+
114+
// Step 2: Check dev-dependencies.
115+
if (packageJson.devDependencies) {
116+
for (const [depName, oldDepVersion] of Object.entries(packageJson.devDependencies)) {
117+
if (!bumpedPackagesMap.has(depName)) continue;
118+
119+
const newDepVersion = String(bumpedPackagesMap.get(depName));
120+
121+
console.log(
122+
'> Bump transitive dev-dependency:',
123+
cyan(depName),
124+
'from',
125+
cyan(oldDepVersion),
126+
'to',
127+
cyan(newDepVersion),
128+
);
129+
130+
execSync(
131+
`npm pkg set devDependencies.${depName}="${newDepVersion}" -w ${packageName}`,
132+
);
133+
}
134+
}
135+
136+
console.log(); // New line.
137+
}
138+
139+
console.log(`
140+
${green('Successfully bumped transitive dependency and dev-dependency versions')}
141+
`);
142+
143+
// --------------------------------------------------------------------------------
144+
// Script: Run `npm install` to update lockfile
145+
// --------------------------------------------------------------------------------
146+
147+
console.log(`
148+
${bgCyan('Run `npm install` to update lockfile')}
149+
`);
150+
151+
execSync('npm install', { stdio: 'inherit' });
152+
153+
console.log(`
154+
${green('Successfully ran `npm install` to update lockfile')}
155+
`);

tests/integration-api-cjs/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"private": true,
33
"name": "tests-integration-api-cjs",
4-
"version": "2.0.2",
54
"type": "commonjs",
65
"scripts": {
76
"test": "node --test"

tests/integration-api-esm/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"private": true,
33
"name": "tests-integration-api-esm",
4-
"version": "2.0.2",
54
"type": "module",
65
"scripts": {
76
"test": "node --test"

tests/integration-binaries-permission/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"private": true,
33
"name": "tests-integration-binaries-permission",
4-
"version": "2.0.2",
54
"scripts": {
65
"test": "node --test"
76
},

website/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"private": true,
33
"name": "website",
4-
"version": "2.0.2",
54
"type": "module",
65
"scripts": {
76
"dev": "npx vitepress --open --host",

0 commit comments

Comments
 (0)