From 999f4a13b8db5dfaccb2babe7b51f31bba0322b0 Mon Sep 17 00:00:00 2001 From: Tyler Westbrook Date: Fri, 26 Jun 2026 22:21:53 -0500 Subject: [PATCH] Minor fixes --- .github/workflows/release.yml | 6 ++--- scripts/stamp-version.js | 45 ++++++++++++++++++++++++++++++++ tests/unit/stamp-version.test.js | 44 +++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 scripts/stamp-version.js create mode 100644 tests/unit/stamp-version.test.js diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index d1bca2f..08872c6 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -8,7 +8,7 @@ on: workflow_dispatch: inputs: version: - description: 'Release tag, e.g. v0.1.1 (the tag is created at the current commit on this branch)' + description: 'Release tag, e.g. v0.3.2.1 (the tag is created at the current commit on this branch)' required: true type: string prerelease: @@ -42,7 +42,7 @@ jobs: shell: bash env: VERSION: ${{ inputs.version }} - run: npm version "${VERSION#v}" --no-git-tag-version --allow-same-version + run: node scripts/stamp-version.js "${VERSION#v}" - name: Configure Windows code signing shell: bash @@ -86,7 +86,7 @@ jobs: shell: bash env: VERSION: ${{ inputs.version }} - run: npm version "${VERSION#v}" --no-git-tag-version --allow-same-version + run: node scripts/stamp-version.js "${VERSION#v}" - name: Package Linux artifacts (tarball + .deb) shell: bash diff --git a/scripts/stamp-version.js b/scripts/stamp-version.js new file mode 100644 index 0000000..74d6770 --- /dev/null +++ b/scripts/stamp-version.js @@ -0,0 +1,45 @@ +'use strict'; + +const fs = require('node:fs'); +const path = require('node:path'); + +function readJson(file) { + return JSON.parse(fs.readFileSync(file, 'utf8')); +} + +function writeJson(file, value) { + fs.writeFileSync(file, JSON.stringify(value, null, 2) + '\n'); +} + +function stampVersion(rootDir, version) { + if (!version || typeof version !== 'string') { + throw new Error('version is required'); + } + + const pkgPath = path.join(rootDir, 'package.json'); + const pkg = readJson(pkgPath); + pkg.version = version; + writeJson(pkgPath, pkg); + + const lockPath = path.join(rootDir, 'package-lock.json'); + if (!fs.existsSync(lockPath)) return; + + const lock = readJson(lockPath); + lock.version = version; + if (lock.packages && lock.packages['']) { + lock.packages[''].version = version; + } + writeJson(lockPath, lock); +} + +if (require.main === module) { + try { + const version = process.argv[2] || process.env.VERSION; + stampVersion(process.cwd(), version); + } catch (err) { + console.error(err && err.message ? err.message : err); + process.exitCode = 1; + } +} + +module.exports = { stampVersion }; diff --git a/tests/unit/stamp-version.test.js b/tests/unit/stamp-version.test.js new file mode 100644 index 0000000..4fa8fec --- /dev/null +++ b/tests/unit/stamp-version.test.js @@ -0,0 +1,44 @@ +'use strict'; + +const fs = require('node:fs'); +const path = require('node:path'); +const test = require('node:test'); +const assert = require('node:assert/strict'); + +const { makeTmpDir, rmrf } = require('./helpers'); +const { stampVersion } = require('../../scripts/stamp-version'); + +test('stampVersion updates package metadata without semver validation', () => { + const root = makeTmpDir('stamp-version'); + try { + fs.writeFileSync(path.join(root, 'package.json'), JSON.stringify({ + name: 'stepforge', + version: '0.1.0', + private: true, + }, null, 2)); + + fs.writeFileSync(path.join(root, 'package-lock.json'), JSON.stringify({ + name: 'stepforge', + version: '0.1.0', + lockfileVersion: 3, + requires: true, + packages: { + '': { + name: 'stepforge', + version: '0.1.0', + }, + }, + }, null, 2)); + + stampVersion(root, '0.3.2.1'); + + const pkg = JSON.parse(fs.readFileSync(path.join(root, 'package.json'), 'utf8')); + const lock = JSON.parse(fs.readFileSync(path.join(root, 'package-lock.json'), 'utf8')); + + assert.equal(pkg.version, '0.3.2.1'); + assert.equal(lock.version, '0.3.2.1'); + assert.equal(lock.packages[''].version, '0.3.2.1'); + } finally { + rmrf(root); + } +});