Files
ts-scripts/single-scripts/howto.ts

84 lines
2.3 KiB
TypeScript
Executable File

#!/usr/bin/env runts -- --allow-all
import {parseArgs} from "jsr:@std/cli/parse-args";
import {execCommandShell, exit, log, ProcessBar, term,} from "https://script.hatter.ink/@67/deno-commons-mod.ts";
import {howto} from "https://script.hatter.ink/@1/deno-ai-mod.ts";
async function main() {
const flags = parseArgs(Deno.args, {
boolean: ["help", "no-exec"],
alias: {
h: "help",
N: "no-exec",
},
});
if (flags.help) {
console.log(`howto.ts
howto.ts --help - show help
howto.ts 'MESSAGE' - generate command line for MESSAGE
`);
exit(0);
}
const noExec = flags["no-exec"];
const message = (flags._)
? flags._.join(" ")
: prompt("Input your message: ");
if (!message) {
throw new Error("Message is required");
}
const summary = await new ProcessBar("AI howto").call(
async (): Promise<string> => {
return await howto(message);
},
);
log.success(`AI howto command line message: \n${summary}`);
if (!noExec) {
try {
const commandLine = extractCommand(summary);
console.log(
"Found command line: ",
term.auto(`[green][[[${commandLine}]]][/]`),
);
if (confirm("Execute this command?")) {
const status = await execCommandShell("sh", [
"-c",
commandLine,
]);
exit(status);
}
} catch (e) {
log.error("Extract command line failed", e);
}
}
}
function extractCommand(summary: string): string {
const lines = summary.split(/\r?\n/);
const sb = [];
let inCode = false;
for (const line of lines) {
if (inCode) {
if (line.startsWith("```")) {
return sb.join("\n");
}
sb.push(line);
} else if (line.startsWith("```")) {
inCode = true;
}
}
throw new Error("Command line not found");
}
main().catch((err) => {
log.error(err);
process.exit(0);
}).then(() => process.exit(0));
// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260412T160614+08:00.MEUCIEasEUKkR/P0ESG/IpIB
// 9turT2yM/EoOpSEwQdlz277qAiEAxCYPwvP563coKn1Ov3wI1icQR3Tesg2Hta0tdIn8f+8=