Files
sk-az-tools/scripts/bump-patch.mjs
2026-03-07 10:13:47 +01:00

49 lines
1.4 KiB
JavaScript
Executable File

#!/usr/bin/env node
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import semver from 'semver';
function bump(fileName) {
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const filePath = path.resolve(__dirname, '..', fileName);
if (!bump.nextVersion) {
const packageJsonPath = path.resolve(__dirname, '../package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
const version = packageJson.version;
if (typeof version !== 'string') {
throw new Error('package.json does not contain a string "version" field.');
}
const nextVersion = semver.inc(version, 'patch');
if (!nextVersion) {
throw new Error(`Unsupported semver format: "${version}"`);
}
bump.nextVersion = nextVersion;
}
if (!fs.existsSync(filePath)) {
throw new Error(`File not found: ${filePath}`);
}
const json = JSON.parse(fs.readFileSync(filePath, 'utf8'));
json.version = bump.nextVersion;
if (path.basename(filePath) === 'package-lock.json' && json.packages?.['']) {
json.packages[''].version = bump.nextVersion;
}
fs.writeFileSync(filePath, `${JSON.stringify(json, null, 4)}\n`, 'utf8');
console.log(`Bumped version in ${fileName} to ${bump.nextVersion}`);
}
bump('package.json');
bump('package-lock.json');