From 09d3f08406c2f2c97b49ddf1d48b20f42829c26a Mon Sep 17 00:00:00 2001 From: YISH Date: Wed, 25 Sep 2024 19:23:14 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=9A=20Improve=20version=20bump?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../workflows/{release.yml => version.yml} | 38 +++++++++++------ package.json | 3 +- version-bump.mjs => version.mjs | 42 ++++++++++++++++--- 3 files changed, 64 insertions(+), 19 deletions(-) rename .github/workflows/{release.yml => version.yml} (77%) rename version-bump.mjs => version.mjs (53%) diff --git a/.github/workflows/release.yml b/.github/workflows/version.yml similarity index 77% rename from .github/workflows/release.yml rename to .github/workflows/version.yml index f48fd93..aa40440 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/version.yml @@ -1,11 +1,14 @@ -name: Release Obsidian plugin +name: Bump Version on: workflow_dispatch: inputs: - version: - description: 'version' + bump: + description: 'Bump version' required: true + type: choice + options: [major, premajor, minor, preminor, patch, prepatch, prerelease] + default: 'patch' env: PLUGIN_NAME: obsidian-enhancing-export @@ -13,6 +16,8 @@ env: jobs: bump: runs-on: ubuntu-latest + outputs: + version: ${{env.version}} steps: - uses: actions/checkout@v4 with: @@ -22,15 +27,21 @@ jobs: uses: actions/setup-node@v4 with: node-version: 20 + - uses: pnpm/action-setup@v4 + with: + version: latest + - name: Install Dependencies + run: pnpm install - name: Create local changes run: | - npm run version ${{ github.event.inputs.version }} + npm run version-bump ${{ github.event.inputs.bump }} + echo "version=$(node version.mjs)" >> "$GITHUB_ENV" - name: Commit files run: | git config --local user.email "github-actions[bot]@users.noreply.github.com" git config --local user.name "github-actions[bot]" - git commit -a -m "🔖 Bump version number to ${{ github.event.inputs.version }}" - git tag -a ${{ github.event.inputs.version }} -m "" + git commit -a -m "🔖 Bump version number to $version" + git tag -a $version -m "" - name: Push changes uses: ad-m/github-push-action@master with: @@ -44,18 +55,19 @@ jobs: steps: - uses: actions/checkout@v4 with: - ref: ${{ github.event.inputs.version }} + ref: ${{ needs.bump.outputs.version }} - name: Use Node.js uses: actions/setup-node@v4 with: - node-version: 18 + node-version: 20 - uses: pnpm/action-setup@v4 with: - version: 9 + version: latest + - name: Install Dependencies + run: pnpm install - name: Build id: build run: | - pnpm install npm run build mkdir ${{ env.PLUGIN_NAME }} cp dist/* ${{ env.PLUGIN_NAME }} @@ -67,10 +79,10 @@ jobs: uses: actions/create-release@v1 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - VERSION: ${{ github.event.inputs.version }} + VERSION: ${{ needs.bump.outputs.version }} with: - tag_name: ${{ github.event.inputs.version }} - release_name: ${{ github.event.inputs.version }} + tag_name: ${{ needs.bump.outputs.version }} + release_name: ${{ needs.bump.outputs.version }} draft: false prerelease: false diff --git a/package.json b/package.json index 87ffa3e..5ef52ae 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "scripts": { "dev": "vite build -w -m development", "build": "vite build", - "version": "node version-bump.mjs", + "version-bump": "node version.mjs bump", + "version": "node version.mjs", "lint": "eslint --ext .ts,.js src", "lint-fix": "eslint --fix --ext .ts,.js src", "format-check": "prettier --check \"src/**/*.ts\"", diff --git a/version-bump.mjs b/version.mjs similarity index 53% rename from version-bump.mjs rename to version.mjs index ce64d97..5f39ab1 100644 --- a/version-bump.mjs +++ b/version.mjs @@ -1,14 +1,46 @@ import { readFileSync, writeFileSync } from 'fs'; import { exec } from 'child_process'; -import process from 'process'; +import console from 'console'; +import process, { exit } from 'process'; +import { parse } from 'semver'; + + + +const ReleaseTypes = ['major', 'premajor', 'minor', 'preminor', 'patch', 'prepatch', 'prerelease']; + +let bump = 'patch'; + +let cmdType = 'version'; + +for (let arg of process.argv.slice(2)) { + arg = arg.toLowerCase(); + if (arg === 'bump') { + cmdType = 'bump'; + continue; + } + if (ReleaseTypes.includes(arg)) { + bump = arg; + break; + } +} const pkg = JSON.parse(readFileSync('package.json', 'utf8')); -const version = process.argv.at(2) ?? pkg.version; -if (version != pkg.version) { - pkg.version = version; +if (cmdType === 'version') { + console.log(pkg.version); + exit(); +} + +let semver = parse(pkg.version); + +if (semver) { + semver = semver.inc(bump); +} + +if (semver?.toString() != pkg.version) { + pkg.version = semver.version; writeFileSync('package.json', `${JSON.stringify(pkg, null, 2)}\n`); exec('git add package.json'); } @@ -26,4 +58,4 @@ let versions = JSON.parse(readFileSync('versions.json', 'utf8')); versions[pkg.version] = minAppVersion; writeFileSync('versions.json', JSON.stringify(versions, null, '\t')); -exec('git add manifest.json versions.json'); +exec('git add manifest.json versions.json'); \ No newline at end of file