🔄 Refactor commit script to use parseArgs and add flags for help, check-only, and auto-commit functionality
This commit is contained in:
@@ -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<boolean> {
|
||||
}
|
||||
|
||||
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<string> => {
|
||||
@@ -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=
|
||||
|
||||
Reference in New Issue
Block a user