From 7361e1336168c7a320d6b37cdbf4ef32d2bfb0b4 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 12 Apr 2026 10:25:22 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=84=20Refactor=20commit=20script=20to?= =?UTF-8?q?=20use=20`parseArgs`=20and=20add=20flags=20for=20help,=20check-?= =?UTF-8?q?only,=20and=20auto-commit=20functionality?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- single-scripts/commit.ts | 59 +++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 19 deletions(-) diff --git a/single-scripts/commit.ts b/single-scripts/commit.ts index 36cbcc3..b0774bc 100755 --- a/single-scripts/commit.ts +++ b/single-scripts/commit.ts @@ -1,7 +1,6 @@ #!/usr/bin/env runts -- --allow-all -import {createInterface} from "node:readline/promises"; -import {stdin, stdout} from "node:process"; +import {parseArgs} from "jsr:@std/cli/parse-args"; 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 {getGitLocalRev, getGitRemoteRev, getGitStatus,} from "https://script.hatter.ink/@2/deno-git-mod.ts"; @@ -21,6 +20,22 @@ async function checkRev(): Promise { } 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 const gitStatus = await getGitStatus(); if (gitStatus === null) { @@ -28,6 +43,10 @@ async function main() { return; } log.info("Git status:", gitStatus); + if (checkOnly) { + log.info("Check only, skip commit."); + return; + } const summary = await new ProcessBar("AI summarizing").call( async (): Promise => { @@ -40,21 +59,23 @@ async function main() { let emptyCount = 0; let message = "empty message"; - const readline = createInterface({ input: stdin, output: stdout }); - do { - if (emptyCount++ === 3) { - readline.close(); - console.log("Too many empty messages, then exit"); - return; - } - message = await readline.question( - "Input your commit message (Enter for AI summary) > ", - ); - if (message.trim().length == 0) { - message = summary ?? ""; - } - } while (message.length == 0); - readline.close(); + if (autoCommit && summary) { + message = summary; + } else { + do { + if (emptyCount++ === 3) { + readline.close(); + console.log("Too many empty messages, then exit"); + return; + } + message = prompt( + "Input your commit message (Enter for AI summary) > ", + ); + if (message.trim().length == 0) { + message = summary ?? ""; + } + } while (message.length == 0); + } const gitCommitArgs: string[] = []; gitCommitArgs.push("commit"); @@ -91,5 +112,5 @@ main().catch((err) => { process.exit(0); }).then(() => process.exit(0)); -// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260406T232253+08:00.MEUCIQDmyDRTnp6ofB1pCeV/ -// WP/RH6N+6MmJRjdp72t2sdDnoAIgEeExNIF/K3IwhrNZNWIRTv+AVjKzKKS3dKUTYfGbwTI= +// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260412T102509+08:00.MEUCIQDWJGWbn1BKanpi6xQs +// rnI4eZ9+4F9jfQOnZEM9DUwYXAIgVSihT4fD/n92DyMyzsQsggPZ0qGcJnID+35ApBKkRZc=