From a46e636654b62204c995df26d8a0a591bb4edfd4 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 12 Apr 2026 16:01:25 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Update=20script=20metadata=20and?= =?UTF-8?q?=20enhance=20howto.ts=20with=20command=20extraction=20and=20exe?= =?UTF-8?q?cution=20logic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script-meta-v2.json | 6 +++--- single-scripts/howto.ts | 38 +++++++++++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/script-meta-v2.json b/script-meta-v2.json index ca965f8..bf3851f 100644 --- a/script-meta-v2.json +++ b/script-meta-v2.json @@ -153,12 +153,12 @@ }, "howto.ts": { "script_name": "howto.ts", - "script_length": 1225, - "script_sha256": "97b7f0efd97b774c70000a5cb56f9115df0e9473cc2018b0404f7556bdba0e83", + "script_length": 2121, + "script_sha256": "0c78b2ca3729ffb4fbe92c46f8bc15e62dd9224670db5cc0fe9efccf29759958", "script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/single-scripts/howto.ts", "single_script_file": true, "publish_time": 1775973075081, - "update_time": 1775973377013 + "update_time": 1775980874679 }, "init-cheatsheet.ts": { "script_name": "init-cheatsheet.ts", diff --git a/single-scripts/howto.ts b/single-scripts/howto.ts index 353a8b4..bd0ea0c 100755 --- a/single-scripts/howto.ts +++ b/single-scripts/howto.ts @@ -1,7 +1,7 @@ #!/usr/bin/env runts -- --allow-all import {parseArgs} from "jsr:@std/cli/parse-args"; -import {exit, log, ProcessBar,} from "https://script.hatter.ink/@67/deno-commons-mod.ts"; +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() { @@ -33,6 +33,38 @@ howto.ts 'MESSAGE' - generate command line for MESSAGE }, ); log.success(`AI howto command line message: \n${summary}`); + + try { + const commandLine = extractCommand(summary); + console.log( + "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) => { @@ -40,5 +72,5 @@ main().catch((err) => { process.exit(0); }).then(() => process.exit(0)); -// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260412T135613+08:00.MEUCIQDbJGYgxtAv1d6Vrmyj -// vQDEJGFUuZuH1UQOYQhWRzN69wIgS8tw92nMpF5TLQy/qS4uk7w7d8iY8rmT6DR+V3jg0lM= +// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260412T160105+08:00.MEQCIGSMGJYgv+Am7EZUBj9N +// B1c1seTE1jc48Z7CZiKn0d23AiAdmw6UTSXRMH/BQKbhuVoFs6GiBo783QxnCXYT4TWf8A==