From 153786f4289ecd1ff4ebb4cebf8a235b79a509b7 Mon Sep 17 00:00:00 2001 From: skie1997 Date: Thu, 25 Jun 2026 14:11:07 +0800 Subject: [PATCH] fix: publish script bug --- .github/workflows/hotfix.yml | 3 + .github/workflows/pre-release.yml | 3 + .github/workflows/release.yml | 3 + common/scripts/pre-release.js | 40 ++++++------- common/scripts/release.js | 69 +++++++++------------- common/scripts/validate-release-version.js | 58 ++++++++++++++++++ 6 files changed, 114 insertions(+), 62 deletions(-) create mode 100644 common/scripts/validate-release-version.js diff --git a/.github/workflows/hotfix.yml b/.github/workflows/hotfix.yml index a65c3e4..6bcbbc7 100644 --- a/.github/workflows/hotfix.yml +++ b/.github/workflows/hotfix.yml @@ -51,6 +51,9 @@ jobs: - name: Update version run: node common/scripts/install-run-rush.js version --bump + - name: Validate hotfix versions + run: node common/scripts/validate-release-version.js ${{ steps.semver_parser.outputs.full }} + - name: Build packages run: node common/scripts/install-run-rush.js build --only tag:package diff --git a/.github/workflows/pre-release.yml b/.github/workflows/pre-release.yml index 3e3a4e0..c5ae117 100644 --- a/.github/workflows/pre-release.yml +++ b/.github/workflows/pre-release.yml @@ -43,6 +43,9 @@ jobs: - name: Apply prereleaseName run: node common/scripts/apply-release-version.js ${{ steps.semver_parser.outputs.pre_release_name }} ${{ steps.semver_parser.outputs.main }} + - name: Validate prerelease versions + run: node common/scripts/validate-release-version.js ${{ steps.semver_parser.outputs.full }} + - name: Build packages run: node common/scripts/install-run-rush.js build --only tag:package diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index b7ca82e..99c3382 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -54,6 +54,9 @@ jobs: - name: Update version run: node common/scripts/apply-release-version.js 'none' ${{ steps.semver_parser.outputs.main }} + - name: Validate release versions + run: node common/scripts/validate-release-version.js ${{ steps.semver_parser.outputs.main }} + - name: Build packages run: node common/scripts/install-run-rush.js build --only tag:package diff --git a/common/scripts/pre-release.js b/common/scripts/pre-release.js index 64f324d..3272b3e 100644 --- a/common/scripts/pre-release.js +++ b/common/scripts/pre-release.js @@ -8,12 +8,23 @@ const path = require('path') const checkAndUpdateNextBump = require('./version-policies'); const getPackageJson = require('./get-package-json'); const writePrereleaseVersion = require('./set-prerelease-version'); +const validateReleaseVersion = path.join(__dirname, './validate-release-version.js'); const semverRegex = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-(alpha|beta|rc)(?:\.(?:(0|[1-9])))*)$/; const preReleaseNameReg = /^((alpha|beta|rc)(?:\.(?:0|[1-9]))*)$/; +function runCommand(command) { + const res = spawnSync('sh', ['-c', command], { + stdio: 'inherit', + shell: false, + }); + + if (res.status !== 0) { + process.exit(res.status || 1); + } +} function run() { let preReleaseName = process.argv.slice(2)[0]; @@ -54,41 +65,28 @@ function run() { if (preReleaseName && preReleaseType) { // 1. apply version and update version of package.json - writePrereleaseVersion(checkAndUpdateNextBump(process.argv.slice(2)[1]), preReleaseName) + writePrereleaseVersion(checkAndUpdateNextBump(process.argv.slice(2)[1]), null, preReleaseName) + + runCommand(`node ${validateReleaseVersion}`); // 2. build all the packages - spawnSync('sh', ['-c', `rush build --only tag:package`], { - stdio: 'inherit', - shell: false, - }); + runCommand(`rush build --only tag:package`); // 3. publish to npm - spawnSync('sh', ['-c', `rush publish --publish --include-all --tag ${preReleaseType}`], { - stdio: 'inherit', - shell: false, - }); + runCommand(`rush publish --publish --include-all --tag ${preReleaseType} --set-access-level public`); // 4. update version of local packages to shrinkwrap - spawnSync('sh', ['-c', `rush update`], { - stdio: 'inherit', - shell: false, - }); + runCommand(`rush update`); if (package) { const pkgJsonPath = path.join(__dirname, '../../', package.projectFolder, 'package.json'); const pkgJson = getPackageJson(pkgJsonPath) // 5. add the the changes - spawnSync('sh', ['-c', `git add --all`], { - stdio: 'inherit', - shell: false, - }); + runCommand(`git add --all`); // 6. commit all the changes - spawnSync('sh', ['-c', `git commit -m "build: prerelease version ${pkgJson.version}"`], { - stdio: 'inherit', - shell: false, - }); + runCommand(`git commit -m "build: prerelease version ${pkgJson.version}"`); } } } diff --git a/common/scripts/release.js b/common/scripts/release.js index 2d1cbf3..d2e74c4 100644 --- a/common/scripts/release.js +++ b/common/scripts/release.js @@ -1,80 +1,67 @@ /** - * prelease - * node release.js [alpha.0] [patch | major | minor | 1.0.0] + * release + * node release.js [patch | major | minor | 1.0.0] */ const { spawnSync } = require('child_process') -const fs = require('fs') const path = require('path') const checkAndUpdateNextBump = require('./version-policies'); +const getPackageJson = require('./get-package-json'); +const validateReleaseVersion = path.join(__dirname, './validate-release-version.js'); -function getPackageJson(pkgJsonPath) { - const pkgJson = fs.readFileSync(pkgJsonPath, { encoding: 'utf-8' }) - return JSON.parse(pkgJson) +function runCommand(command) { + const res = spawnSync('sh', ['-c', command], { + stdio: 'inherit', + shell: false, + }); + + if (res.status !== 0) { + process.exit(res.status || 1); + } } +function getExpectedVersion(releaseVersion) { + return /^\d+\.\d+\.\d+$/.test(releaseVersion || '') ? releaseVersion : ''; +} function run() { let releaseVersion = process.argv.slice(2)[0]; - const cwd = process.cwd(); // 0. update `nextBump` checkAndUpdateNextBump(releaseVersion); // 1. update version of package.json, this operation will remove the common/changes - spawnSync('sh', ['-c', `rush version --bump`], { - stdio: 'inherit', - shell: false, - }); + runCommand(`rush version --bump`); + + runCommand(`node ${validateReleaseVersion} ${getExpectedVersion(releaseVersion)}`); // 2. build all the packages - spawnSync('sh', ['-c', `rush build --only tag:package`], { - stdio: 'inherit', - shell: false, - }); + runCommand(`rush build --only tag:package`); // 3. publish to npm - spawnSync('sh', ['-c', 'rush publish --publish --include-all'], { - stdio: 'inherit', - shell: false, - }); + runCommand('rush publish --publish --include-all --set-access-level public'); // 4. update version of local packages to shrinkwrap - spawnSync('sh', ['-c', `rush update`], { - stdio: 'inherit', - shell: false, - }); + runCommand(`rush update`); const rushJson = getPackageJson(path.join(__dirname, '../../rush.json')); - const package = rushJson.projects.find((project) => project.name === '@visactor/vchart-theme'); + const project = rushJson.projects.find((item) => item.packageName === '@visactor/vchart-theme'); - if (package) { + if (project) { const pkgJsonPath = path.join(__dirname, '../../', project.projectFolder, 'package.json') const pkgJson = getPackageJson(pkgJsonPath) // 5. add tag - spawnSync('sh', ['-c', `git tag v${pkgJson.versopn}`], { - stdio: 'inherit', - shell: false, - }); + runCommand(`git tag v${pkgJson.version}`); // 6. add all the changes - spawnSync('sh', ['-c', `git add --all`], { - stdio: 'inherit', - shell: false, - }); + runCommand(`git add --all`); // 7. commit all the changes - spawnSync('sh', ['-c', `git commit -m "build: publish version ${pkgJson.version}"`], { - stdio: 'inherit', - shell: false, - }); + runCommand(`git commit -m "build: publish version ${pkgJson.version}"`); // 8. push tag to origin - spawnSync('sh', ['-c', `git push origin v${pkgJson.version}`], { - stdio: 'inherit', - shell: false, - }); + runCommand(`git push origin v${pkgJson.version}`); } } diff --git a/common/scripts/validate-release-version.js b/common/scripts/validate-release-version.js new file mode 100644 index 0000000..c559140 --- /dev/null +++ b/common/scripts/validate-release-version.js @@ -0,0 +1,58 @@ +const path = require('path'); +const getPackageJson = require('./get-package-json'); + +function fail(message) { + console.error(`\x1b[31m[release version error]\x1b[0m ${message}`); + process.exitCode = 1; +} + +function run() { + const expectedVersion = process.argv.slice(2)[0]; + const rushJson = getPackageJson(path.join(__dirname, '../../rush.json')); + const projects = rushJson.projects || []; + const publishProjects = projects.filter(project => project.shouldPublish); + const mainProject = publishProjects.find(project => project.packageName === '@visactor/vchart-theme'); + + if (!mainProject) { + fail('Cannot find @visactor/vchart-theme in rush.json.'); + return; + } + + const mainPkgJson = getPackageJson(path.join(__dirname, '../../', mainProject.projectFolder, 'package.json')); + const releaseVersion = expectedVersion || mainPkgJson.version; + const publishPackages = publishProjects.map(project => project.packageName); + + publishProjects.forEach(project => { + const pkgJsonPath = path.join(__dirname, '../../', project.projectFolder, 'package.json'); + const pkgJson = getPackageJson(pkgJsonPath); + + if (pkgJson.version !== releaseVersion) { + fail(`${project.packageName} version is ${pkgJson.version}, expected ${releaseVersion}.`); + } + + ['dependencies', 'devDependencies'].forEach(depType => { + const deps = pkgJson[depType]; + + if (!deps) { + return; + } + + publishPackages.forEach(packageName => { + const depVersion = deps[packageName]; + const expectedDepVersion = `workspace:${releaseVersion}`; + + if (depVersion && depVersion !== expectedDepVersion) { + fail(`${project.packageName} ${depType}.${packageName} is ${depVersion}, expected ${expectedDepVersion}.`); + } + }); + }); + }); + + if (process.exitCode) { + return; + } + + console.log(`All publish package versions are aligned at ${releaseVersion}.`); +} + +run();