Skip to content

Commit 25fab48

Browse files
committed
refactor: create a uniform way of obtaining this extension's details.
Added: - Added new `extensionDetails` private property in `Configuration` as a Map object. - Added new `setExtensionData` method to set the data into the `extensionDetails` Map. Also added its method call into the `constructor`. - Added new `getExtensionData` public method to get the value of a specified key from the `extensionDetails` Map. Changed: - Refactored `getExtensionNames` to be more generic. This will be used in the new `setExtensionData` method to get and set the names, id and version into the `extensionDetails` Map. - Changed the name to `getExtensionPackageJsonData`. - Changed it's visibility to `private`. - Changed the reading of the package.json file to use the `readJsonFile` method as it does the exact same thing, and makes it DRYer. - Added the extension's version. - Changed all references to the old `getExtensionNames` method to use the new `getExtensionData` method with the specific key needed. Removed: - Removed the unused `extensionNames` variable and method call from the `getExtensionsPathsFromWslEnv` method.
1 parent 5a96715 commit 25fab48

2 files changed

Lines changed: 54 additions & 17 deletions

File tree

src/configuration.ts

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ export class Configuration {
2222
*/
2323
private logger: Logger;
2424

25+
/**
26+
* This extension details in the form of a key:value Map object, for ease of use.
27+
*/
28+
private extensionDetails = new Map<string, any>();
29+
2530
/**
2631
* A key:value Map object of language IDs and their config file paths.
2732
*/
@@ -61,10 +66,12 @@ export class Configuration {
6166
public constructor(logger: Logger) {
6267
this.logger = logger;
6368

69+
this.setExtensionData();
70+
6471
// Always output extension information to channel on activate.
65-
const extensionId = this.getExtensionNames().id;
66-
const extensionVersion = vscode.extensions.getExtension(extensionId)?.packageJSON.version;
67-
this.logger.info(`Extension: ${extensionId} (${extensionVersion})`);
72+
const id = this.getExtensionData("id");
73+
const version = this.getExtensionData("version");
74+
this.logger.info(`Extension: ${id} (${version})`);
6875

6976
this.findAllLanguageConfigFilePaths();
7077
this.setLanguageConfigDefinitions();
@@ -194,22 +201,56 @@ export class Configuration {
194201
}
195202

196203
/**
197-
* Get the names and ids of this extension from package.json.
204+
* Get the names, id, and version of this extension from package.json.
198205
*
199-
* @returns {object} An object containing the extension id, name, and display name.
206+
* @returns {object} An object containing the extension id, name, display name, and version.
200207
*/
201-
public getExtensionNames(): {id: string; name: string; displayName: string} {
202-
const packageJSON = JSON.parse(fs.readFileSync(__dirname + "/../../package.json").toString());
208+
private getExtensionPackageJsonData(): {id: string; name: string; displayName: string; version: string} {
209+
const packageJSON = this.readJsonFile(__dirname + "/../../package.json");
203210

204211
const displayName: string = packageJSON.displayName;
205212
const fullname: string = packageJSON.name;
206213
const id: string = `${packageJSON.publisher}.${fullname}`;
214+
const version: string = packageJSON.version;
207215

208216
let nameParts = fullname.split("-");
209217
nameParts[0] = "auto";
210218
const name = nameParts.join("-");
211219

212-
return {id: id, name: name, displayName: displayName};
220+
return {id: id, name: name, displayName: displayName, version: version};
221+
}
222+
223+
/**
224+
* Set the extension data into the extensionDetails Map.
225+
*/
226+
private setExtensionData() {
227+
const extensionPackageJsonData = this.getExtensionPackageJsonData();
228+
229+
const id = extensionPackageJsonData.id;
230+
const name = extensionPackageJsonData.name;
231+
const displayName = extensionPackageJsonData.displayName;
232+
const version = extensionPackageJsonData.version;
233+
234+
const userExtensionsPath = path.join(vscode.extensions.getExtension(id).extensionPath, "../");
235+
236+
this.extensionDetails.set("id", id);
237+
this.extensionDetails.set("name", name);
238+
this.extensionDetails.set("displayName", displayName);
239+
this.extensionDetails.set("version", version);
240+
this.extensionDetails.set("userExtensionsPath", userExtensionsPath);
241+
}
242+
243+
/**
244+
* Get the extension's details.
245+
*
246+
* @param {string} key The key of the specific extension detail to get.
247+
*
248+
* @returns {any} Returns a value of a specific key.
249+
*/
250+
public getExtensionData(key: string): any {
251+
if (this.extensionDetails.has(key)) {
252+
return this.extensionDetails.get(key);
253+
}
213254
}
214255

215256
/**
@@ -218,7 +259,7 @@ export class Configuration {
218259
* @returns {vscode.WorkspaceConfiguration}
219260
*/
220261
public getConfiguration(): vscode.WorkspaceConfiguration {
221-
return vscode.workspace.getConfiguration(this.getExtensionNames().name, null);
262+
return vscode.workspace.getConfiguration(this.getExtensionData("name"), null);
222263
}
223264

224265
/**
@@ -997,7 +1038,7 @@ export class Configuration {
9971038
*/
9981039
private handleChangeBladeMultiLineBlock(textEditor: vscode.TextEditor) {
9991040
let langId = textEditor.document.languageId;
1000-
const extensionNames = this.getExtensionNames();
1041+
const extensionName = this.getExtensionData("name");
10011042

10021043
// Only carry out function if languageId is blade.
10031044
if (langId === "blade" && !this.isLangIdDisabled(langId)) {
@@ -1021,7 +1062,7 @@ export class Configuration {
10211062
// then output a message to the user.
10221063
else if (langId == "blade" && this.isLangIdDisabled(langId)) {
10231064
vscode.window.showInformationMessage(
1024-
`Blade is set as disabled in the "${extensionNames.name}.disabledLanguages" setting. The "${extensionNames.name}.bladeOverrideComments" setting will have no affect.`,
1065+
`Blade is set as disabled in the "${extensionName}.disabledLanguages" setting. The "${extensionName}.bladeOverrideComments" setting will have no affect.`,
10251066
"OK"
10261067
);
10271068

@@ -1036,8 +1077,6 @@ export class Configuration {
10361077
* @returns {Object} An object containing the user and built-in extensions paths.
10371078
*/
10381079
private getExtensionsPathsFromWslEnv() {
1039-
const extensionNames = this.getExtensionNames();
1040-
10411080
/** Built-in Extensions */
10421081

10431082
// Get the path to the VS Code's exectutable from the VSCODE_CWD environment variable.

src/extension.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ export function activate(context: vscode.ExtensionContext) {
1717

1818
disposables.push(...configureCommentBlocksDisposable, ...registerCommandsDisposable);
1919

20-
const extensionNames = configuration.getExtensionNames();
21-
22-
const extensionName = extensionNames.name;
23-
const extensionDisplayName = extensionNames.displayName;
20+
const extensionName = configuration.getExtensionData("name");
21+
const extensionDisplayName = configuration.getExtensionData("displayName");
2422

2523
let disabledLangConfig: string[] = configuration.getConfigurationValue<string[]>("disabledLanguages");
2624

0 commit comments

Comments
 (0)