45 lines
1.3 KiB
JavaScript
Executable File
45 lines
1.3 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import semver from 'semver';
|
|
import { Command, Option } from 'commander';
|
|
|
|
function bump(fileName, version, releaseType = 'patch') {
|
|
const filePath = path.resolve(process.cwd(), fileName);
|
|
|
|
if (!fs.existsSync(filePath)) {
|
|
throw new Error(`File not found: ${filePath}`);
|
|
}
|
|
|
|
const json = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
const currentVersion = json.version;
|
|
|
|
if (typeof currentVersion !== 'string') {
|
|
throw new Error(`${fileName} does not contain a string "version" field.`);
|
|
}
|
|
|
|
const nextVersion = version ?? semver.inc(currentVersion, releaseType);
|
|
|
|
if (!nextVersion) {
|
|
throw new Error(`Unsupported semver format: "${currentVersion}"`);
|
|
}
|
|
|
|
json.version = nextVersion;
|
|
|
|
fs.writeFileSync(filePath, `${JSON.stringify(json, null, 4)}\n`, 'utf8');
|
|
console.log(`Bumped version in ${fileName} to ${nextVersion}`);
|
|
return nextVersion;
|
|
}
|
|
|
|
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);
|
|
|
|
bump('package.json', undefined, program.opts().releaseType);
|