2021-11-30 17:30:25 +01:00
|
|
|
// SPDX-FileCopyrightText: 2021 Johannes Loher
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
import fs from "fs-extra";
|
|
|
|
import semver from "semver";
|
|
|
|
import yargs from "yargs";
|
|
|
|
import { hideBin } from "yargs/helpers";
|
|
|
|
|
2023-06-29 21:53:50 +02:00
|
|
|
const packageType = "system";
|
|
|
|
const repositoryOwner = process.env.CI_REPO_OWNER;
|
|
|
|
const repositoryName = process.env.CI_REPO_NAME;
|
|
|
|
const repositoryURL = process.env.CI_REPO_LINK;
|
|
|
|
const forgeURL = process.env.CI_FORGE_URL;
|
2022-02-08 21:46:12 +01:00
|
|
|
|
2023-07-09 23:05:17 +02:00
|
|
|
const getManifestUrl = (channel) =>
|
2023-07-10 22:23:13 +02:00
|
|
|
`${forgeURL}/api/packages/${repositoryOwner}/generic/${repositoryName}/${channel}/${packageType}.json`;
|
2023-06-29 21:53:50 +02:00
|
|
|
const getDownloadURL = (version) => `${repositoryURL}/releases/download/${version}/${repositoryName}.zip`;
|
|
|
|
const bugsURL = `${repositoryURL}/issues`;
|
|
|
|
const getChangelogURL = (version) => `${repositoryURL}/releases/tag/${version}`;
|
|
|
|
const getReadmeURL = (version) => `${repositoryURL}/raw/tag/${version}/README.md`;
|
|
|
|
const getLicenseURL = (version) => `${repositoryURL}/raw/tag/${version}/LICENSE.md`;
|
2022-02-08 21:46:12 +01:00
|
|
|
|
2023-06-29 21:53:50 +02:00
|
|
|
const manifestPath = `${packageType}.json`;
|
2021-11-30 17:30:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the contents of the manifest file as object.
|
2022-01-31 15:13:32 +01:00
|
|
|
* @returns {unknown} An object describing the manifest
|
2021-11-30 17:30:25 +01:00
|
|
|
*/
|
|
|
|
function getManifest() {
|
2023-07-10 22:23:13 +02:00
|
|
|
if (fs.existsSync(manifestPath)) {
|
|
|
|
return fs.readJSONSync(manifestPath);
|
|
|
|
}
|
2021-11-30 17:30:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the target version based on on the current version and the argument passed as release.
|
|
|
|
* @param {string} currentVersion The current version
|
|
|
|
* @param {semver.ReleaseType | string} release Either a semver release type or a valid semver version
|
|
|
|
* @returns {string | null} The target version
|
|
|
|
*/
|
|
|
|
function getTargetVersion(currentVersion, release) {
|
2023-07-10 22:23:13 +02:00
|
|
|
if (["major", "premajor", "minor", "preminor", "patch", "prepatch", "prerelease"].includes(release)) {
|
|
|
|
return semver.inc(currentVersion, release);
|
|
|
|
} else {
|
|
|
|
return semver.valid(release);
|
|
|
|
}
|
2021-11-30 17:30:25 +01:00
|
|
|
}
|
|
|
|
|
2023-07-09 23:05:17 +02:00
|
|
|
/**
|
|
|
|
* Get the channel for a given version.
|
|
|
|
* @param {string} version The version for which to get the channel
|
|
|
|
* @returns {"latest" | "beta"} The channel for the version
|
|
|
|
*/
|
|
|
|
function getChannel(version) {
|
2023-07-10 22:23:13 +02:00
|
|
|
return version.includes("-") ? "beta" : "latest";
|
2023-07-09 23:05:17 +02:00
|
|
|
}
|
|
|
|
|
2021-11-30 17:30:25 +01:00
|
|
|
/**
|
|
|
|
* Update version and download URL.
|
|
|
|
* @param {semver.ReleaseType | string} release Either a semver release type or a valid semver version
|
|
|
|
*/
|
|
|
|
function bumpVersion(release) {
|
2023-07-10 22:23:13 +02:00
|
|
|
if (!release) {
|
|
|
|
throw new Error("Missing release type");
|
|
|
|
}
|
2021-11-30 17:30:25 +01:00
|
|
|
|
2023-07-10 22:23:13 +02:00
|
|
|
const packageJson = fs.readJSONSync("package.json");
|
|
|
|
const manifest = getManifest();
|
|
|
|
if (!manifest) throw new Error("Manifest JSON not found");
|
2021-11-30 17:30:25 +01:00
|
|
|
|
2023-07-10 22:23:13 +02:00
|
|
|
const currentVersion = packageJson.version;
|
|
|
|
const targetVersion = getTargetVersion(currentVersion, release);
|
2021-11-30 17:30:25 +01:00
|
|
|
|
2023-07-10 22:23:13 +02:00
|
|
|
if (!targetVersion) {
|
|
|
|
throw new Error("Incorrect version arguments");
|
|
|
|
}
|
|
|
|
if (targetVersion === currentVersion) {
|
|
|
|
throw new Error("Target version is identical to current version");
|
|
|
|
}
|
2021-11-30 17:30:25 +01:00
|
|
|
|
2023-07-10 22:23:13 +02:00
|
|
|
console.log(`Bumping version number to '${targetVersion}'`);
|
|
|
|
packageJson.version = targetVersion;
|
|
|
|
fs.writeJSONSync("package.json", packageJson, { spaces: 2 });
|
|
|
|
manifest.version = targetVersion;
|
|
|
|
manifest.url = repositoryURL;
|
|
|
|
manifest.manifest = getManifestUrl(getChannel(targetVersion));
|
|
|
|
manifest.download = getDownloadURL(targetVersion);
|
|
|
|
manifest.bugs = bugsURL;
|
|
|
|
manifest.changelog = getChangelogURL(targetVersion);
|
|
|
|
manifest.readme = getReadmeURL(targetVersion);
|
|
|
|
manifest.license = getLicenseURL(targetVersion);
|
|
|
|
fs.writeJSONSync(manifestPath, manifest, { spaces: 2 });
|
2021-11-30 17:30:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const argv = yargs(hideBin(process.argv)).usage("Usage: $0").option("release", {
|
2023-07-10 22:23:13 +02:00
|
|
|
alias: "r",
|
|
|
|
type: "string",
|
|
|
|
demandOption: true,
|
|
|
|
description: "Either a semver release type or a valid semver version",
|
2021-11-30 17:30:25 +01:00
|
|
|
}).argv;
|
|
|
|
const release = argv.r;
|
|
|
|
bumpVersion(release);
|