Skip to content

Commit 126c7fb

Browse files
authored
Merge pull request #21 from aredotna/fixes-piped-add
Fixes piped add
2 parents a5b3bf9 + 7b1fb92 commit 126c7fb

3 files changed

Lines changed: 20 additions & 6 deletions

File tree

src/cli.contract.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import { dirname, resolve } from "node:path";
77
const __dirname = dirname(fileURLToPath(import.meta.url));
88
const projectRoot = resolve(__dirname, "..");
99

10-
function runCli(args: string[]) {
10+
function runCli(args: string[], options?: { input?: string }) {
1111
return spawnSync(
1212
process.execPath,
1313
["--import", "tsx", "src/cli.tsx", ...args],
1414
{
1515
cwd: projectRoot,
1616
encoding: "utf8",
1717
env: process.env,
18+
input: options?.input,
1819
},
1920
);
2021
}
@@ -70,3 +71,12 @@ test("--quiet keeps schema while compacting JSON", () => {
7071
assert.ok(!quiet.stdout.includes("\n "), "expected compact JSON output");
7172
assert.ok(pretty.stdout.includes("\n "), "expected pretty JSON output");
7273
});
74+
75+
test("add command reads value from piped stdin", () => {
76+
const result = runCli(["add", "some-channel"], { input: "hello from stdin" });
77+
const output = result.stdout + result.stderr;
78+
assert.ok(
79+
!output.includes("Missing required argument: value"),
80+
`expected stdin to supply the value, got: ${output}`,
81+
);
82+
});

src/commands/add.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { Box, Text } from "ink";
22
import { client, getData } from "../api/client";
3+
import { readStdin } from "../lib/args";
34
import { Spinner } from "../components/Spinner";
45
import { useCommand } from "../hooks/use-command";
56

67
interface Props {
78
channel: string;
8-
value: string;
9+
value?: string;
910
title?: string;
1011
description?: string;
1112
altText?: string;
@@ -16,7 +17,7 @@ interface Props {
1617

1718
export function AddCommand({
1819
channel,
19-
value,
20+
value: valueProp,
2021
title,
2122
description,
2223
altText,
@@ -25,6 +26,9 @@ export function AddCommand({
2526
insertAt,
2627
}: Props) {
2728
const { data, error, loading } = useCommand(async () => {
29+
const resolvedValue = valueProp ?? (await readStdin());
30+
if (!resolvedValue) throw new Error("Missing required argument: value");
31+
2832
const ch = await getData(
2933
client.GET("/v3/channels/{id}", {
3034
params: { path: { id: channel } },
@@ -33,7 +37,7 @@ export function AddCommand({
3337
const block = await getData(
3438
client.POST("/v3/blocks", {
3539
body: {
36-
value,
40+
value: resolvedValue,
3741
channel_ids: [ch.id],
3842
title,
3943
description,

src/lib/registry.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,11 +578,11 @@ export const commands: CommandDefinition[] = [
578578
},
579579
],
580580
render(args, flags) {
581-
const value = requireArg([args.slice(1).join(" ")], 0, "value");
581+
const argValue = args.slice(1).join(" ").trim() || undefined;
582582
return (
583583
<AddCommand
584584
channel={requireArg(args, 0, "channel")}
585-
value={value}
585+
value={argValue}
586586
title={flag(flags, "title")}
587587
description={flag(flags, "description")}
588588
altText={flag(flags, "alt-text")}

0 commit comments

Comments
 (0)