-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: code cleanup, separate class w/ activate() deactivate() lif…
…ecycle implementations
- Loading branch information
1 parent
6bd865f
commit 0cf88bc
Showing
9 changed files
with
159 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ node_modules | |
.vscode-test/ | ||
*.vsix | ||
.eslintcache | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
v16.14.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,14 @@ | ||
// The module 'vscode' contains the VS Code extensibility API | ||
// Import the module and reference it with the alias vscode in your code below | ||
import * as vscode from "vscode"; | ||
import { ApiClient } from "./api/api-client"; | ||
import { FeatureListTreeDataProvider } from "./features/FeatureListTreeDataProvider"; | ||
import { getWorkspaceRootPath } from "./utils/vscode-utils"; | ||
import { ExtensionInitialization } from "./services/extension-initialization.service"; | ||
|
||
// This method is called when your extension is activated | ||
// Your extension is activated the very first time the command is executed | ||
export async function activate(context: vscode.ExtensionContext) { | ||
// Register the tree view | ||
const rootPath = getWorkspaceRootPath(); | ||
|
||
if (rootPath) { | ||
// TODO: Read config from local file | ||
const apiClient = new ApiClient({ | ||
appHost: "http://localhost:3100", | ||
featuresHost: "http://localhost:3100", | ||
apiKey: "key_prod_b118a91f4800c2c6", | ||
}); | ||
const features = await apiClient.getFeatures(); | ||
|
||
vscode.window.registerTreeDataProvider( | ||
"featuresList", | ||
new FeatureListTreeDataProvider(context, features) | ||
); | ||
|
||
const treeView = vscode.window.createTreeView("featuresList", { | ||
treeDataProvider: new FeatureListTreeDataProvider(context, features), | ||
}); | ||
let extensionInitializationService: ExtensionInitialization | null = null; | ||
|
||
context.subscriptions.push(treeView); | ||
} | ||
|
||
// Use the console to output diagnostic information (console.log) and errors (console.error) | ||
// This line of code will only be executed once when your extension is activated | ||
console.log("Congratulations, your extension 'growthbook' is now active!"); | ||
|
||
// The command has been defined in the package.json file | ||
// Now provide the implementation of the command with registerCommand | ||
// The commandId parameter must match the command field in package.json | ||
const disposable = vscode.commands.registerCommand( | ||
"growthbook.helloWorld", | ||
() => { | ||
// The code you place here will be executed every time your command is executed | ||
// Display a message box to the user | ||
vscode.window.showInformationMessage("Hello World from growthbook!"); | ||
} | ||
); | ||
export async function activate(context: vscode.ExtensionContext) { | ||
extensionInitializationService = ExtensionInitialization.getInstance(context); | ||
|
||
context.subscriptions.push(disposable); | ||
extensionInitializationService?.activate(); | ||
} | ||
|
||
// This method is called when your extension is deactivated | ||
export function deactivate() {} | ||
export function deactivate() { | ||
extensionInitializationService?.deactivate(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import * as vscode from "vscode"; | ||
import { ApiClient } from "../api/api-client"; | ||
import { | ||
FeatureListTreeDataProvider, | ||
FeatureListTreeItem, | ||
} from "../features/FeatureListTreeDataProvider"; | ||
import { FeatureDefinition } from "../features/types"; | ||
import { | ||
getGrowthBookConfig, | ||
getWorkspaceRootPath, | ||
GrowthBookConfig, | ||
} from "../utils/vscode-utils"; | ||
|
||
interface IExtensionInitialization { | ||
activate(): Promise<void>; | ||
deactivate(): Promise<void>; | ||
} | ||
|
||
let extensionInitializationService: ExtensionInitialization | null = null; | ||
|
||
/** | ||
* Use the getInstance() method of the class. | ||
* If you do not provide a valid config, your instance will be null. | ||
*/ | ||
export class ExtensionInitialization implements IExtensionInitialization { | ||
private apiClient: ApiClient; | ||
private features: FeatureDefinition[] = []; | ||
private treeView: vscode.TreeView<FeatureListTreeItem> | null = null; | ||
|
||
private constructor( | ||
private context: vscode.ExtensionContext, | ||
private growthBookConfig: GrowthBookConfig | ||
) { | ||
const { featuresHost, featuresKey, appHost } = this.growthBookConfig; | ||
|
||
/* eslint-disable @typescript-eslint/no-non-null-assertion -- handled in getInstance() */ | ||
this.apiClient = new ApiClient({ | ||
appHost: appHost!, | ||
featuresHost: featuresHost!, | ||
featuresKey: featuresKey!, | ||
}); | ||
/* eslint-enable @typescript-eslint/no-non-null-assertion */ | ||
} | ||
|
||
static getInstance( | ||
context: vscode.ExtensionContext, | ||
growthBookConfig: GrowthBookConfig | null = getGrowthBookConfig( | ||
getWorkspaceRootPath() || "" | ||
) | ||
): ExtensionInitialization | null { | ||
if (!growthBookConfig) { | ||
console.error("ImplementationError: missing GrowthBook config"); | ||
return null; | ||
} | ||
|
||
const { featuresHost, featuresKey, appHost } = growthBookConfig; | ||
if (!featuresHost || !featuresKey || !appHost) { | ||
console.error( | ||
"ImplementationError: all GrowthBook config values are required for initialization" | ||
); | ||
return null; | ||
} | ||
|
||
extensionInitializationService = new ExtensionInitialization( | ||
context, | ||
growthBookConfig | ||
); | ||
|
||
return extensionInitializationService; | ||
} | ||
|
||
/** | ||
* Implement in the extension's activate() lifecycle hook | ||
*/ | ||
async activate(): Promise<void> { | ||
this.features = await this.apiClient.getFeatures(); | ||
this.initializeTreeView(); | ||
|
||
return Promise.resolve(); | ||
} | ||
|
||
/** | ||
* Implement in the extension's deactivate() lifecycle hook | ||
*/ | ||
async deactivate(): Promise<void> { | ||
this.treeView?.dispose(); | ||
return Promise.resolve(); | ||
} | ||
|
||
/** | ||
* Populate the left side tree view with the features data | ||
*/ | ||
private initializeTreeView(): void { | ||
vscode.window.registerTreeDataProvider( | ||
"featuresList", | ||
new FeatureListTreeDataProvider(this.context, this.features) | ||
); | ||
|
||
this.treeView = vscode.window.createTreeView("featuresList", { | ||
treeDataProvider: new FeatureListTreeDataProvider( | ||
this.context, | ||
this.features | ||
), | ||
}); | ||
|
||
this.context.subscriptions.push(this.treeView); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters