diff --git a/src/cli.contract.test.ts b/src/cli.contract.test.ts index 60b8b5f..d95d773 100644 --- a/src/cli.contract.test.ts +++ b/src/cli.contract.test.ts @@ -7,7 +7,7 @@ import { dirname, resolve } from "node:path"; const __dirname = dirname(fileURLToPath(import.meta.url)); const projectRoot = resolve(__dirname, ".."); -function runCli(args: string[]) { +function runCli(args: string[], options?: { input?: string }) { return spawnSync( process.execPath, ["--import", "tsx", "src/cli.tsx", ...args], @@ -15,6 +15,7 @@ function runCli(args: string[]) { cwd: projectRoot, encoding: "utf8", env: process.env, + input: options?.input, }, ); } @@ -70,3 +71,12 @@ test("--quiet keeps schema while compacting JSON", () => { assert.ok(!quiet.stdout.includes("\n "), "expected compact JSON output"); assert.ok(pretty.stdout.includes("\n "), "expected pretty JSON output"); }); + +test("add command reads value from piped stdin", () => { + const result = runCli(["add", "some-channel"], { input: "hello from stdin" }); + const output = result.stdout + result.stderr; + assert.ok( + !output.includes("Missing required argument: value"), + `expected stdin to supply the value, got: ${output}`, + ); +}); diff --git a/src/commands/add.tsx b/src/commands/add.tsx index 471dc3f..20c2765 100644 --- a/src/commands/add.tsx +++ b/src/commands/add.tsx @@ -1,11 +1,12 @@ import { Box, Text } from "ink"; import { client, getData } from "../api/client"; +import { readStdin } from "../lib/args"; import { Spinner } from "../components/Spinner"; import { useCommand } from "../hooks/use-command"; interface Props { channel: string; - value: string; + value?: string; title?: string; description?: string; altText?: string; @@ -16,7 +17,7 @@ interface Props { export function AddCommand({ channel, - value, + value: valueProp, title, description, altText, @@ -25,6 +26,9 @@ export function AddCommand({ insertAt, }: Props) { const { data, error, loading } = useCommand(async () => { + const resolvedValue = valueProp ?? (await readStdin()); + if (!resolvedValue) throw new Error("Missing required argument: value"); + const ch = await getData( client.GET("/v3/channels/{id}", { params: { path: { id: channel } }, @@ -33,7 +37,7 @@ export function AddCommand({ const block = await getData( client.POST("/v3/blocks", { body: { - value, + value: resolvedValue, channel_ids: [ch.id], title, description, diff --git a/src/lib/registry.tsx b/src/lib/registry.tsx index 2dfac1d..d5f718d 100644 --- a/src/lib/registry.tsx +++ b/src/lib/registry.tsx @@ -578,11 +578,11 @@ export const commands: CommandDefinition[] = [ }, ], render(args, flags) { - const value = requireArg([args.slice(1).join(" ")], 0, "value"); + const argValue = args.slice(1).join(" ").trim() || undefined; return (