Skip to content

Commit 21c3d4b

Browse files
committed
Fix piping large amounts of data to other processes
1 parent 6ea73cd commit 21c3d4b

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

packages/cli/src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import * as render from "./render/index.js";
1616
import { major, minor } from "semver";
1717
import { getExitCode } from "./getExitCode.js";
1818
import { applyProfile, profiles } from "./profiles.js";
19+
import { write } from "./write.js";
1920

2021
const packageJson = createRequire(import.meta.url)("../package.json");
2122
const version = packageJson.version;
@@ -223,7 +224,7 @@ particularly ESM-related module resolution issues.`,
223224
result.problems = groupProblemsByKind(analysis.problems);
224225
}
225226

226-
console.log(JSON.stringify(result, undefined, 2));
227+
await write(JSON.stringify(result, undefined, 2) + "\n");
227228

228229
if (deleteTgz) {
229230
await unlink(deleteTgz);

packages/cli/src/write.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Readable, Writable } from "node:stream";
2+
import { pipeline } from "node:stream/promises";
3+
4+
// JSON output is often longer than 64 kb, so we need to use streams to write it to stdout
5+
// in order to avoid truncation when piping to other commands.
6+
export function write(data: string, out: Writable = process.stdout): Promise<void> {
7+
const stream = new Readable();
8+
stream.push(data);
9+
stream.push(null);
10+
return pipeline(stream, out);
11+
}

0 commit comments

Comments
 (0)