1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Add bundled extensions version checker

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
Alex Andreev 2022-01-21 16:15:08 +03:00
parent 6285ec4c9e
commit a61121e5ca
2 changed files with 119 additions and 0 deletions

View File

@ -0,0 +1,67 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { DownloadFileOptions } from "../../renderer/utils";
import { BundledVersionChecker } from "../bundled-latest-version-checker";
process.env.BUNDLED_EXTENSIONS_URL = "https://someurl.com/versions.json";
describe("BundledExtensionsVersionChecker", () => {
it("returns for non-bundled extensions", async () => {
const checker = new BundledVersionChecker();
const version = await checker.getLatestVersion({
name: "foo",
version: "1.0.0"
});
expect(version).toBeNull();
});
it("returns null if json file not found", async () => {
const downloadJson = (args: DownloadFileOptions) => {
expect(args).toEqual({
url: process.env.BUNDLED_EXTENSIONS_URL,
});
return { promise: new Promise((resolve, reject) => {
reject({ url: process.env.BUNDLED_EXTENSIONS_URL, error: "File not found" })
}) };
};
const checker = new BundledVersionChecker(downloadJson);
const version = await checker.getLatestVersion({
name: "foo",
version: "1.0.0"
}, true);
expect(version).toBeNull();
})
it("returns null if extension name not found in file", async () => {
const downloadJson = (args: DownloadFileOptions) => {
expect(args).toEqual({
url: process.env.BUNDLED_EXTENSIONS_URL,
});
return { promise: new Promise((resolve) => {
resolve({
"sample-foo": "v4.4.0",
"sample-bar": "1.0.0"
});
}) };
};
const checker = new BundledVersionChecker(downloadJson);
const version = await checker.getLatestVersion({
name: "crd-extension",
version: "1.0.0"
}, true);
expect(version).toBeNull();
});
})

View File

@ -0,0 +1,52 @@
/**
* 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 { 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 updateUrl = process.env.BUNDLED_EXTENSIONS_URL;
const json = await this.getJson(updateUrl);
if (!json || json.error || !json[manifest.name]) {
logger.info(`[BUNDLED-EXTENSIONS-UPDATER]: No version found for ${manifest.name}.`);
return null;
}
const version = json[manifest.name];
return {
input: `${updateUrl}/${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;
}
}