From d12939c1b9599bb37a3d03fa25126a62b5aa0738 Mon Sep 17 00:00:00 2001 From: camcam-lemon Date: Mon, 2 Dec 2024 11:18:28 +0900 Subject: [PATCH] fix: change to update version.js from scripts Changed it because it is easier and more testable than doing it from github actions --- .github/workflows/release-publish.yml | 5 ----- package.json | 1 + scripts/version.js | 24 ++++++++++++++++++++++++ 3 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 scripts/version.js diff --git a/.github/workflows/release-publish.yml b/.github/workflows/release-publish.yml index 85d37209..f07a2d19 100644 --- a/.github/workflows/release-publish.yml +++ b/.github/workflows/release-publish.yml @@ -43,11 +43,6 @@ jobs: - name: Prepare release run: npm version --no-git-tag-version --allow-same-version ${{env.RELEASE_VERSION}} - - name: Update version file - run: | - echo "export const version = '${{env.RELEASE_VERSION}}'" > src/version.js - git add src/version.js - - name: Convert repository name to lower case run: echo "GITHUB_REPOSITORY_LOWER_CASE=$(echo $GITHUB_REPOSITORY | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV diff --git a/package.json b/package.json index d879480b..3de1d76f 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "prepare": "tsc && vite build", "prepack": "tsc && vite build", "test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" npx jest", + "version": "node scripts/version.js", "lint": "standard", "lint-fix": "standard --fix" }, diff --git a/scripts/version.js b/scripts/version.js new file mode 100644 index 00000000..fd4a08f9 --- /dev/null +++ b/scripts/version.js @@ -0,0 +1,24 @@ +#!/usr/bin/env node + +/** + * This script reads the version from package.json and writes it to src/version.js. + * It also stages the changes to src/version.js in git. + * + * Should be used as an [`npm version` script](https://docs.npmjs.com/cli/v8/using-npm/scripts#npm-version)! + */ +import { execFile } from 'node:child_process' +import { readFile, writeFile } from 'node:fs/promises' +import { promisify } from 'node:util' + +async function main () { + const packageJson = JSON.parse(await readFile('package.json', 'utf8')) + const version = packageJson.version + + await writeFile('src/version.js', `export const version = '${version}'\n`) + await promisify(execFile)('git', ['add', 'src/version.js']) +} + +main().catch((error) => { + console.error(error) + process.exit(1) +})