-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathgenerate.ts
More file actions
64 lines (53 loc) · 1.58 KB
/
generate.ts
File metadata and controls
64 lines (53 loc) · 1.58 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
#!/usr/bin/env node
/**
* CLI module to generate a commit message using given input.
*
* This script does not interact with VS Code or Git, it only processes text.
* This works as a Git hook. See shell/README.md for hook usage.
*
* It simply receives text as an argument and prints output to `stdout` for use
* in a hook flow. Or to `stderr`, in the case of a message not appropriate for
* a commit message.
*/
import { generateMsg } from "../prepareCommitMsg";
const HELP_TEXT: string = `Usage: auto_commit_msg_generate DIFF_INDEX_OUTPUT
Generate a commit message from given input.
Arguments:
DIFF_INDEX_OUTPUT Text output from 'git diff-index --name-status HEAD'.
Options:
--help, -h Show this help and exit.`;
/**
* Command-line entry-point.
*
* Returns a suitable generated commit message as text.
*/
import { shouldShowHelp } from "./utils";
/**
* Entry-point for the CLI. Handles help flag and input validation.
*/
function main(args: string[]): void {
if (shouldShowHelp(args)) {
console.log(HELP_TEXT);
return;
}
const linesArg = args[0];
if (typeof linesArg === "undefined") {
throw new Error(
"Exactly one argument is required - text output from diff-index command.",
);
}
const lines = linesArg.split("\n");
if (!lines.length) {
throw new Error("No file changes found");
}
const msg = generateMsg(lines);
console.log(msg);
}
const args = process.argv.slice(2);
try {
main(args);
} catch (err) {
const message: string = err instanceof Error ? err.message : String(err);
console.error(message);
process.exit(1);
}