Skip to content

Commit

Permalink
12 hover provider for manifest files (#23)
Browse files Browse the repository at this point in the history
* refactored code and added better error handling

* refactored

* minor changes
  • Loading branch information
rajpreet-s authored Jul 29, 2024
1 parent c49efb8 commit d57742a
Show file tree
Hide file tree
Showing 16 changed files with 150 additions and 202 deletions.
87 changes: 33 additions & 54 deletions src/commands/debrickedCommand.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as vscode from "vscode";
import { DebrickedCommands, MessageStatus, Organization } from "../constants/index";
import { DebrickedCommands, Organization } from "../constants/index";
import { BaseCommandService, ScanService, FileService } from "../services";
import { Logger, GlobalState, Common, StatusBarMessageHelper } from "../helpers";
import { DebrickedCommandNode } from "../types";
import { Logger, GlobalState, Common, ErrorHandler } from "../helpers";

export class DebrickedCommand {
private static get globalState(): GlobalState {
Expand All @@ -13,69 +12,49 @@ export class DebrickedCommand {
Logger.logInfo("Started registering commands");
DebrickedCommand.globalState.setGlobalData(Organization.seqIdKey, Common.generateHashCode());

const baseSubCommands: DebrickedCommandNode[] | undefined = DebrickedCommands.BASE_COMMAND.sub_commands;
const fileSubCommands: DebrickedCommandNode[] | undefined = DebrickedCommands.FILES.sub_commands;
const baseSubCommands = DebrickedCommands.BASE_COMMAND.sub_commands;
const fileSubCommands = DebrickedCommands.FILES.sub_commands;

context.subscriptions.push(
vscode.commands.registerCommand(DebrickedCommands.BASE_COMMAND.command, async () => {
await BaseCommandService.baseCommand();
}),
);
// Register base command
DebrickedCommand.registerCommand(context, DebrickedCommands.BASE_COMMAND.command, BaseCommandService.baseCommand);

// Register base sub-commands
if (baseSubCommands) {
context.subscriptions.push(
vscode.commands.registerCommand(baseSubCommands[0].command, async () => {
DebrickedCommand.globalState.setGlobalData(Organization.seqIdKey, Common.generateHashCode());
await BaseCommandService.installCommand();
}),
);

context.subscriptions.push(
vscode.commands.registerCommand(baseSubCommands[1].command, async () => {
await BaseCommandService.updateCommand();
}),
);

context.subscriptions.push(
vscode.commands.registerCommand(baseSubCommands[2].command, async () => {
await BaseCommandService.help();
}),
);

context.subscriptions.push(
vscode.commands.registerCommand(baseSubCommands[3].command, async () => {
await Logger.openLogFile();
}),
);
DebrickedCommand.registerCommand(context, baseSubCommands[0].command, async () => {
DebrickedCommand.globalState.setGlobalData(Organization.seqIdKey, Common.generateHashCode());
await BaseCommandService.installCommand();
});
DebrickedCommand.registerCommand(context, baseSubCommands[1].command, BaseCommandService.updateCommand);
DebrickedCommand.registerCommand(context, baseSubCommands[2].command, BaseCommandService.help);
DebrickedCommand.registerCommand(context, baseSubCommands[3].command, Logger.openLogFile);
}

context.subscriptions.push(
vscode.commands.registerCommand(DebrickedCommands.SCAN.command, async () => {
await ScanService.scanService();
}),
);
// Register scan command
DebrickedCommand.registerCommand(context, DebrickedCommands.SCAN.command, ScanService.scanService);

context.subscriptions.push(
vscode.commands.registerCommand(DebrickedCommands.FILES.command, async () => {
await FileService.filesService();
}),
);
// Register files command
DebrickedCommand.registerCommand(context, DebrickedCommands.FILES.command, FileService.filesService);

// Register file sub-commands
if (fileSubCommands) {
context.subscriptions.push(
vscode.commands.registerCommand(fileSubCommands[0].command, async () => {
await FileService.findFilesService();
}),
);
DebrickedCommand.registerCommand(context, fileSubCommands[0].command, FileService.findFilesService);
}

// Add file watcher for all files found from 'debricked files find'
await ScanService.addWatcherToManifestFiles((await FileService.findFilesService()) || [], context);
} catch (error: any) {
StatusBarMessageHelper.showErrorMessage(`${Organization.name} - ${MessageStatus.ERROR}: ${error.message}`);
Logger.logError(`Error: ${error.stack}`);
const foundFiles = (await FileService.findFilesService()) || [];
await ScanService.addWatcherToManifestFiles(foundFiles, context);
} catch (error) {
ErrorHandler.handleError(error);
} finally {
Logger.logInfo("command register has been completed");
Logger.logInfo("Command registration has been completed");
}
}

private static registerCommand(
context: vscode.ExtensionContext,
command: string,
callback: (...args: any[]) => any,
) {
context.subscriptions.push(vscode.commands.registerCommand(command, callback));
}
}
1 change: 1 addition & 0 deletions src/constants/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class Messages {

// Quick Pick Messages
static readonly QUICK_PICK_FLAG = "Select a flag to use (optional).";
static readonly QUICK_PICK_TOKEN = "Select a token to update";
static readonly QUICK_PICK_SUB_COMMAND = "Select a sub-command to use (optional).";

// Command Execution Messages
Expand Down
1 change: 1 addition & 0 deletions src/constants/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as os from "os";
export class Organization {
static readonly name = "debricked";

static readonly nameCaps = "Debricked";
// Command and OS-specific constants
static readonly command = Organization.getPlatformSpecificCommand();
static readonly osPlatform = os.platform();
Expand Down
5 changes: 3 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export async function activate(context: vscode.ExtensionContext) {
await vscode.window.withProgress(
{
location: vscode.ProgressLocation.Notification,
title: "Debricked",
title: Organization.nameCaps,
cancellable: false,
},
async (progress) => {
Expand Down Expand Up @@ -50,8 +50,9 @@ export async function activate(context: vscode.ExtensionContext) {
await BaseCommandService.installCommand();
}

fetchRepositories();
await fetchRepositories();
progress.report({ message: "Debricked extension is ready to use", increment: 100 - progressCount });
await new Promise((resolve) => setTimeout(resolve, 1000)); // added for showing the last progress info
},
);
}
Expand Down
7 changes: 3 additions & 4 deletions src/helpers/AuthHelper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as vscode from "vscode";
import { Messages, Organization } from "../constants/index";
import { GlobalState, Logger, ShowInputBoxHelper } from ".";
import { GlobalState, Logger, ShowInputBoxHelper, StatusBarMessageHelper } from ".";

export class AuthHelper {
private static get globalState(): GlobalState {
Expand Down Expand Up @@ -36,7 +35,7 @@ export class AuthHelper {
if (token) {
await AuthHelper.globalState.setSecretData(TOKEN_KEY, token);
const message = tokenKey === "access" ? Messages.ACCESS_TOKEN_SAVED : Messages.BEARER_TOKEN_SAVED;
vscode.window.showInformationMessage(message);
StatusBarMessageHelper.showInformationMessage(message);
} else {
const message = tokenKey === "access" ? Messages.ACCESS_TOKEN_RQD : Messages.BEARER_TOKEN_RQD;
throw new Error(message);
Expand All @@ -45,7 +44,7 @@ export class AuthHelper {

return token;
} catch (error: any) {
await vscode.window.showErrorMessage(error.message);
await StatusBarMessageHelper.showErrorMessage(error.message);
Logger.logError("Token input was empty or some other error occurred");
return undefined;
}
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/apiHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class ApiHelper {
): Promise<AxiosResponse<any>> {
try {
// Get the bearer token
const token = await AuthHelper.getToken(false, "bearer");
const token = await AuthHelper.getToken(true, "bearer");
Logger.logDebug(`Fetched Token: ${token}`); // Log the token for debugging

// Define the URL with query parameters
Expand Down
10 changes: 10 additions & 0 deletions src/helpers/errorHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Logger } from "./loggerHelper";
import { StatusBarMessageHelper } from "./statusBarMessageHelper";

export class ErrorHandler {
public static handleError(error: unknown) {
const errorMessage = error instanceof Error ? error.message : String(error);
StatusBarMessageHelper.showErrorMessage(errorMessage);
Logger.logError(`Error: ${error instanceof Error ? error.stack : errorMessage}`);
}
}
2 changes: 2 additions & 0 deletions src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ShowInputBoxHelper } from "./showInputBoxHelper";
import { DebrickedDataHelper } from "./debrickedDataHelper";
import { GlobalState } from "./globalState";
import { ApiHelper } from "./apiHelper";
import { ErrorHandler } from "./errorHandler";

export {
AuthHelper,
Expand All @@ -30,4 +31,5 @@ export {
DebrickedDataHelper,
GlobalState,
ApiHelper,
ErrorHandler,
};
6 changes: 3 additions & 3 deletions src/helpers/installHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as path from "path";
import * as os from "os";
import { exec } from "child_process";
import { Messages, MessageStatus, Organization } from "../constants/index";
import { Logger } from "../helpers";
import { Logger, StatusBarMessageHelper } from "../helpers";
import * as vscode from "vscode";

export class InstallHelper {
Expand Down Expand Up @@ -50,7 +50,7 @@ export class InstallHelper {
await vscode.window.withProgress(
{
location: vscode.ProgressLocation.Notification,
title: "Debricked",
title: Organization.nameCaps,
cancellable: false,
},
async (progress) => {
Expand All @@ -65,7 +65,7 @@ export class InstallHelper {
const installOutput = await this.executeCommand(installCommand);
Logger.logMessageByStatus(MessageStatus.INFO, `${installOutput}`);
Logger.logMessageByStatus(MessageStatus.INFO, `${Messages.INSTALLATION_SUCCESS}`);
vscode.window.showInformationMessage("Debricked: CLI installed successful");
StatusBarMessageHelper.showInformationMessage("CLI installed successfully");
} catch (error: any) {
vscode.window.showErrorMessage(error.message);
Logger.logMessageByStatus(MessageStatus.ERROR, `${Messages.INSTALLATION_ERROR}: ${error.stack}`);
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/loggerHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ export class Logger {

public static initialize(context: vscode.ExtensionContext) {
const logDir = context.logUri.fsPath;
this.logFilePath = path.join(logDir, Organization.logFile);
Logger.logFilePath = path.join(logDir, Organization.logFile);

// Ensure the log directory exists
DebrickedDataHelper.createDir(logDir);
}

public static async openLogFile() {
const logUri = vscode.Uri.file(this.logFilePath);
const logUri = vscode.Uri.file(Logger.logFilePath);
const document = await vscode.workspace.openTextDocument(logUri);
await vscode.window.showTextDocument(document);
}
Expand Down
10 changes: 5 additions & 5 deletions src/helpers/messageHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ export class StatusMessage {
public static getStatusMessage(type: string, command: string): string {
switch (type) {
case MessageStatus.START:
return `${Organization.name} - ${command} started.`;
return `${Organization.nameCaps} - ${command} started.`;
case MessageStatus.COMPLETE:
return `${Organization.name} - ${command} completed.`;
return `${Organization.nameCaps} - ${command} completed.`;
case MessageStatus.ERROR:
return `${Organization.name} - ${command} encountered an error.`;
return `${Organization.nameCaps} - ${command} encountered an error.`;
case MessageStatus.FINISHED:
return `${Organization.name} - ${command} finished.`;
return `${Organization.nameCaps} - ${command} finished.`;
default:
return `${Organization.name} - ${command} unknown status.`;
return `${Organization.nameCaps} - ${command} unknown status.`;
}
}
}
5 changes: 3 additions & 2 deletions src/helpers/showInputBoxHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ export class ShowInputBoxHelper {
inputBoxOptions.value = options.value;
}

const input = vscode.window.showInputBox(inputBoxOptions);
if (input) {
const input = await vscode.window.showInputBox(inputBoxOptions);

if (input !== undefined) {
return input;
}

Expand Down
7 changes: 6 additions & 1 deletion src/helpers/statusBarMessageHelper.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Organization } from "../constants";
import * as vscode from "vscode";

export class StatusBarMessageHelper {
Expand All @@ -11,6 +12,10 @@ export class StatusBarMessageHelper {
}

public static showErrorMessage(message: string): void {
vscode.window.showErrorMessage(message);
vscode.window.showErrorMessage(`${Organization.nameCaps}: ` + message);
}

public static showInformationMessage(message: string): void {
vscode.window.showInformationMessage(`${Organization.nameCaps}: ` + message);
}
}
35 changes: 6 additions & 29 deletions src/services/baseCommandService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
InstallHelper,
Common,
AuthHelper,
ErrorHandler,
} from "../helpers";
import * as vscode from "vscode";
import { GlobalState } from "helpers/globalState";
Expand Down Expand Up @@ -55,13 +56,7 @@ export class BaseCommandService {
break;
}
} catch (error: any) {
StatusBarMessageHelper.showErrorMessage(
`${Organization.name} - ${DebrickedCommands.BASE_COMMAND.cli_command} ${MessageStatus.ERROR}: ${error.message}`,
);
StatusBarMessageHelper.setStatusBarMessage(
StatusMessage.getStatusMessage(MessageStatus.ERROR, DebrickedCommands.BASE_COMMAND.cli_command),
);
Logger.logMessageByStatus(MessageStatus.ERROR, error.stack);
ErrorHandler.handleError(error);
} finally {
StatusBarMessageHelper.setStatusBarMessage(
StatusMessage.getStatusMessage(MessageStatus.FINISHED, DebrickedCommands.BASE_COMMAND.cli_command),
Expand Down Expand Up @@ -97,13 +92,7 @@ export class BaseCommandService {
StatusMessage.getStatusMessage(MessageStatus.COMPLETE, DebrickedCommands.BASE_COMMAND.cli_command),
);
} catch (error: any) {
StatusBarMessageHelper.showErrorMessage(
`${Organization.name} - ${DebrickedCommands.BASE_COMMAND.cli_command} ${MessageStatus.ERROR}: ${error.message}`,
);
StatusBarMessageHelper.setStatusBarMessage(
StatusMessage.getStatusMessage(MessageStatus.ERROR, DebrickedCommands.BASE_COMMAND.cli_command),
);
Logger.logMessageByStatus(MessageStatus.ERROR, error.stack);
throw error;
} finally {
StatusBarMessageHelper.setStatusBarMessage(
StatusMessage.getStatusMessage(MessageStatus.FINISHED, DebrickedCommands.BASE_COMMAND.cli_command),
Expand Down Expand Up @@ -132,13 +121,7 @@ export class BaseCommandService {
`${Organization.extensionVersionKey}: ${BaseCommandService.globalState.getGlobalData(Organization.extensionVersionKey, "")}`,
);
} catch (error: any) {
StatusBarMessageHelper.showErrorMessage(
`${Organization.name} - ${DebrickedCommands.BASE_COMMAND.command} ${MessageStatus.ERROR}: ${error.message}`,
);
StatusBarMessageHelper.setStatusBarMessage(
StatusMessage.getStatusMessage(MessageStatus.ERROR, DebrickedCommands.BASE_COMMAND.command),
);
Logger.logMessageByStatus(MessageStatus.ERROR, error.stack);
ErrorHandler.handleError(error);
} finally {
StatusBarMessageHelper.setStatusBarMessage(
StatusMessage.getStatusMessage(MessageStatus.FINISHED, DebrickedCommands.BASE_COMMAND.command),
Expand All @@ -157,7 +140,7 @@ export class BaseCommandService {

let selectedSubCommand: any;
if (subCommand) {
selectedSubCommand = await QuickPick.showQuickPick(subCommand, Messages.QUICK_PICK_FLAG);
selectedSubCommand = await QuickPick.showQuickPick(subCommand, Messages.QUICK_PICK_TOKEN);
}
switch (selectedSubCommand?.cli_command) {
case "accessToken":
Expand All @@ -168,13 +151,7 @@ export class BaseCommandService {
break;
}
} catch (error: any) {
StatusBarMessageHelper.showErrorMessage(
`${Organization.name} - ${DebrickedCommands.BASE_COMMAND.command} ${MessageStatus.ERROR}: ${error.message}`,
);
StatusBarMessageHelper.setStatusBarMessage(
StatusMessage.getStatusMessage(MessageStatus.ERROR, DebrickedCommands.BASE_COMMAND.command),
);
Logger.logMessageByStatus(MessageStatus.ERROR, error.stack);
throw error;
} finally {
StatusBarMessageHelper.setStatusBarMessage(
StatusMessage.getStatusMessage(MessageStatus.FINISHED, DebrickedCommands.BASE_COMMAND.command),
Expand Down
Loading

0 comments on commit d57742a

Please sign in to comment.