Files
sk-az-tools/scripts/check-package-version.mjs

61 lines
2.6 KiB
JavaScript
Executable File

#!/usr/bin/env node
import { readFileSync, writeFileSync } from "node:fs";
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." },
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}\n`);
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
spawnSync("npm", ["install", "@slawek/sk-tools"], { stdio: "inherit" });
spawnSync("npm", ["link", "@slawek/sk-tools"], { stdio: "inherit" });
// Show the updated dependency tree
spawnSync("npm", ["ls"], { stdio: "inherit" });
} else {
console.log(`\nSK Tools version requested: ${skAzToolsPackage.dependencies["@slawek/sk-tools"] ?? "not found"}`);
}