diff --git a/scripts/bump-patch.mjs b/scripts/bump-patch.mjs index 34bbea9..259dd83 100755 --- a/scripts/bump-patch.mjs +++ b/scripts/bump-patch.mjs @@ -2,47 +2,34 @@ 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; - } +function bump(fileName, version) { + 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')); - json.version = bump.nextVersion; + const currentVersion = json.version; - if (path.basename(filePath) === 'package-lock.json' && json.packages?.['']) { - json.packages[''].version = bump.nextVersion; + if (typeof currentVersion !== 'string') { + throw new Error(`${fileName} does not contain a string "version" field.`); } + const nextVersion = version ?? semver.inc(currentVersion, 'patch'); + + 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 ${bump.nextVersion}`); + console.log(`Bumped version in ${fileName} to ${nextVersion}`); + return nextVersion; } -bump('package.json'); -bump('package-lock.json'); +const bumpedVersion = bump('package.json'); +bump('package-lock.json', bumpedVersion);