ktx/scripts/update-public-release-version.mjs
Andrey Avtomonov 2f70861a18
feat(release): one version everywhere via @semantic-release/git (#186)
* feat(release): commit version files back to branch for one-version-everywhere

Add @semantic-release/git to the release plugin chain so the bumped
package.json, release-policy.json, and packages/cli/package.json land
back on the release branch after publish. This keeps the published npm
version and the in-repo version files in sync, so local builds from
main report the released version (e.g. ktx --version and the daemon
/health endpoint via KTX_DAEMON_VERSION).

Also widens assertPublicNpmReleaseTag to accept branch-<sanitized> tags,
unblocking branch RC publishes that pass through update-public-release-
version.mjs.

* test(release): pin GITHUB_REF_NAME in main-rc releaseTag assertion

The bare releaseTag('rc') call defaulted to process.env.GITHUB_REF_NAME,
which on PR CI is the merge ref (e.g. 186/merge) and yields
'branch-186-merge' instead of 'next'. Pass an explicit { GITHUB_REF_NAME:
'main' } so the test exercises the main-rc path regardless of CI env.
2026-05-20 17:01:26 +02:00

83 lines
2.5 KiB
JavaScript

#!/usr/bin/env node
import { readFile, writeFile } from 'node:fs/promises';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import {
PUBLIC_NPM_PACKAGE_NAME,
assertPublicNpmPackageVersion,
assertPublicNpmReleaseTag,
releasePolicyPath,
} from './public-npm-release-metadata.mjs';
function scriptRootDir() {
return resolve(dirname(fileURLToPath(import.meta.url)), '..');
}
async function readJson(path) {
return JSON.parse(await readFile(path, 'utf8'));
}
async function writeJson(path, value) {
await writeFile(path, `${JSON.stringify(value, null, 2)}\n`);
}
export async function updatePublicReleaseVersion(rootDir, version, tag) {
const safeVersion = assertPublicNpmPackageVersion(version);
const safeTag = assertPublicNpmReleaseTag(tag);
const packageJsonPath = join(rootDir, 'package.json');
const packageJson = await readJson(packageJsonPath);
packageJson.version = safeVersion;
await writeJson(packageJsonPath, packageJson);
const cliPackageJsonPath = join(rootDir, 'packages', 'cli', 'package.json');
const cliPackageJson = await readJson(cliPackageJsonPath);
cliPackageJson.version = safeVersion;
await writeJson(cliPackageJsonPath, cliPackageJson);
const policyPath = releasePolicyPath(rootDir);
const policy = await readJson(policyPath);
policy.publicNpmPackageVersion = safeVersion;
policy.releaseMode = 'npm-public-release-ready';
policy.requiredBeforePublishing = [];
policy.npm = {
...policy.npm,
publish: true,
registry: policy.npm?.registry ?? null,
access: 'public',
tag: safeTag,
packages: [PUBLIC_NPM_PACKAGE_NAME],
};
policy.publishedPackageSmoke = {
...policy.publishedPackageSmoke,
packageName: PUBLIC_NPM_PACKAGE_NAME,
version: safeVersion,
};
await writeJson(policyPath, policy);
return {
version: safeVersion,
tag: safeTag,
};
}
async function main() {
const [version, tag] = process.argv.slice(2);
if (!version || !tag) {
throw new Error('Usage: node scripts/update-public-release-version.mjs <version> <latest|next>');
}
const result = await updatePublicReleaseVersion(scriptRootDir(), version, tag);
process.stdout.write(`Updated ${PUBLIC_NPM_PACKAGE_NAME} release metadata to ${result.version} (${result.tag})\n`);
}
if (import.meta.url === pathToFileURL(process.argv[1] ?? '').href) {
try {
await main();
} catch (error) {
process.stderr.write(`${error instanceof Error ? error.stack : String(error)}\n`);
process.exitCode = 1;
}
}