Skip to content

Commit 16e202a

Browse files
committed
feat: add .ts files for .sh logic and remove unnecessary cli.ts
1 parent e6ad636 commit 16e202a

File tree

5 files changed

+199
-43
lines changed

5 files changed

+199
-43
lines changed

src/bin/diffIndexGenerate.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/env node
2+
/**
3+
* AutoCommitMsg CLI scirpt.
4+
*
5+
* Mirrors the behavior of `shell/acm.sh` in TypeScript.
6+
*/
7+
import { execFileSync } from "child_process";
8+
import { generateMsg } from "../prepareCommitMsg";
9+
import { shouldShowHelp } from "./utils";
10+
11+
/**
12+
* CLI usage help text.
13+
*/
14+
const HELP_TEXT: string = `Usage: acm [--cached] [--help|-h]
15+
16+
Check Git changes and generate a commit message.
17+
18+
Options:
19+
--cached Use only staged changes (equivalent to git --cached).
20+
--help, -h Show this help and exit.`;
21+
22+
/**
23+
* Run `git diff-index` and return its stdout as a string.
24+
*
25+
* TODO: Use _diffIndex instead after refactoring for flags.
26+
* @param useCached When true, include only staged changes using `--cached`.
27+
*
28+
* @returns output Diff output from git.
29+
*/
30+
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+
];
38+
39+
if (useCached) {
40+
flags.push("--cached");
41+
}
42+
43+
flags.push("HEAD");
44+
45+
const output: string = execFileSync("git", flags, {
46+
encoding: "utf8",
47+
stdio: ["ignore", "pipe", "pipe"],
48+
});
49+
50+
return output.trim();
51+
}
52+
53+
/**
54+
* Generate a commit message from the current repository diff.
55+
*
56+
* @param useCached When true, include only staged changes using `--cached`.
57+
* @returns Generated commit message text.
58+
*/
59+
export function generateCommitMessage(useCached: boolean): string {
60+
const diffOutput: string = runGitDiff(useCached);
61+
if (!diffOutput) {
62+
throw new Error("No file changes found");
63+
}
64+
65+
const lines: string[] = diffOutput.split("\n");
66+
return generateMsg(lines);
67+
}
68+
69+
/**
70+
* Command-line entry-point.
71+
*
72+
* Accepts an optional `--cached` flag to use staged changes only.
73+
* Prints the generated commit message to stdout.
74+
*/
75+
function main(argv: string[]): void {
76+
if (shouldShowHelp(argv)) {
77+
console.log(HELP_TEXT);
78+
return;
79+
}
80+
const useCached: boolean = argv.includes("--cached");
81+
const msg: string = generateCommitMessage(useCached);
82+
console.log(msg);
83+
}
84+
85+
if (require.main === module) {
86+
main(process.argv.slice(2));
87+
}

src/bin/diffIndexGenerateCommit.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env node
2+
/**
3+
* Git commit AutoCommitMsg script.
4+
*
5+
* Wrapper around acm.ts to generate a message and commit it in edit mode.
6+
*/
7+
import { execFileSync } from "child_process";
8+
import { generateCommitMessage } from "./diffIndexGenerate";
9+
import { shouldShowHelp } from "./utils";
10+
11+
/**
12+
* CLI usage help text.
13+
*/
14+
const HELP_TEXT: string = `Usage: gacm [--cached] [--help|-h]
15+
16+
Check Git changes, generate a commit message, and run Git commit.
17+
18+
Options:
19+
--cached Use only staged changes (equivalent to git --cached).
20+
--help, -h Show this help and exit.`;
21+
22+
/**
23+
* Command-line entry-point.
24+
*/
25+
function main(argv: string[]): void {
26+
if (shouldShowHelp(argv)) {
27+
console.log(HELP_TEXT);
28+
return;
29+
}
30+
31+
const useCached: boolean = argv.includes("--cached");
32+
const passthrough: string[] = argv.filter((arg: string) => arg !== "--cached");
33+
34+
const msg: string = generateCommitMessage(useCached);
35+
36+
const commitArgs: string[] = ["commit", "--edit", "-m", msg, ...passthrough];
37+
execFileSync("git", commitArgs, { stdio: "inherit" });
38+
}
39+
40+
if (require.main === module) {
41+
main(process.argv.slice(2));
42+
}

src/bin/generate.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env node
2+
/**
3+
* CLI module to generate a commit message using given input.
4+
*
5+
* This script does not interact with VS Code or Git, it only processes text.
6+
*
7+
* It simply receives text as an argument and prints output to `stdout` for use
8+
* in a hook flow. Or to `stderr`, in the case of a message not appropriate for
9+
* a commit message.
10+
*/
11+
import { generateMsg } from "../prepareCommitMsg";
12+
13+
const HELP_TEXT: string = `Usage: auto_commit_msg_generate DIFF_INDEX_OUTPUT
14+
15+
Generate a commit message from given input.
16+
17+
Arguments:
18+
DIFF_INDEX_OUTPUT Text output from 'git diff-index --name-status HEAD'.
19+
20+
Options:
21+
--help, -h Show this help and exit.`;
22+
23+
24+
/**
25+
* Command-line entry-point.
26+
*
27+
* Returns a suitable generated commit message as text.
28+
*/
29+
import { shouldShowHelp } from "./utils";
30+
31+
/**
32+
* Entry-point for the CLI. Handles help flag and input validation.
33+
*/
34+
function main(args: string[]): void {
35+
if (shouldShowHelp(args)) {
36+
console.log(HELP_TEXT);
37+
return;
38+
}
39+
const linesArg = args[0];
40+
41+
if (typeof linesArg === "undefined") {
42+
throw new Error("Exactly one argument is required - text output from diff-index command.");
43+
}
44+
45+
const lines = linesArg.split("\n");
46+
47+
if (!lines.length) {
48+
throw new Error("No file changes found");
49+
}
50+
51+
const msg = generateMsg(lines);
52+
console.log(msg);
53+
}
54+
55+
const args = process.argv.slice(2);
56+
main(args);

src/bin/utils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* CLI utilities shared by bin commands.
3+
*/
4+
5+
/**
6+
* Determine whether help should be displayed.
7+
*
8+
* @param argv Command-line arguments passed to the CLI.
9+
*
10+
* @returns boolean True when `--help` or `-h` is present.
11+
*/
12+
export function shouldShowHelp(argv: string[]): boolean {
13+
return argv.includes("--help") || argv.includes("-h");
14+
}

src/cli.ts

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)