diff --git a/.github/workflows/bump-version.yml b/.github/workflows/bump-version.yml index 8b5dc61c..d18b6ce5 100644 --- a/.github/workflows/bump-version.yml +++ b/.github/workflows/bump-version.yml @@ -2,6 +2,14 @@ name: Bump Version on: workflow_dispatch: + inputs: + version_type: + description: 'Version type to bump' + required: true + type: choice + options: + - patch + - minor permissions: contents: write @@ -34,7 +42,7 @@ jobs: - name: Bump version id: bump - run: npx tsx scripts/bump-version.ts + run: npx tsx scripts/bump-version.ts ${{ inputs.version_type }} - name: Create PR uses: peter-evans/create-pull-request@v7 diff --git a/package.json b/package.json index 616ccf97..377ec5e3 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,8 @@ "format": "prettier --write \"**/*.{ts,tsx,md}\"", "pr": "gh pr create --fill-first --base dev", "merge-main": "gh pr create --title \"merge dev to main\" --body \"\" --base main --head dev", - "bump-version": "gh workflow run .github/workflows/bump-version.yml --ref dev", + "bump-patch": "gh workflow run .github/workflows/bump-version.yml --ref dev -f version_type=patch", + "bump-minor": "gh workflow run .github/workflows/bump-version.yml --ref dev -f version_type=minor", "publish-all": "pnpm --filter \"./packages/**\" -r publish --access public", "publish-preview": "pnpm --filter \"./packages/**\" -r publish --force --registry https://preview.registry.zenstack.dev/", "unpublish-preview": "pnpm --filter \"./packages/**\" -r --shell-mode exec -- npm unpublish -f --registry https://preview.registry.zenstack.dev/ \"\\$PNPM_PACKAGE_NAME\"" diff --git a/scripts/bump-version.ts b/scripts/bump-version.ts index 5fdda3fd..204c4a38 100644 --- a/scripts/bump-version.ts +++ b/scripts/bump-version.ts @@ -32,13 +32,26 @@ function getWorkspacePackageJsonFiles(workspaceFile: string): string[] { return result; } -function incrementVersion(version: string): string { +function incrementVersion(version: string, type: 'patch' | 'minor' = 'patch'): string { const parts = version.split('.'); - const last = parts.length - 1; - const lastNum = parseInt(parts[last], 10); - if (isNaN(lastNum)) throw new Error(`Invalid version: ${version}`); - parts[last] = (lastNum + 1).toString(); - return parts.join('.'); + if (parts.length !== 3) throw new Error(`Invalid version format: ${version}`); + + const [major, minor, patch] = parts.map(p => parseInt(p, 10)); + if (isNaN(major) || isNaN(minor) || isNaN(patch)) { + throw new Error(`Invalid version: ${version}`); + } + + if (type === 'minor') { + return `${major}.${minor + 1}.0`; + } else { + return `${major}.${minor}.${patch + 1}`; + } +} + +// get version type from command line argument +const versionType = process.argv[2] as 'patch' | 'minor' | undefined; +if (versionType && versionType !== 'patch' && versionType !== 'minor') { + throw new Error(`Invalid version type: ${versionType}. Expected 'patch' or 'minor'.`); } // find all package.json files in the workspace @@ -50,7 +63,7 @@ const rootPackageJson = path.resolve(_dirname, '../package.json'); const rootPkg = JSON.parse(fs.readFileSync(rootPackageJson, 'utf8')) as { version?: string }; if (!rootPkg.version) throw new Error('No "version" key found in package.json'); const rootVersion = rootPkg.version; -const newVersion = incrementVersion(rootVersion); +const newVersion = incrementVersion(rootVersion, versionType || 'patch'); for (const file of packageFiles) { const content = fs.readFileSync(file, 'utf8');