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