Refactor bump-patch script to use Commander for argument parsing

This commit is contained in:
2026-03-09 08:43:44 +01:00
parent 9142518cc3
commit 47a8b19748

View File

@@ -2,8 +2,8 @@
import fs from 'node:fs';
import path from 'node:path';
import { parseArgs } from 'node:util';
import semver from 'semver';
import { Command, Option } from 'commander';
function bump(fileName, version, releaseType = 'patch') {
const filePath = path.resolve(process.cwd(), fileName);
@@ -32,17 +32,13 @@ function bump(fileName, version, releaseType = 'patch') {
return nextVersion;
}
const { values } = parseArgs({
options: {
'release-type': { type: 'string', short: 'r' },
},
strict: true,
allowPositionals: false,
});
const program = new Command();
program
.name('bump-patch')
.description('Bump the version in package.json')
.addOption(new Option('-r, --release-type <type>', 'Release type (major, minor, patch)', 'patch')
.choices('release-type', ['major', 'minor', 'patch'])
)
.parse(process.argv);
const releaseType = values['release-type'] ?? 'patch';
if (!['major', 'minor', 'patch'].includes(releaseType)) {
throw new Error(`Invalid --release-type '${releaseType}'. Allowed: major, minor, patch.`);
}
bump('package.json', undefined, releaseType);
bump('package.json', undefined, program.opts().releaseType);