Skip to content

Commit ec6151b

Browse files
committed
Refactor CLI scripts for improved readability and structure. Consolidate git diff flags into a constant and enhance error handling. Update docstrings for clarity.
1 parent f1f3462 commit ec6151b

3 files changed

Lines changed: 14 additions & 22 deletions

File tree

src/cli/diffIndexGenerate.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
#!/usr/bin/env node
22
/**
3-
* AutoCommitMsg CLI scirpt.
4-
*
5-
* Mirrors the behavior of `shell/acm.sh` in TypeScript.
3+
* AutoCommitMsg CLI script.
64
*/
75
import { execFileSync } from "child_process";
86
import { generateMsg } from "../prepareCommitMsg";
97
import { shouldShowHelp } from "./utils";
108

11-
/**
12-
* CLI usage help text.
13-
*/
149
const HELP_TEXT: string = `Usage: acm [--cached] [--help|-h]
1510
1611
Check Git changes and generate a commit message.
@@ -19,6 +14,14 @@ Options:
1914
--cached Use only staged changes (equivalent to git --cached).
2015
--help, -h Show this help and exit.`;
2116

17+
const DIFF_FLAGS = [
18+
"diff-index",
19+
"--name-status",
20+
"--find-renames",
21+
"--find-copies",
22+
"--no-color",
23+
];
24+
2225
/**
2326
* Run `git diff-index` and return its stdout as a string.
2427
*
@@ -28,18 +31,11 @@ Options:
2831
* @returns output Diff output from git.
2932
*/
3033
function runGitDiff(useCached: boolean): string {
31-
const flags: string[] = [
32-
"diff-index",
33-
"--name-status",
34-
"--find-renames",
35-
"--find-copies",
36-
"--no-color",
37-
];
34+
const flags: string[] = [...DIFF_FLAGS];
3835

3936
if (useCached) {
4037
flags.push("--cached");
4138
}
42-
4339
flags.push("HEAD");
4440

4541
const output: string = execFileSync("git", flags, {
@@ -84,7 +80,7 @@ function main(argv: string[]): void {
8480

8581
if (require.main === module) {
8682
try {
87-
main(process.argv.slice(2));
83+
main(process.argv.slice(2));
8884
} catch (err) {
8985
const message: string = err instanceof Error ? err.message : String(err);
9086
console.error(`Error: ${message}`);

src/cli/diffIndexGenerateCommit.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
#!/usr/bin/env node
22
/**
33
* Git commit AutoCommitMsg script.
4-
*
5-
* Wrapper around acm.ts to generate a message and commit it in edit mode.
64
*/
75
import { execFileSync } from "child_process";
86
import { generateCommitMessage } from "./diffIndexGenerate";
97
import { shouldShowHelp } from "./utils";
108

11-
/**
12-
* CLI usage help text.
13-
*/
149
const HELP_TEXT: string = `Usage: gacm [--cached] [--help|-h]
1510
1611
Check Git changes, generate a commit message, and run Git commit.
@@ -41,7 +36,7 @@ function main(argv: string[]): void {
4136

4237
if (require.main === module) {
4338
try {
44-
main(process.argv.slice(2));
39+
main(process.argv.slice(2));
4540
} catch (err) {
4641
const message: string = err instanceof Error ? err.message : String(err);
4742
console.error(`Error: ${message}`);

src/cli/generate.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* CLI module to generate a commit message using given input.
44
*
55
* This script does not interact with VS Code or Git, it only processes text.
6+
* This works as a Git hook. See shell/README.md for hook usage.
67
*
78
* It simply receives text as an argument and prints output to `stdout` for use
89
* in a hook flow. Or to `stderr`, in the case of a message not appropriate for
@@ -55,7 +56,7 @@ function main(args: string[]): void {
5556

5657
const args = process.argv.slice(2);
5758
try {
58-
main(args);
59+
main(args);
5960
} catch (err) {
6061
const message: string = err instanceof Error ? err.message : String(err);
6162
console.error(message);

0 commit comments

Comments
 (0)