#!/usr/bin/env node import fs from 'node:fs'; import path from 'node:path'; import { parseArgs } from 'node:util'; import semver from 'semver'; 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 { values } = parseArgs({ options: { 'release-type': { type: 'string', short: 'r' }, }, strict: true, allowPositionals: false, }); const releaseType = values['release-type'] ?? 'patch'; if (!['major', 'minor', 'patch'].includes(releaseType)) { throw new Error(`Invalid --release-type '${releaseType}'. Allowed: major, minor, patch.`); } const bumpedVersion = bump('package.json', undefined, releaseType); bump('package-lock.json', bumpedVersion);