-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathdiffIndexGenerate.ts
More file actions
81 lines (71 loc) · 2.1 KB
/
diffIndexGenerate.ts
File metadata and controls
81 lines (71 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env node
/**
* AutoCommitMsg CLI script - to check Git changes and generate a commit message.
*/
import { execFileSync } from "child_process";
import { Repository } from "../api/git";
import { getChanges } from "../git/cli";
import { NO_LINES_MSG } from "../lib/constants";
import { generateMsg } from "../prepareCommitMsg";
import { shouldShowHelp } from "./utils";
const HELP_TEXT: string = `Usage: acm [--help|-h]
Check Git changes and output a generated commit message.
Options:
--help, -h Show this help and exit.
--verbose Show debug output for this run.
`;
/**
* Get the current repository by running git command.
*
* Executes git command to determine the root directory of the current Git
* repository.
*
* @returns A Repository object with the root URI of the current Git repository.
* @throws Error if not in a Git repository or if the git command fails.
*/
function _getCurrentRepository(): Repository {
return {
rootUri: {
fsPath: execFileSync("git", ["rev-parse", "--show-toplevel"], {
encoding: "utf8",
}).trim(),
},
} as Repository;
}
/**
* Generate a commit message from the current repository diff.
*
* @returns Generated commit message text.
*/
export async function generateCommitMessage(): Promise<string> {
const repo = _getCurrentRepository();
const fileChanges: string[] = await getChanges(repo);
if (!fileChanges.length) {
throw new Error(NO_LINES_MSG);
}
return generateMsg(fileChanges);
}
/**
* Command-line entry-point.
*
* Prints the generated commit message to stdout.
*/
async function main(argv: string[]): Promise<void> {
if (shouldShowHelp(argv)) {
console.log(HELP_TEXT);
return;
}
const verbose: boolean = argv.includes("--verbose");
if (verbose) {
process.env.ACM_DEBUG = "1";
}
const msg: string = await generateCommitMessage();
console.log(msg);
}
if (require.main === module) {
main(process.argv.slice(2)).catch((err: unknown) => {
const message: string = err instanceof Error ? err.message : String(err);
console.error(`Error: ${message}`);
process.exit(1);
});
}