1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/extensions/bundled-latest-version-checker.ts
Alex Andreev 578a6cec21 Update extension after ipc event
Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
2022-01-25 15:34:20 +03:00

52 lines
1.6 KiB
TypeScript

/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { SemVer } from "semver";
import logger from "../common/logger";
import { extensionUpdateUrl } from "../common/vars";
import { DownloadFileOptions, downloadJson } from "../renderer/utils";
import type { LensExtensionManifest } from "./lens-extension";
import type { LensExtensionLatestVersionChecker } from "./lens-extension-latest-version-checker";
type ExtensionName = string;
type ExtensionVersion = string;
type Versions = Record<ExtensionName, ExtensionVersion>;
export class BundledVersionChecker implements LensExtensionLatestVersionChecker {
protected downloadJson;
constructor(downloadJsonOverride?: (args: DownloadFileOptions) => any) {
this.downloadJson = downloadJsonOverride || downloadJson;
}
public async getLatestVersion(manifest: LensExtensionManifest, isBundled?: boolean) {
if (!isBundled) {
return null;
}
const json = await this.getJson(`${extensionUpdateUrl}/versions.json`);
if (!json || json.error || !json[manifest.name]) {
logger.info(`[EXTENSION-VERSION-CHECKER]: No version found for ${manifest.name}.`);
return null;
}
const version = json[manifest.name];
return {
input: `${extensionUpdateUrl}/${manifest.name}-${version}.tar`,
version: new SemVer(version).version
}
}
protected async getJson(url: string): Promise<Versions> {
const { promise } = this.downloadJson({ url });
const json = await promise.catch(() => {
// do nothing
});
return json;
}
}