Add triggershell run/scripts - execute configured scripts from the CLI

`run <scriptId>` auto-detects whether the web server is already
reachable (a quick /api/healthz check):

- If it is, the run goes through the existing POST
  /api/scripts/:id/runs endpoint (token-authenticated, same as any
  other API client) and the CLI subscribes over /ws/runs exactly like
  a browser tab - so the run shows up live in Run History and any open
  browser watching it, with zero server-side changes, since the
  broadcast path has no idea a run was triggered by a click vs a CLI
  invocation.
- If nothing's reachable, it calls startRun() directly in its own
  process (after its own migrateOnBoot/reconcileOrphanedRuns, so a
  from-scratch .triggershell/ works standalone) and streams output by
  listening on the same in-process runEvents emitter a WS client would
  otherwise be fed from - read-log-then-listen, the same ordering
  ws/server.ts's subscribe() already uses, so a fast script finishing
  before the listener attaches still gets its output printed.

Both modes support --var name=value (repeatable; repeat a name for
multiselect), --no-wait, and Ctrl-C cancellation through the same
mechanism the web UI's Cancel button uses (a WS cancel message
remotely, cancelRun() directly locally). `scripts list`/`scripts show`
are local-only, no network - same direct-config-read pattern as
`validate`/`doctor`.

Extracts defaultValuesForScript() out of dynamic-form.tsx into
src/lib/config/defaults.ts so the CLI's --var handling and the web
form fill in a script's configured defaults identically instead of
duplicating that logic.

Verified live end-to-end: a CLI-triggered remote run was observed
streaming to both the triggering CLI process and an independent WS
client (simulating a browser tab) simultaneously; local-mode Ctrl-C
confirmed to actually kill the spawned child process, not just the
CLI; token, wrong-token, and TRIGGERSHELL_API_TOKEN auth paths all
verified against a running auth-enabled server.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-16 12:29:55 +02:00
co-authored by Claude Sonnet 5
parent 28407402c8
commit e13b7e3b1a
11 changed files with 654 additions and 14 deletions
+82
View File
@@ -0,0 +1,82 @@
import path from "node:path";
import { ConfigError, loadConfig } from "../../lib/config/load";
import type { ScriptConfig } from "../../lib/config/schema";
import { loadDotenv } from "../lib/env-file";
import { resolveConfigPath } from "../lib/paths";
export interface ScriptsOptions {
config?: string;
}
function loadScripts(opts: ScriptsOptions): ScriptConfig[] | null {
const configPath = resolveConfigPath(opts.config);
loadDotenv(path.join(path.dirname(configPath), ".env"));
try {
return loadConfig(configPath).config.scripts;
} catch (error) {
if (error instanceof ConfigError) {
console.error(`Config error: ${error.message}`);
for (const issue of error.issues) console.error(` - ${issue}`);
process.exitCode = 1;
return null;
}
throw error;
}
}
export async function scriptsListCommand(opts: ScriptsOptions): Promise<void> {
const scripts = loadScripts(opts);
if (!scripts) return;
if (scripts.length === 0) {
console.log("No scripts configured.");
return;
}
const idWidth = Math.max(...scripts.map((s) => s.id.length));
for (const script of scripts) {
const description = script.description ? ` - ${script.description}` : "";
console.log(`${script.id.padEnd(idWidth)} ${script.name}${description}`);
}
}
export async function scriptsShowCommand(
scriptId: string,
opts: ScriptsOptions,
): Promise<void> {
const scripts = loadScripts(opts);
if (!scripts) return;
const script = scripts.find((s) => s.id === scriptId);
if (!script) {
console.error(`No script '${scriptId}' configured.`);
if (scripts.length > 0) {
console.error(`Available scripts: ${scripts.map((s) => s.id).join(", ")}`);
}
process.exitCode = 1;
return;
}
console.log(script.name);
if (script.description) console.log(script.description);
console.log(`\ncommand: ${script.command} ${script.args.join(" ")}`.trimEnd());
if (script.variables.length === 0) {
console.log("\nThis script takes no parameters.");
return;
}
console.log("\nvariables:");
for (const variable of script.variables) {
const parts: string[] = [variable.type];
if (variable.required) parts.push("required");
if (variable.type === "enum" || variable.type === "multiselect") {
parts.push(`choices: ${variable.choices.join(", ")}`);
}
if (variable.default !== undefined) {
parts.push(`default: ${JSON.stringify(variable.default)}`);
}
console.log(` ${variable.name} (${parts.join(", ")})`);
}
}