1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/extensions/extension.ts
Roman ed123e2819 extensions-api -- in-progress
Signed-off-by: Roman <ixrock@gmail.com>
2020-09-01 13:20:49 +03:00

87 lines
2.2 KiB
TypeScript

import { readJsonSync } from "fs-extra";
import { action, observable, when } from "mobx";
import { ExtensionModel } from "./extension-store";
import extensionManifest from "./example-extension/package.json"
import logger from "../main/logger";
// TODO: extensions api
// * Lazy load/unload extension (js/ts?) (from sources: local folder, npm_modules/@lens/some_plugin, etc.)
// * figure out how to expose lens external apis to extension:
// - opt1: import {someApi} from "@lens" => replaced to import from "$PATH/build/Lens.js" on the fly ?
// - opt2: eval with injected exposed apis / contents.executeJavaScript / script[src] / etc. ?
export type ExtensionId = string;
export type ExtensionVersion = string | number;
export type ExtensionManifest = typeof extensionManifest;
export class LensExtension implements ExtensionModel {
public id: ExtensionId;
public updateUrl: string;
@observable name = "";
@observable description = "";
@observable version: ExtensionVersion = "0.0.0";
@observable manifest: ExtensionManifest;
@observable manifestPath: string;
@observable isReady = false;
@observable isEnabled = false;
whenReady = when(() => this.isReady);
constructor(model: ExtensionModel) {
this.importModel(model);
}
@action
async importModel({ enabled, manifestPath, ...model }: ExtensionModel) {
try {
this.manifest = await readJsonSync(manifestPath, { throws: true })
this.manifestPath = manifestPath;
this.isEnabled = enabled;
Object.assign(this, model);
this.isReady = true;
} catch (err) {
logger.error(`[EXTENSION]: cannot read manifest at ${manifestPath}`, { ...model, err: String(err) })
this.disable();
}
}
async init() {
// todo?
}
async install() {
// todo
}
async uninstall() {
// todo
}
async checkNewVersion() {
// todo
}
async enable() {
this.isEnabled = true;
// todo
}
async disable() {
this.isEnabled = false;
// todo
}
toJSON(): ExtensionModel {
return {
id: this.id,
name: this.name,
version: this.version,
description: this.description,
manifestPath: this.manifestPath,
enabled: this.isEnabled,
updateUrl: this.updateUrl,
}
}
}