From ade8f065e0cab0ce31175f72799e9010454cfc78 Mon Sep 17 00:00:00 2001 From: Slawomir Koszewski Date: Wed, 11 Mar 2026 11:59:20 +0100 Subject: [PATCH] feat: enhance package version management in check-package-version script --- scripts/check-package-version.mjs | 35 ++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/scripts/check-package-version.mjs b/scripts/check-package-version.mjs index 20cde9a..6a6cbc3 100755 --- a/scripts/check-package-version.mjs +++ b/scripts/check-package-version.mjs @@ -1,28 +1,53 @@ #!/usr/bin/env node import { readFileSync, writeFileSync } from "node:fs"; -import { execSync, spawnSync } from "node:child_process"; +import { spawnSync } from "node:child_process"; import { resolve } from "node:path"; import { parseArgs } from "node:util"; +import { inc } from "semver"; const skAzToolsPackagePath = resolve("package.json"); +const skAzToolsPackageLockPath = resolve("package-lock.json"); const skToolsPackagePath = resolve("../sk-tools", "package.json"); const skAzToolsPackage = JSON.parse(readFileSync(skAzToolsPackagePath, "utf-8")); +const skAzToolsPackageLock = JSON.parse(readFileSync(skAzToolsPackageLockPath, "utf-8")); const skToolsPackage = JSON.parse(readFileSync(skToolsPackagePath, "utf-8")); const { values } = parseArgs({ options: { - update: { type: "boolean", short: "u", description: "Update @slawek/sk-tools to the latest version." } + update: { type: "boolean", short: "u", description: "Update @slawek/sk-tools to the latest version." }, + bump: { type: "string", short: "b", description: "Bump the version of @slawek/sk-az-tools in package.json. Allowed values: major, minor, patch." } } }); +if (!["major", "minor", "patch"].includes(values.bump)) { + console.error(`Invalid bump type: ${values.bump}. Allowed values are: major, minor, patch.`); + process.exit(1); +} + // Package versions console.log(`SK Tools version: ${skToolsPackage.version}`); -console.log(`SK Azure Tools version: ${skAzToolsPackage.version}`); +console.log(`SK Azure Tools version: ${skAzToolsPackage.version}\n`); -if (values.update) { - console.log(`\nUpdating package.json to use @slawek/sk-tools version ${skToolsPackage.version} or later...`); +if (values.bump) { + const newVersion = inc(skAzToolsPackage.version, values.bump); + if (!newVersion) { + console.error(`Failed to bump version: ${skAzToolsPackage.version}`); + process.exit(1); + } + skAzToolsPackage.version = newVersion; + writeFileSync(skAzToolsPackagePath, JSON.stringify(skAzToolsPackage, null, 4)); + console.log(`Bumped SK Azure Tools version to: ${newVersion}`); +} + +console.log(`SK Azure Tools Locked version: ${skAzToolsPackageLock.version}`); + +// Update package.json if --update flag is set +// or if the version of @slawek/sk-az-tools in package.json +// is different than the version in package-lock.json. +if (values.update || skAzToolsPackage.version !== skToolsPackage.version) { + console.log(`Updating package.json...`); skAzToolsPackage.dependencies["@slawek/sk-tools"] = `>=${skToolsPackage.version}`; writeFileSync(skAzToolsPackagePath, JSON.stringify(skAzToolsPackage, null, 4)); // Install and link the updated package