feat(cli): use external editor for commit message editing
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
#!/usr/bin/env bun
|
||||
|
||||
import * as readline from "node:readline";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { unlink } from "node:fs/promises";
|
||||
import { loadConfig, saveConfig } from "./src/config";
|
||||
import {
|
||||
isGitRepo,
|
||||
@@ -105,10 +108,38 @@ async function confirmCommit(message: string): Promise<"y" | "n" | "e"> {
|
||||
}
|
||||
|
||||
async function editMessage(current: string): Promise<string | null> {
|
||||
console.log(` Current: ${DIM}${current}${RESET}`);
|
||||
console.log(` Enter new message (empty to abort):`);
|
||||
const newMsg = await ask(" > ");
|
||||
return newMsg || null;
|
||||
const tmpPath = join(tmpdir(), `gai-msg-${Date.now()}`);
|
||||
|
||||
const header = `# Edit commit message below. Save and close to confirm.\n# Delete all lines to abort.\n`;
|
||||
await Bun.write(tmpPath, header + current);
|
||||
|
||||
const editor =
|
||||
process.env.VISUAL ||
|
||||
process.env.EDITOR ||
|
||||
(process.platform === "darwin" ? "vi" : "nano");
|
||||
|
||||
const proc = Bun.spawn([editor, tmpPath], {
|
||||
stdout: "inherit",
|
||||
stderr: "inherit",
|
||||
stdin: "inherit",
|
||||
});
|
||||
|
||||
const exitCode = await proc.exited;
|
||||
if (exitCode !== 0) {
|
||||
await unlink(tmpPath).catch(() => {});
|
||||
return null;
|
||||
}
|
||||
|
||||
const content = await Bun.file(tmpPath).text();
|
||||
await unlink(tmpPath).catch(() => {});
|
||||
|
||||
const lines = content
|
||||
.split("\n")
|
||||
.filter((line) => !line.startsWith("#"))
|
||||
.join("\n")
|
||||
.trim();
|
||||
|
||||
return lines || null;
|
||||
}
|
||||
|
||||
async function main() {
|
||||
|
||||
Reference in New Issue
Block a user