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

Introduce BundledExtensionComparer

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
Alex Andreev 2022-02-17 16:24:41 +03:00
parent bf80481a3e
commit 4808620085
3 changed files with 84 additions and 1 deletions

View File

@ -0,0 +1,40 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { BundledExtensionInstallChecker } from "../bundled-extension-install-checker";
import mockFs from "mock-fs";
const mockOpts = {
"some-user-data-directory": {
"some-file.tgz": "contents",
},
"extension-updates": {
"file.txt": "text",
"node-menu-0.0.1": {
"package.json": "{\"name\": \"node-menu\", \"version\": \"0.0.1\"}",
},
"survey-0.0.1": {
"dummyfile.text": "dummytext",
},
},
};
describe("BundledExtensionInstallChecker", () => {
afterEach(() => {
mockFs.restore();
});
it("Should return false if extension's package json not found", async () => {
mockFs(mockOpts);
const surveyInstalled = new BundledExtensionInstallChecker({
name: "survey",
version: "0.0.1",
downloadUrl: "http://my-example-url.com/node-menu-0.0.1.tgz",
}, "./extension-updates").isUpdateAlredyInstalled();
expect(surveyInstalled).toBeFalsy();
});
});

View File

@ -7,7 +7,7 @@ import semverGt from "semver/functions/gt";
type Extensions = Record<string, string>;
type ExtensionToDownload = {
export type ExtensionToDownload = {
name: string;
version: string;
downloadUrl: string;

View File

@ -0,0 +1,43 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import fs from "fs";
import logger from "../logger";
import type { ExtensionToDownload } from "./bundled-extension-comparer";
export class BundledExtensionInstallChecker {
constructor(private extension: ExtensionToDownload, private updatesFolder: string) {
}
private getExtensionManifest() {
try {
const itemsInFolder = fs.readdirSync(this.updatesFolder);
const directories = itemsInFolder.filter(dirOrFile =>
fs.lstatSync(`${this.updatesFolder}/${dirOrFile}`).isDirectory(),
);
for (const dir of directories) {
const packageJson = fs.readFileSync(`${this.updatesFolder}/${dir}/package.json`, "utf-8");
const contents: { name?: string, version?: string } = JSON.parse(packageJson);
if (contents.name == this.extension.name) {
return contents;
}
}
} catch (err) {
logger.error(err);
return {};
}
return {};
}
public isUpdateAlredyInstalled() {
const manifest = this.getExtensionManifest();
return manifest.version == this.extension.version;
}
}