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
11 changes: 10 additions & 1 deletion packages/cli/src/services/__tests__/output.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,17 @@ describe('Output service', () => {

it('given <stdout>, print output to console', async () => {
const output = '{}';
const writeMock = jest.spyOn(process.stdout, 'write').mockImplementation((chunk: any, callback?: any) => {
if (typeof callback === 'function') {
callback();
}
return true;
});

expect(await writeOutput(output, '<stdout>')).toBeUndefined();
expect(process.stdout.write).toBeCalledWith(output);
expect(writeMock).toHaveBeenCalledWith(output, expect.any(Function));

writeMock.mockRestore();
});
});
});
9 changes: 8 additions & 1 deletion packages/cli/src/services/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ export async function writeOutput(outputStr: string, outputFile: string): Promis
if (outputFile !== '<stdout>') {
await fs.writeFile(outputFile, outputStr);
} else {
process.stdout.write(outputStr);
// Handle backpressure by using the callback parameter
// The callback is invoked when the data is flushed (or an error occurs)
return new Promise((resolve, reject) => {
process.stdout.write(outputStr, err => {
if (err) reject(err);
else resolve();
});
});
}
}