🔄 Refactor commit script to use parseArgs and add flags for help, check-only, and auto-commit functionality

This commit is contained in:
2026-04-12 10:25:22 +08:00
parent 9956702df5
commit 7361e13361

View File

@@ -1,7 +1,6 @@
#!/usr/bin/env runts -- --allow-all #!/usr/bin/env runts -- --allow-all
import {createInterface} from "node:readline/promises"; import {parseArgs} from "jsr:@std/cli/parse-args";
import {stdin, stdout} from "node:process";
import {execCommandShell, log, ProcessBar, term,} from "https://script.hatter.ink/@61/deno-commons-mod.ts"; import {execCommandShell, log, ProcessBar, term,} from "https://script.hatter.ink/@61/deno-commons-mod.ts";
import {summarizeGitStatusDiff} from "https://script.hatter.ink/@0/deno-ai-mod.ts"; import {summarizeGitStatusDiff} from "https://script.hatter.ink/@0/deno-ai-mod.ts";
import {getGitLocalRev, getGitRemoteRev, getGitStatus,} from "https://script.hatter.ink/@2/deno-git-mod.ts"; import {getGitLocalRev, getGitRemoteRev, getGitStatus,} from "https://script.hatter.ink/@2/deno-git-mod.ts";
@@ -21,6 +20,22 @@ async function checkRev(): Promise<boolean> {
} }
async function main() { async function main() {
const flags = parseArgs(Deno.args, {
boolean: ["help", "check-only", "auto-commit"],
});
const checkOnly = flags["check-only"];
const autoCommit = flags["auto-commit"];
if (flags.help) {
console.log(`commit.ts
commit.ts [--help] - show help message
commit.ts [--check-only] - check rev and status only
commit.ts [--auto-commit] - auto commit with AI summarize
`);
return;
}
await checkRev(); // check local rev <--> remote rev equals await checkRev(); // check local rev <--> remote rev equals
const gitStatus = await getGitStatus(); const gitStatus = await getGitStatus();
if (gitStatus === null) { if (gitStatus === null) {
@@ -28,6 +43,10 @@ async function main() {
return; return;
} }
log.info("Git status:", gitStatus); log.info("Git status:", gitStatus);
if (checkOnly) {
log.info("Check only, skip commit.");
return;
}
const summary = await new ProcessBar("AI summarizing").call( const summary = await new ProcessBar("AI summarizing").call(
async (): Promise<string> => { async (): Promise<string> => {
@@ -40,21 +59,23 @@ async function main() {
let emptyCount = 0; let emptyCount = 0;
let message = "empty message"; let message = "empty message";
const readline = createInterface({ input: stdin, output: stdout }); if (autoCommit && summary) {
do { message = summary;
if (emptyCount++ === 3) { } else {
readline.close(); do {
console.log("Too many empty messages, then exit"); if (emptyCount++ === 3) {
return; readline.close();
} console.log("Too many empty messages, then exit");
message = await readline.question( return;
"Input your commit message (Enter for AI summary) > ", }
); message = prompt(
if (message.trim().length == 0) { "Input your commit message (Enter for AI summary) > ",
message = summary ?? ""; );
} if (message.trim().length == 0) {
} while (message.length == 0); message = summary ?? "";
readline.close(); }
} while (message.length == 0);
}
const gitCommitArgs: string[] = []; const gitCommitArgs: string[] = [];
gitCommitArgs.push("commit"); gitCommitArgs.push("commit");
@@ -91,5 +112,5 @@ main().catch((err) => {
process.exit(0); process.exit(0);
}).then(() => process.exit(0)); }).then(() => process.exit(0));
// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260406T232253+08:00.MEUCIQDmyDRTnp6ofB1pCeV/ // @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260412T102509+08:00.MEUCIQDWJGWbn1BKanpi6xQs
// WP/RH6N+6MmJRjdp72t2sdDnoAIgEeExNIF/K3IwhrNZNWIRTv+AVjKzKKS3dKUTYfGbwTI= // rnI4eZ9+4F9jfQOnZEM9DUwYXAIgVSihT4fD/n92DyMyzsQsggPZ0qGcJnID+35ApBKkRZc=