-
Notifications
You must be signed in to change notification settings - Fork 2
Add Version Bump Script and Auto-Publish CI Job #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Pearce-Ropion
wants to merge
3
commits into
main
Choose a base branch
from
pearce/publish-job
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| name: Check version change | ||
| description: Detect whether the package.json version changed since the previous commit | ||
|
|
||
| outputs: | ||
| changed: | ||
| description: 'true if the version changed since HEAD^' | ||
| value: ${{ steps.check.outputs.changed }} | ||
| current: | ||
| description: 'The current version in package.json' | ||
| value: ${{ steps.check.outputs.current }} | ||
|
|
||
| runs: | ||
| using: composite | ||
| steps: | ||
| - name: Run check | ||
| id: check | ||
| shell: bash | ||
| run: yarn tsx "$GITHUB_ACTION_PATH/check-version-change.ts" |
45 changes: 45 additions & 0 deletions
45
.github/actions/check-version-change/check-version-change.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { execSync } from 'node:child_process'; | ||
| import { readFileSync } from 'node:fs'; | ||
|
|
||
| import * as core from '@actions/core'; | ||
| import semver from 'semver'; | ||
|
|
||
| const readVersion = (json: string, source: string): string => { | ||
| const parsed: unknown = JSON.parse(json); | ||
| if ( | ||
| typeof parsed !== 'object' || | ||
| parsed === null || | ||
| !('version' in parsed) || | ||
| typeof parsed.version !== 'string' | ||
| ) { | ||
| throw new Error(`${source} is missing a string "version" field`); | ||
| } | ||
| const valid = semver.valid(parsed.version); | ||
| if (valid === null) { | ||
| throw new Error(`${source} has invalid semver version "${parsed.version}"`); | ||
| } | ||
| return valid; | ||
| }; | ||
|
|
||
| try { | ||
| const current = readVersion( | ||
| readFileSync('package.json', 'utf8'), | ||
| 'package.json', | ||
| ); | ||
| const previous = readVersion( | ||
| execSync('git show HEAD^:package.json', { encoding: 'utf8' }), | ||
| 'package.json@HEAD^', | ||
| ); | ||
| const changed = !semver.eq(current, previous); | ||
|
|
||
| core.info( | ||
| changed | ||
| ? `Version changed: ${previous} -> ${current}` | ||
| : `Version unchanged (${current}); skipping publish.`, | ||
| ); | ||
|
|
||
| core.setOutput('current', current); | ||
| core.setOutput('changed', changed); | ||
| } catch (error) { | ||
| core.setFailed(error instanceof Error ? error : 'An unknown error occurred'); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| import { readFileSync, writeFileSync } from 'node:fs'; | ||
|
|
||
| import { select } from '@inquirer/prompts'; | ||
| import * as semver from 'semver'; | ||
|
|
||
| type BaseBump = 'patch' | 'minor' | 'major'; | ||
| type Choice = { label: string; next: string }; | ||
|
|
||
| const PACKAGE_JSON_PATH = 'package.json'; | ||
| const PRE_TAG = 'pre'; | ||
|
|
||
| const PREBUMP: Record<BaseBump, semver.ReleaseType> = { | ||
| patch: 'prepatch', | ||
| minor: 'preminor', | ||
| major: 'premajor', | ||
| }; | ||
|
|
||
| const isPrerelease = (parsed: semver.SemVer): boolean => { | ||
| if (parsed.prerelease.length === 0) return false; | ||
| const [tag, counter, ...rest] = parsed.prerelease; | ||
| if (tag !== PRE_TAG || typeof counter !== 'number' || rest.length > 0) { | ||
| throw new Error( | ||
| `Unsupported prerelease "${parsed.version}"; expected x.y.z-${PRE_TAG}.N`, | ||
| ); | ||
| } | ||
| return true; | ||
| }; | ||
|
|
||
| const baseStable = (parsed: semver.SemVer): string => | ||
| `${parsed.major}.${parsed.minor}.${parsed.patch}`; | ||
|
|
||
| const inc = ( | ||
| version: string, | ||
| release: semver.ReleaseType, | ||
| identifier?: typeof PRE_TAG, | ||
| ): string => { | ||
| const result = | ||
| identifier === undefined | ||
| ? semver.inc(version, release) | ||
| : semver.inc(version, release, identifier); | ||
| if (result === null) { | ||
| throw new Error(`semver.inc failed: inc("${version}", "${release}")`); | ||
| } | ||
| return result; | ||
| }; | ||
|
|
||
| const buildChoices = (current: semver.SemVer): Choice[] => { | ||
| const choices: Choice[] = []; | ||
|
|
||
| if (isPrerelease(current)) { | ||
| choices.push({ | ||
| label: 'prerelease', | ||
| next: inc(current.version, 'prerelease'), | ||
| }); | ||
| choices.push({ label: 'release', next: baseStable(current) }); | ||
| } | ||
|
|
||
| const stable = baseStable(current); | ||
| for (const bump of ['patch', 'minor', 'major'] as const) { | ||
| choices.push({ label: bump, next: inc(stable, bump) }); | ||
| choices.push({ | ||
| label: `${bump} (${PRE_TAG})`, | ||
| next: inc(stable, PREBUMP[bump], PRE_TAG), | ||
| }); | ||
| } | ||
|
|
||
| return choices; | ||
| }; | ||
|
|
||
| const promptChoice = async ( | ||
| current: string, | ||
| choices: Choice[], | ||
| ): Promise<Choice> => { | ||
| const labelWidth = Math.max(...choices.map(c => c.label.length)); | ||
| return select({ | ||
| message: `Select a bump (current: ${current}):`, | ||
| choices: choices.map(c => ({ | ||
| name: `${c.label.padEnd(labelWidth)} -> ${c.next}`, | ||
| value: c, | ||
| })), | ||
| }); | ||
| }; | ||
|
|
||
| const readCurrentVersion = (): semver.SemVer => { | ||
| const pkg: unknown = JSON.parse(readFileSync(PACKAGE_JSON_PATH, 'utf8')); | ||
| if ( | ||
| typeof pkg !== 'object' || | ||
| pkg === null || | ||
| !('version' in pkg) || | ||
| typeof pkg.version !== 'string' | ||
| ) { | ||
| throw new Error('package.json is missing a string "version" field'); | ||
| } | ||
| const parsed = semver.parse(pkg.version); | ||
| if (parsed === null) { | ||
| throw new Error(`Invalid semver version in package.json: "${pkg.version}"`); | ||
| } | ||
| return parsed; | ||
| }; | ||
|
|
||
| const writeNewVersion = (version: string): void => { | ||
| const raw = readFileSync(PACKAGE_JSON_PATH, 'utf8'); | ||
| const updated = raw.replace(/("version"\s*:\s*")[^"]+(")/, `$1${version}$2`); | ||
| if (updated === raw) { | ||
| throw new Error('Failed to locate "version" field in package.json'); | ||
| } | ||
| writeFileSync(PACKAGE_JSON_PATH, updated); | ||
| }; | ||
|
|
||
| const main = async (): Promise<void> => { | ||
| const current = readCurrentVersion(); | ||
| const choices = buildChoices(current); | ||
| const choice = await promptChoice(current.version, choices); | ||
| writeNewVersion(choice.next); | ||
| console.log(`Bumped ${current.version} -> ${choice.next}`); | ||
| }; | ||
|
|
||
| main().catch((error: unknown) => { | ||
| if (error instanceof Error && error.name === 'ExitPromptError') { | ||
| process.exit(130); | ||
| } | ||
| console.error(error instanceof Error ? error.message : error); | ||
| process.exit(1); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know it's already gated, but just to be doubly sure, wanna add a double check that we're on
mainhere?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea