Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/cli.contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ 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],
{
cwd: projectRoot,
encoding: "utf8",
env: process.env,
input: options?.input,
},
);
}
Expand Down Expand Up @@ -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}`,
);
});
10 changes: 7 additions & 3 deletions src/commands/add.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,7 +17,7 @@ interface Props {

export function AddCommand({
channel,
value,
value: valueProp,
title,
description,
altText,
Expand All @@ -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 } },
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/registry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<AddCommand
channel={requireArg(args, 0, "channel")}
value={value}
value={argValue}
title={flag(flags, "title")}
description={flag(flags, "description")}
altText={flag(flags, "alt-text")}
Expand Down
Loading