Skip to content

Commit

Permalink
Resolve extension enabled status when loading it (#1485)
Browse files Browse the repository at this point in the history
* Resolve extension enabled status when loading it

Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com>

* Check if extension is enabled in store unless it is bundled

Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com>

* Return false by default for isEnabled

Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com>

* Refactor isEnabled assignment

Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com>
  • Loading branch information
nevalla committed Nov 25, 2020
1 parent ca49fb9 commit 29ea0c8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
18 changes: 10 additions & 8 deletions src/extensions/extension-discovery.ts
Expand Up @@ -6,6 +6,7 @@ import path from "path";
import { getBundledExtensions } from "../common/utils/app-version";
import logger from "../main/logger";
import { extensionInstaller, PackageJson } from "./extension-installer";
import { extensionsStore } from "./extensions-store";
import type { LensExtensionId, LensExtensionManifest } from "./lens-extension";

export interface InstalledExtension {
Expand Down Expand Up @@ -91,7 +92,7 @@ export class ExtensionDiscovery {
init() {
this.watchExtensions();
}

/**
* Watches for added/removed local extensions.
* Dependencies are installed automatically after an extension folder is copied.
Expand Down Expand Up @@ -213,24 +214,26 @@ export class ExtensionDiscovery {
}
}

protected async getByManifest(manifestPath: string, { isBundled = false, isEnabled = isBundled }: {
protected async getByManifest(manifestPath: string, { isBundled = false }: {
isBundled?: boolean;
isEnabled?: boolean;
} = {}): Promise<InstalledExtension | null> {
let manifestJson: LensExtensionManifest;
let isEnabled: boolean;

try {
// check manifest file for existence
fs.accessSync(manifestPath, fs.constants.F_OK);

manifestJson = __non_webpack_require__(manifestPath);
const installedManifestPath = path.join(this.nodeModulesPath, manifestJson.name, "package.json");
this.packagesJson.dependencies[manifestJson.name] = path.dirname(manifestPath);
const isEnabled = isBundled || extensionsStore.isEnabled(installedManifestPath);

return {
manifestPath: path.join(this.nodeModulesPath, manifestJson.name, "package.json"),
manifestPath: installedManifestPath,
manifest: manifestJson,
isBundled,
isEnabled,
isEnabled
};
} catch (error) {
logger.error(`${logModule}: can't install extension at ${manifestPath}: ${error}`, { manifestJson });
Expand Down Expand Up @@ -316,13 +319,12 @@ export class ExtensionDiscovery {
/**
* Loads extension from absolute path, updates this.packagesJson to include it and returns the extension.
*/
async loadExtensionFromPath(absPath: string, { isBundled = false, isEnabled = isBundled }: {
async loadExtensionFromPath(absPath: string, { isBundled = false }: {
isBundled?: boolean;
isEnabled?: boolean;
} = {}): Promise<InstalledExtension | null> {
const manifestPath = path.resolve(absPath, manifestFilename);

return this.getByManifest(manifestPath, { isBundled, isEnabled });
return this.getByManifest(manifestPath, { isBundled });
}
}

Expand Down
7 changes: 1 addition & 6 deletions src/extensions/extensions-store.ts
Expand Up @@ -47,11 +47,6 @@ export class ExtensionsStore extends BaseStore<LensExtensionsStoreModel> {
await extensionLoader.whenLoaded;
await this.whenLoaded;

// activate user-extensions when state is ready
extensionLoader.userExtensions.forEach((ext, extId) => {
ext.isEnabled = this.isEnabled(extId);
});

// apply state on changes from store
reaction(() => this.state.toJS(), extensionsState => {
extensionsState.forEach((state, extId) => {
Expand All @@ -70,7 +65,7 @@ export class ExtensionsStore extends BaseStore<LensExtensionsStoreModel> {

isEnabled(extId: LensExtensionId) {
const state = this.state.get(extId);
return !state /* enabled by default */ || state.enabled;
return state && state.enabled; // by default false
}

@action
Expand Down

0 comments on commit 29ea0c8

Please sign in to comment.