Files
ts-scripts/single-scripts/commit.ts
2026-02-07 01:31:25 +08:00

81 lines
2.7 KiB
TypeScript
Executable File

#!/usr/bin/env runts -- --allow-import --allow-run --allow-read
import {createInterface} from "node:readline/promises";
import {stdin, stdout} from "node:process";
import {execCommandShell, log, ProcessBar, term,} from "https://script.hatter.ink/@29/deno-commons-mod.ts";
import {getGitLocalRev, getGitRemoteRev, getGitStatus,} from "https://script.hatter.ink/@0/deno-git-mod.ts";
async function checkRev(): Promise<boolean> {
const localRev = await getGitLocalRev();
const remoteRev = await new ProcessBar("Checking rev").call(
async (): Promise<string> => {
return await getGitRemoteRev();
},
);
if (localRev === remoteRev) {
console.log(term.green(`Check rev successfully, rev: ${localRev}`));
} else {
throw `Check rev failed, local rev: ${localRev} vs remote rev: ${remoteRev}`;
}
}
async function main() {
await checkRev(); // check local rev <--> remote rev equals
const gitStatus = await getGitStatus();
if (gitStatus === null) {
log.warn("No git status found");
return;
}
log.info("Git status:", gitStatus);
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 > ");
} while (message.length == 0);
readline.close();
const gitCommitArgs: string[] = [];
gitCommitArgs.push("commit");
for (const file of gitStatus.modified) {
gitCommitArgs.push(file);
}
for (const file of gitStatus.deleted) {
gitCommitArgs.push(file);
}
for (const file of gitStatus.untracked) {
gitCommitArgs.push(file);
}
gitCommitArgs.push("-m");
gitCommitArgs.push(message);
if (gitStatus.untracked.length > 0) {
const gitAddArgs = ["add"];
for (const file of gitStatus.untracked) {
gitAddArgs.push(file);
}
log.info(">>>>>", ["git", gitAddArgs]);
await execCommandShell("git", gitAddArgs);
}
log.info(">>>>>", ["git", gitCommitArgs]);
await execCommandShell("git", gitCommitArgs);
log.info(">>>>>", ["git", "push"]);
await new ProcessBar("Git pushing").call(async (): Promise<void> => {
await execCommandShell("git", ["push"]);
});
}
main().catch((err) => {
log.error(err);
process.exit(0);
}).then(() => process.exit(0));
// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260207T013110+08:00.MEYCIQDXoa6SfDaJ5KsDplYF
// zBZXJkEuC9HndsMQrmLMFTAXdQIhALMYnHVZNFOTYo6+COVCOmjnMPayKHNHlgfEa0siKPvb