Skip to content

Commit 16c99da

Browse files
committed
fix(output): update output texts
1 parent 2a1f83f commit 16c99da

File tree

4 files changed

+54
-47
lines changed

4 files changed

+54
-47
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ $ git-merged-branches --delete
4646
fix/crash-on-start
4747
feature/add-new-feature
4848

49-
Deleting branches locally...
50-
Deleting branches remotely...
49+
Deleting branches locally
50+
Deleting branches remotely
5151
Branches deleted successfully.
5252
```
5353

src/output.ts

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { logError, pluralize } from "./helpers.js";
2-
import { deleteLocalBranches, deleteRemoteBranches, fetchRemoteBranches } from "./repo.js";
1+
import { pluralize } from "./helpers.js";
2+
import { deleteBranches, fetchRemoteBranches } from "./repo.js";
33
import { isValidURL } from "./validate.js";
44
import type { GitMergedConfig, GitMergedOptions } from "./types.js";
55

@@ -40,30 +40,26 @@ export function outputMergedBranches(
4040
config: GitMergedConfig,
4141
options: GitMergedOptions = {}
4242
): void {
43-
if (!branches.length) {
44-
return console.info(`No branches merged into '${targetBranch}'.`);
45-
}
43+
if (!branches.length) { return console.info(`No branches merged into '${targetBranch}'.`); }
44+
45+
const pluralized = pluralize(branches.length, ["branch", "branches"]);
4646

47-
console.info(`${pluralize(branches.length, ["branch", "branches"])} merged into '${targetBranch}':`);
47+
console.info(`${pluralized} merged into '${targetBranch}':`);
4848
console.info(formatTaskBranches(branches, config).join("\n"));
4949

5050
const remoteBranches = fetchRemoteBranches("origin");
5151
const remoteMerged = branches.filter(branch => remoteBranches.includes(branch));
52-
if (!options.deleteBranches) {
53-
console.info("\nRun the following to delete branches, or use the --delete option to delete them automatically:");
54-
console.info(`locally:\n git branch --delete ${branches.join(" ")}`);
55-
if (remoteMerged.length) { console.info(`remotely:\n git push origin --delete ${remoteMerged.join(" ")}`); }
56-
} else {
57-
try {
58-
console.info("\nDeleting branches locally...");
59-
deleteLocalBranches(branches);
60-
if (remoteMerged.length) {
61-
console.info("\nDeleting branches remotely...");
62-
deleteRemoteBranches(remoteMerged);
63-
}
64-
console.info("Branches deleted successfully.");
65-
} catch (error) {
66-
logError("Failed to delete branches", error);
67-
}
52+
if (options.deleteBranches) {
53+
deleteBranches(branches, remoteMerged);
54+
console.info("Branches deleted successfully.");
55+
return;
56+
}
57+
58+
console.info(`\nUse --delete to delete ${pluralized} automatically.`);
59+
console.info("\nDelete locally:");
60+
console.info(` git branch --delete ${branches.join(" ")}`);
61+
if (remoteMerged.length) {
62+
console.info("\nDelete remotely:");
63+
console.info(` git push origin --delete ${remoteMerged.join(" ")}`);
6864
}
6965
}

src/repo.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { execSync } from "node:child_process";
2+
import { cwd } from "node:process";
23
import { readFileSync } from "node:fs";
34
import { join } from "node:path";
45
import { logError } from "./helpers";
@@ -67,7 +68,7 @@ export function fetchRemoteBranches(remote = "origin"): string[] {
6768

6869
export function getConfig(): GitMergedConfig {
6970
try {
70-
const pkgPath = join(process.cwd(), "package.json");
71+
const pkgPath = join(cwd(), "package.json");
7172
const pkg = JSON.parse(readFileSync(pkgPath, "utf-8"));
7273
return pkg["git-merged-branches"] || {};
7374
} catch (error) {
@@ -76,10 +77,21 @@ export function getConfig(): GitMergedConfig {
7677
}
7778
}
7879

79-
export function deleteLocalBranches(branches: string[]): void {
80+
function deleteLocalBranches(branches: string[]): void {
8081
execSync(`git branch --delete ${branches.join(" ")}`, { stdio: "inherit" });
8182
}
82-
83-
export function deleteRemoteBranches(branches: string[]): void {
83+
function deleteRemoteBranches(branches: string[]): void {
8484
execSync(`git push origin --delete ${branches.join(" ")}`, { stdio: "inherit" });
8585
}
86+
export function deleteBranches(localBranches: string[], remoteBranches: string[]): void {
87+
try {
88+
console.info("\nDeleting branches locally…");
89+
deleteLocalBranches(localBranches);
90+
if (remoteBranches.length) {
91+
console.info("\nDeleting branches remotely…");
92+
deleteRemoteBranches(remoteBranches);
93+
}
94+
} catch (error) {
95+
logError("Failed to delete branches", error);
96+
}
97+
}

src/tests/output.test.ts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,10 @@ describe("outputMergedBranches", () => {
8585
expect(infoSpy).toHaveBeenNthCalledWith(2, branchOutput.join("\n"));
8686

8787
const localDelete = `git branch --delete ${branches.join(" ")}`;
88-
expect(infoSpy).toHaveBeenNthCalledWith(3, "\nRun the following to delete branches, or use the --delete option to delete them automatically:");
89-
expect(infoSpy).toHaveBeenNthCalledWith(4, `locally:\n ${localDelete}`);
90-
expect(infoSpy).toHaveBeenCalledTimes(4);
88+
expect(infoSpy).toHaveBeenNthCalledWith(3, "\nUse --delete to delete 2 branches automatically.");
89+
expect(infoSpy).toHaveBeenNthCalledWith(4, "\nDelete locally:");
90+
expect(infoSpy).toHaveBeenNthCalledWith(5, ` ${localDelete}`);
91+
expect(infoSpy).toHaveBeenCalledTimes(5);
9192
expect(warnSpy).not.toHaveBeenCalled();
9293
});
9394

@@ -105,10 +106,12 @@ describe("outputMergedBranches", () => {
105106

106107
const localDelete = `git branch --delete ${branches.join(" ")}`;
107108
const remoteDelete = `git push origin --delete ${branches.join(" ")}`;
108-
expect(infoSpy).toHaveBeenNthCalledWith(3, "\nRun the following to delete branches, or use the --delete option to delete them automatically:");
109-
expect(infoSpy).toHaveBeenNthCalledWith(4, `locally:\n ${localDelete}`);
110-
expect(infoSpy).toHaveBeenNthCalledWith(5, `remotely:\n ${remoteDelete}`);
111-
expect(infoSpy).toHaveBeenCalledTimes(5);
109+
expect(infoSpy).toHaveBeenNthCalledWith(3, "\nUse --delete to delete 2 branches automatically.");
110+
expect(infoSpy).toHaveBeenNthCalledWith(4, "\nDelete locally:");
111+
expect(infoSpy).toHaveBeenNthCalledWith(5, ` ${localDelete}`);
112+
expect(infoSpy).toHaveBeenNthCalledWith(6, "\nDelete remotely:");
113+
expect(infoSpy).toHaveBeenNthCalledWith(7, ` ${remoteDelete}`);
114+
expect(infoSpy).toHaveBeenCalledTimes(7);
112115
expect(warnSpy).not.toHaveBeenCalled();
113116

114117
fetchRemoteMock.mockRestore();
@@ -122,9 +125,10 @@ describe("outputMergedBranches", () => {
122125
expect(infoSpy).toHaveBeenNthCalledWith(2, "feat/TOKEN-800_new-feature <https://test-instance.org/browse/TOKEN-800>");
123126

124127
const localDelete = `git branch --delete ${branches.join(" ")}`;
125-
expect(infoSpy).toHaveBeenNthCalledWith(3, "\nRun the following to delete branches, or use the --delete option to delete them automatically:");
126-
expect(infoSpy).toHaveBeenNthCalledWith(4, `locally:\n ${localDelete}`);
127-
expect(infoSpy).toHaveBeenCalledTimes(4);
128+
expect(infoSpy).toHaveBeenNthCalledWith(3, "\nUse --delete to delete 1 branch automatically.");
129+
expect(infoSpy).toHaveBeenNthCalledWith(4, "\nDelete locally:");
130+
expect(infoSpy).toHaveBeenNthCalledWith(5, ` ${localDelete}`);
131+
expect(infoSpy).toHaveBeenCalledTimes(5);
128132
expect(warnSpy).not.toHaveBeenCalled();
129133
});
130134

@@ -140,28 +144,23 @@ describe("outputMergedBranches", () => {
140144
const config = { ...DEFAULT_CONFIG, issueUrlFormat: "invalid-url" };
141145

142146
outputMergedBranches(branches, "master", config);
143-
expect(infoSpy).toHaveBeenCalledTimes(4);
147+
expect(infoSpy).toHaveBeenCalledTimes(5);
144148
expect(warnSpy).toHaveBeenCalledWith("'invalid-url' is not a valid URL. Skipped formatting.");
145149
expect(warnSpy).toHaveBeenCalledTimes(1);
146150
});
147151

148152
it("should delete branches when --delete option is passed", () => {
149-
const deleteLocalMock = vi.spyOn(repoMethods, "deleteLocalBranches").mockImplementation(() => {});
150-
const deleteRemoteMock = vi.spyOn(repoMethods, "deleteRemoteBranches").mockImplementation(() => {});
153+
const deleteMock = vi.spyOn(repoMethods, "deleteBranches").mockImplementation(() => {});
151154

152155
const branches = ["feat/TOKEN-800_new-feature", "fix/TOKEN-123_some-fix"];
153156
const fetchRemoteMock = vi.spyOn(repoMethods, "fetchRemoteBranches").mockReturnValue(branches);
154157

155158
outputMergedBranches(branches, "master", DEFAULT_CONFIG, { deleteBranches: true });
156159

157-
expect(deleteLocalMock).toHaveBeenCalledWith(branches);
158-
expect(deleteRemoteMock).toHaveBeenCalledWith(branches);
159-
expect(infoSpy).toHaveBeenCalledWith("\nDeleting branches locally...");
160-
expect(infoSpy).toHaveBeenCalledWith("\nDeleting branches remotely...");
160+
expect(deleteMock).toHaveBeenCalledWith(branches, branches);
161161
expect(infoSpy).toHaveBeenCalledWith("Branches deleted successfully.");
162162

163-
deleteLocalMock.mockRestore();
164-
deleteRemoteMock.mockRestore();
163+
deleteMock.mockRestore();
165164
fetchRemoteMock.mockRestore();
166165
});
167166
});

0 commit comments

Comments
 (0)