|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * AutoCommitMsg CLI script. |
| 4 | + */ |
| 5 | +import { execFileSync } from "child_process"; |
| 6 | +import { generateMsg } from "../prepareCommitMsg"; |
| 7 | +import { shouldShowHelp } from "./utils"; |
| 8 | + |
| 9 | +const HELP_TEXT: string = `Usage: acm [--cached] [--help|-h] |
| 10 | +
|
| 11 | +Check Git changes and generate a commit message. |
| 12 | +
|
| 13 | +Options: |
| 14 | + --cached Use only staged changes (equivalent to git --cached). |
| 15 | + If the flag is omitted, then the standard \`git status\` logic is followed: |
| 16 | + look for staged changes and use them, otherwise use unstaged changes. |
| 17 | + --help, -h Show this help and exit.`; |
| 18 | + |
| 19 | +const DIFF_FLAGS = [ |
| 20 | + "diff-index", |
| 21 | + "--name-status", |
| 22 | + "--find-renames", |
| 23 | + "--find-copies", |
| 24 | + "--no-color", |
| 25 | +]; |
| 26 | + |
| 27 | +/** |
| 28 | + * Run `git diff-index` and return its stdout as a string. |
| 29 | + * |
| 30 | + * TODO: Use _diffIndex instead after refactoring for flags. |
| 31 | + * @param useCached When true, include only staged changes using `--cached`. |
| 32 | + * |
| 33 | + * @returns output Diff output from git. |
| 34 | + */ |
| 35 | +function runGitDiff(useCached: boolean): string { |
| 36 | + const flags: string[] = [...DIFF_FLAGS]; |
| 37 | + |
| 38 | + if (useCached) { |
| 39 | + flags.push("--cached"); |
| 40 | + } |
| 41 | + flags.push("HEAD"); |
| 42 | + |
| 43 | + const output: string = execFileSync("git", flags, { |
| 44 | + encoding: "utf8", |
| 45 | + stdio: ["ignore", "pipe", "pipe"], |
| 46 | + }); |
| 47 | + |
| 48 | + return output.trim(); |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Generate a commit message from the current repository diff. |
| 53 | + * |
| 54 | + * @param useCached When true, include only staged changes using `--cached`. |
| 55 | + * @returns Generated commit message text. |
| 56 | + */ |
| 57 | +export function generateCommitMessage(useCached: boolean): string { |
| 58 | + const diffOutput: string = runGitDiff(useCached); |
| 59 | + if (!diffOutput) { |
| 60 | + throw new Error("No file changes found"); |
| 61 | + } |
| 62 | + |
| 63 | + const lines: string[] = diffOutput.split("\n"); |
| 64 | + return generateMsg(lines); |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Command-line entry-point. |
| 69 | + * |
| 70 | + * Accepts an optional `--cached` flag to use staged changes only. |
| 71 | + * Prints the generated commit message to stdout. |
| 72 | + */ |
| 73 | +function main(argv: string[]): void { |
| 74 | + if (shouldShowHelp(argv)) { |
| 75 | + console.log(HELP_TEXT); |
| 76 | + return; |
| 77 | + } |
| 78 | + const useCached: boolean = argv.includes("--cached"); |
| 79 | + const msg: string = generateCommitMessage(useCached); |
| 80 | + console.log(msg); |
| 81 | +} |
| 82 | + |
| 83 | +if (require.main === module) { |
| 84 | + try { |
| 85 | + main(process.argv.slice(2)); |
| 86 | + } catch (err) { |
| 87 | + const message: string = err instanceof Error ? err.message : String(err); |
| 88 | + console.error(`Error: ${message}`); |
| 89 | + process.exit(1); |
| 90 | + } |
| 91 | +} |
0 commit comments