2022-09-27 10:45:27 +03:00
|
|
|
import semver from "semver";
|
|
|
|
import { FetchRequest } from "../utils/index.js";
|
2022-09-30 05:57:27 +03:00
|
|
|
import { atomicWrite } from "./utils/fs.js";
|
2022-09-30 06:19:40 +03:00
|
|
|
import { getGitLog } from "./utils/git.js";
|
2022-09-30 05:57:27 +03:00
|
|
|
import { loadJson, saveJson } from "./utils/json.js";
|
2022-09-27 10:45:27 +03:00
|
|
|
import { resolve } from "./utils/path.js";
|
|
|
|
const cache = {};
|
|
|
|
async function getNpmPackage(name) {
|
|
|
|
if (!cache[name]) {
|
|
|
|
const resp = await (new FetchRequest("https:/\/registry.npmjs.org/" + name)).send();
|
|
|
|
resp.assertOk();
|
|
|
|
cache[name] = resp.bodyJson;
|
|
|
|
}
|
|
|
|
return cache[name] || null;
|
|
|
|
}
|
2022-09-30 05:57:27 +03:00
|
|
|
function writeVersion(version) {
|
2022-11-30 23:44:23 +03:00
|
|
|
const content = `export const version: string = "${version}";\n`;
|
2022-09-30 05:57:27 +03:00
|
|
|
atomicWrite(resolve("src.ts/_version.ts"), content);
|
2022-09-27 10:45:27 +03:00
|
|
|
}
|
|
|
|
(async function () {
|
|
|
|
// Local pkg
|
|
|
|
const pkgPath = resolve("package.json");
|
|
|
|
const pkgInfo = loadJson(pkgPath);
|
|
|
|
const tag = pkgInfo.publishConfig.tag;
|
|
|
|
// Get the remote version that matches our dist-tag
|
|
|
|
const remoteInfo = await getNpmPackage(pkgInfo.name);
|
|
|
|
const remoteVersion = remoteInfo["dist-tags"][tag];
|
|
|
|
// Remote pkg
|
|
|
|
const remotePkgInfo = remoteInfo.versions[remoteVersion];
|
2022-09-30 05:57:27 +03:00
|
|
|
const remoteGitHead = remotePkgInfo.gitHead;
|
2022-09-30 06:19:40 +03:00
|
|
|
let gitHead = "";
|
|
|
|
for (const log of await getGitLog(".")) {
|
|
|
|
if (log.body.startsWith("admin:")) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-10-01 08:34:06 +03:00
|
|
|
if (log.body.startsWith("tests:")) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-09-30 06:19:40 +03:00
|
|
|
gitHead = log.commit;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (gitHead === "") {
|
|
|
|
throw new Error("no meaningful commit found");
|
|
|
|
}
|
2022-09-30 05:57:27 +03:00
|
|
|
// There are new commits, not reflected in the package
|
|
|
|
// published on npm; update the gitHead and version
|
|
|
|
if (gitHead !== remoteGitHead) {
|
|
|
|
// Bump the version from the remote version
|
2022-09-27 10:45:27 +03:00
|
|
|
if (tag.indexOf("beta") >= 0) {
|
|
|
|
// Still a beta branch; advance the beta version
|
|
|
|
const prerelease = semver.prerelease(remoteVersion);
|
|
|
|
if (prerelease == null || prerelease.length !== 2) {
|
|
|
|
throw new Error("no prerelease found");
|
|
|
|
}
|
|
|
|
pkgInfo.version = semver.inc(remoteVersion, "prerelease", String(prerelease[0]));
|
|
|
|
}
|
|
|
|
else if (semver.minor(remoteVersion) == semver.minor(pkgInfo.version)) {
|
|
|
|
// If we want to bump the minor version, it was done explicitly in the pkg
|
|
|
|
pkgInfo.version = semver.inc(remoteVersion, "patch");
|
|
|
|
}
|
2022-09-30 05:57:27 +03:00
|
|
|
pkgInfo.gitHead = gitHead;
|
|
|
|
// Save the package.json
|
|
|
|
saveJson(pkgPath, pkgInfo, true);
|
|
|
|
// Save the src.ts/_version.ts
|
|
|
|
writeVersion(pkgInfo.version);
|
2022-09-27 10:45:27 +03:00
|
|
|
}
|
|
|
|
})().catch((error) => {
|
2022-09-30 05:57:27 +03:00
|
|
|
console.log("ERROR");
|
2022-09-27 10:45:27 +03:00
|
|
|
console.log(error);
|
2022-09-30 05:57:27 +03:00
|
|
|
process.exit(1);
|
2022-09-27 10:45:27 +03:00
|
|
|
});
|
2022-09-30 05:57:27 +03:00
|
|
|
//# sourceMappingURL=update-version.js.map
|