feat(cli): use external editor for commit message editing
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
#!/usr/bin/env bun
|
#!/usr/bin/env bun
|
||||||
|
|
||||||
import * as readline from "node:readline";
|
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 { loadConfig, saveConfig } from "./src/config";
|
||||||
import {
|
import {
|
||||||
isGitRepo,
|
isGitRepo,
|
||||||
@@ -105,10 +108,38 @@ async function confirmCommit(message: string): Promise<"y" | "n" | "e"> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function editMessage(current: string): Promise<string | null> {
|
async function editMessage(current: string): Promise<string | null> {
|
||||||
console.log(` Current: ${DIM}${current}${RESET}`);
|
const tmpPath = join(tmpdir(), `gai-msg-${Date.now()}`);
|
||||||
console.log(` Enter new message (empty to abort):`);
|
|
||||||
const newMsg = await ask(" > ");
|
const header = `# Edit commit message below. Save and close to confirm.\n# Delete all lines to abort.\n`;
|
||||||
return newMsg || null;
|
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() {
|
async function main() {
|
||||||
|
|||||||
Reference in New Issue
Block a user