dockge/backend/check-version.ts

58 lines
1.6 KiB
TypeScript
Raw Permalink Normal View History

2023-11-11 14:18:37 +00:00
import { log } from "./log";
import compareVersions from "compare-versions";
import packageJSON from "../package.json";
import { Settings } from "./settings";
// How much time in ms to wait between update checks
const UPDATE_CHECKER_INTERVAL_MS = 1000 * 60 * 60 * 48;
const CHECK_URL = "https://dockge.kuma.pet/version";
class CheckVersion {
version = packageJSON.version;
latestVersion? : string;
interval? : NodeJS.Timeout;
2023-11-11 14:18:37 +00:00
async startInterval() {
const check = async () => {
if (await Settings.get("checkUpdate") === false) {
return;
2023-11-11 14:18:37 +00:00
}
log.debug("update-checker", "Retrieving latest versions");
2023-11-11 14:18:37 +00:00
try {
const res = await fetch(CHECK_URL);
const data = await res.json();
2023-11-11 14:18:37 +00:00
// For debug
if (process.env.TEST_CHECK_VERSION === "1") {
data.slow = "1000.0.0";
}
2023-11-11 14:18:37 +00:00
const checkBeta = await Settings.get("checkBeta");
2023-11-11 14:18:37 +00:00
if (checkBeta && data.beta) {
if (compareVersions.compare(data.beta, data.slow, ">")) {
this.latestVersion = data.beta;
return;
}
}
2023-11-11 14:18:37 +00:00
if (data.slow) {
this.latestVersion = data.slow;
}
2023-11-11 14:18:37 +00:00
} catch (_) {
log.info("update-checker", "Failed to check for new versions");
}
2023-11-11 14:18:37 +00:00
};
2023-11-11 14:18:37 +00:00
await check();
this.interval = setInterval(check, UPDATE_CHECKER_INTERVAL_MS);
2023-11-11 14:18:37 +00:00
}
}
const checkVersion = new CheckVersion();
export default checkVersion;