From d4adbc7db47dbfae61e07d906ee4c56232aad50a Mon Sep 17 00:00:00 2001 From: Sean Boult <996134+Hacksore@users.noreply.github.com> Date: Sat, 24 Feb 2024 08:59:40 -0600 Subject: [PATCH] Refactor --- src/presenter.ts | 107 ++++++++++++++++++++++++++--------------------- 1 file changed, 59 insertions(+), 48 deletions(-) diff --git a/src/presenter.ts b/src/presenter.ts index 648d0f0..3a76380 100644 --- a/src/presenter.ts +++ b/src/presenter.ts @@ -37,10 +37,10 @@ export class Presenter { this.ticketRegex = input.ticketRegex; this.useTicketRegex = input.useTicketRegex; this.pushOnSave = input.pushOnSave; - this.statusBarItem = config.statusBarItem; + this.statusBarItem = input.statusBarItem; } - static getInstance(config: PresenterConfig): Presenter { + static getInstance(config: PresenterInput): Presenter { if (!Presenter.instance) { Presenter.instance = new Presenter(config); } @@ -80,57 +80,67 @@ export class Presenter { } } + enableOnSaveListener() { + // TODO: can we optimize this better so it only goes on load of the extension + this.disposableForOnSaveListener = vscode.workspace.onDidSaveTextDocument( + (doc) => { + if (!this._enabled) { + return; + } + + const parentDir = getParentDir(doc.uri); + if (dirIsGit(parentDir)) { + //if so then go and get last git log + let commitMessage = dirGetLastLogMessage(parentDir); + + // if the user has a custom commit message + if (this.customCommitMessage !== undefined) { + commitMessage = this.customCommitMessage; + } + + // if the user has a ticket regex + if (this.useTicketRegex && this.ticketRegex !== undefined) { + // get the branch that the user has checked out + const branches = execSync("git branch -v", { + cwd: parentDir.fsPath, + }).toString(); + const currentBranchLineMatches = branches.match(/\* (.*)/); + + // [WIP] {{ticket}} I Am doing the needful" + if (currentBranchLineMatches && currentBranchLineMatches[1]) { + const ticketNumberMatches = currentBranchLineMatches[1].match( + this.ticketRegex + ); + + if (ticketNumberMatches && ticketNumberMatches[1]) { + commitMessage = this.customCommitMessage?.replace( + "{{ticket}}", + ticketNumberMatches[1] + ); + } + } + } + + //create git commit + this.gitCommit(commitMessage, doc.uri); + } + //Letter have a setting to allow initializing repo incase no git + } + ); + } + setupCommands(context: vscode.ExtensionContext) { let disposable = vscode.commands.registerCommand( "save-me-baby.start-saving", () => { this._enabled = true; - this.disposableForOnSaveListener = - vscode.workspace.onDidSaveTextDocument((doc) => { - if (this._enabled) { - //get the parent dir of the file - const parentDir = getParentDir(doc.uri); - if (dirIsGit(parentDir)) { - //if so then go and get last git log - let commitMessage = dirGetLastLogMessage(parentDir); - - // if the user has a custom commit message - if (this.customCommitMessage !== undefined) { - commitMessage = this.customCommitMessage; - } - - // if the user has a ticket regex - if (this.useTicketRegex && this.ticketRegex !== undefined) { - // get the branch that the user has checked out - const branches = execSync("git branch -v", { - cwd: parentDir.fsPath, - }).toString(); - const currentBranchLineMatches = branches.match(/\* (.*)/); - - // [WIP] {{ticket}} I Am doing the needful" - if (currentBranchLineMatches && currentBranchLineMatches[1]) { - const ticketNumberMatches = - currentBranchLineMatches[1].match(this.ticketRegex); - - if (ticketNumberMatches && ticketNumberMatches[1]) { - commitMessage = this.customCommitMessage?.replace( - "{{ticket}}", - ticketNumberMatches[1] - ); - } - } - } - - //create git commit - this.gitCommit(commitMessage, doc.uri); - } - //Letter have a setting to allow initializing repo incase no git - } - }); + this.enableOnSaveListener(); + vscode.window.showInformationMessage("Starting to Save You 😄!"); return true; } ); + context.subscriptions.push(disposable); disposable = vscode.commands.registerCommand( "save-me-baby.stop-saving", @@ -138,22 +148,23 @@ export class Presenter { this.disposableForOnSaveListener?.dispose(); this.disposableForOnSaveListener = null; this._enabled = false; - vscode.window.showInformationMessage("Stoping to Save Save you 😥!"); + vscode.window.showInformationMessage("Stopping to Save You 😥!"); return true; } ); context.subscriptions.push(disposable); disposable = vscode.commands.registerCommand("save-me-baby.toggle", () => { - if (this.enabled) { + this.toggle(); + + if (!this.enabled) { this.disposableForOnSaveListener?.dispose(); this.disposableForOnSaveListener = null; - vscode.window.showInformationMessage("Stopping to Save Save you 😥!"); + vscode.window.showInformationMessage("Stopping to Save You 😥!"); } else { vscode.window.showInformationMessage("Starting to Save You 😄!"); } - this.toggle(); return true; }); context.subscriptions.push(disposable);