diff --git a/.changeset/social-webs-enter.md b/.changeset/social-webs-enter.md new file mode 100644 index 0000000..03f2b9b --- /dev/null +++ b/.changeset/social-webs-enter.md @@ -0,0 +1,5 @@ +--- +"@bunny.net/cli": patch +--- + +bunny api no longer truncates large JSON responses when piping diff --git a/packages/cli/src/commands/api.ts b/packages/cli/src/commands/api.ts index d391a0d..e7c96c3 100644 --- a/packages/cli/src/commands/api.ts +++ b/packages/cli/src/commands/api.ts @@ -167,14 +167,14 @@ export const apiCommand = defineCommand({ text || undefined, ); } - if (text) logger.log(text); + if (text) await writeStdout(`${text}\n`); return; } if (!res.ok) { if (output === "json") { - logger.log( - JSON.stringify({ error: parsed, status: res.status }, null, 2), + await writeStdout( + `${JSON.stringify({ error: parsed, status: res.status }, null, 2)}\n`, ); } else { const msg = @@ -189,6 +189,17 @@ export const apiCommand = defineCommand({ process.exit(1); } - logger.log(JSON.stringify(parsed, null, 2)); + await writeStdout(`${JSON.stringify(parsed, null, 2)}\n`); }, }); + +/** + * Await the write callback so large payloads piped to another process aren't + * truncated — console.log to a POSIX pipe is async (with no way to wait for + * it), and the runtime can exit before the buffer drains. + */ +function writeStdout(data: string): Promise { + return new Promise((resolve, reject) => { + process.stdout.write(data, (err) => (err ? reject(err) : resolve())); + }); +}