mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
extensions-api -- in-progress
Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
parent
e56f8f3362
commit
ed123e2819
3
src/extensions/example-extension/README.md
Normal file
3
src/extensions/example-extension/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# Lens Example Extension
|
||||
|
||||
*TODO*: add more info
|
||||
@ -1,6 +1,6 @@
|
||||
import { LensExtension } from "@lens"; // todo: handle runtime import
|
||||
|
||||
export class ExampleExtension extends LensExtension {
|
||||
export default class ExampleExtension extends LensExtension {
|
||||
async init(): Promise<any> {
|
||||
console.log('Example extension: init')
|
||||
return super.init();
|
||||
|
||||
@ -1,11 +1,10 @@
|
||||
import path from "path";
|
||||
import { action, observable, toJS } from "mobx";
|
||||
import fs from "fs-extra";
|
||||
import { action, comparer, observable, toJS } from "mobx";
|
||||
import { BaseStore } from "../common/base-store";
|
||||
import { LensExtension } from "./extension";
|
||||
import { ExtensionId, ExtensionVersion, LensExtension } from "./extension";
|
||||
import { isDevelopment } from "../common/vars";
|
||||
|
||||
export type ExtensionId = string;
|
||||
export type ExtensionVersion = string | number;
|
||||
import logger from "../main/logger";
|
||||
|
||||
export interface ExtensionStoreModel {
|
||||
version: ExtensionVersion;
|
||||
@ -16,6 +15,7 @@ export interface ExtensionModel {
|
||||
id: ExtensionId;
|
||||
version: ExtensionVersion;
|
||||
name: string;
|
||||
manifestPath: string;
|
||||
description?: string;
|
||||
enabled?: boolean;
|
||||
updateUrl?: string;
|
||||
@ -31,6 +31,21 @@ export class ExtensionStore extends BaseStore<ExtensionStoreModel> {
|
||||
@observable version: ExtensionVersion = "0.0.0";
|
||||
@observable extensions = observable.map<ExtensionId, LensExtension>();
|
||||
@observable removed = observable.map<ExtensionId, LensExtension>();
|
||||
@observable installed = observable.set<LensExtension>([], { equals: comparer.shallow });
|
||||
|
||||
async load() {
|
||||
await this.loadExtensions();
|
||||
return super.load();
|
||||
}
|
||||
|
||||
async loadExtensions() {
|
||||
const localExtensions = await fs.readdir(this.builtInExtensionsPath);
|
||||
logger.info(`[EXTENSIONS]: scanning installed extensions`, { paths: localExtensions });
|
||||
this.installed.replace(localExtensions as any[]);
|
||||
// return localExtensions
|
||||
// .filter(path => ![".", ".."].includes(path))
|
||||
// .map(path => import(`${path}/package.json`));
|
||||
}
|
||||
|
||||
get builtInExtensionsPath(): string {
|
||||
if (isDevelopment) {
|
||||
|
||||
@ -1,5 +1,8 @@
|
||||
import { observable } from "mobx";
|
||||
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.)
|
||||
@ -9,27 +12,41 @@ import { ExtensionModel } from "./extension-store";
|
||||
|
||||
export type ExtensionId = string;
|
||||
export type ExtensionVersion = string | number;
|
||||
export type ExtensionManifest = typeof extensionManifest;
|
||||
|
||||
export class LensExtension implements ExtensionModel {
|
||||
public id: ExtensionId;
|
||||
public version: string | number;
|
||||
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;
|
||||
@observable isInstalled = false;
|
||||
|
||||
whenReady = when(() => this.isReady);
|
||||
|
||||
constructor(model: ExtensionModel) {
|
||||
this.importModel(model);
|
||||
}
|
||||
|
||||
importModel({ enabled, ...model }: ExtensionModel) {
|
||||
Object.assign(this, model);
|
||||
this.isEnabled = enabled;
|
||||
@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(){
|
||||
async init() {
|
||||
// todo?
|
||||
}
|
||||
|
||||
@ -46,10 +63,24 @@ export class LensExtension implements ExtensionModel {
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,6 +4,7 @@ import { render } from "react-dom";
|
||||
import { isMac } from "../common/vars";
|
||||
import { userStore } from "../common/user-store";
|
||||
import { workspaceStore } from "../common/workspace-store";
|
||||
import { extensionStore } from "../extensions/extension-store";
|
||||
import { clusterStore, getHostedClusterId } from "../common/cluster-store";
|
||||
import { i18nStore } from "./i18n";
|
||||
import { themeStore } from "./theme.store";
|
||||
@ -23,6 +24,7 @@ export async function bootstrap(App: AppComponent) {
|
||||
userStore.load(),
|
||||
workspaceStore.load(),
|
||||
clusterStore.load(),
|
||||
extensionStore.load(),
|
||||
i18nStore.init(),
|
||||
themeStore.init(),
|
||||
]);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user