Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Hacksore committed Feb 29, 2024
1 parent 7307b71 commit d4adbc7
Showing 1 changed file with 59 additions and 48 deletions.
107 changes: 59 additions & 48 deletions src/presenter.ts
Original file line number Diff line number Diff line change
@@ -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,80 +80,91 @@ 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",
() => {
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);

0 comments on commit d4adbc7

Please sign in to comment.