Write results and diagnostics
createOutput() provides write(...) for command output and error(...) for diagnostics. Individual channels are useful in tests and streaming adapters.
Finish interactive output
The application owns terminal detection, progress line width and redraw, signal handling, and cleanup. Create one TerminalProgressReporter per run and close it before writing the final result or error. If progress and regular output run at the same time, serialize their writes to the shared destination.
In the example, the application owns runApplication(...) and the progress indicator. createOutput() writes results and diagnostics. The first failure is retained until progress is closed, then passed to the error policy.
import { createOutput, createTerminalApp } from 'icore';
const output = createOutput();
const app = createTerminalApp({ commands, output });
const progress = new TerminalProgressReporter({
stdout: process.stdout
});
let failure: { error: unknown } | undefined;
try {
await runApplication({ progress });
}
catch (error) {
failure = { error };
}
try {
await progress.close();
}
catch (error) {
failure ??= { error };
}
if (failure !== undefined) {
process.exitCode = await app.reportError(failure.error, {
phase: 'external'
});
}
Write a result and an error
Send command results to standard output and diagnostics to standard error. This lets you pipe the result to another program.
import { createOutput } from 'icore';
const output = createOutput();
await output.write('regular command output\n');
await output.error('warning: using cached data\n');
Write to a specific channel
Pass output.stdout.write(...) or output.stderr.write(...) when a component needs to know which channel it is writing to.
await output.stdout.write('regular command output\n');
await output.stderr.write('warning: using cached data\n');
Capture output in tests
Pass your own sinks to createOutput(...). Tests can capture both channels without replacing the global process.stdout.
let stdout = '';
let stderr = '';
const output = createOutput({
stdout: {
write(chunk) {
stdout += chunk;
}
},
stderr: {
write(chunk) {
stderr += chunk;
}
}
});
await output.write('ok\n');
await output.error('warning\n');
Create individual writers
Use createStdoutWriter(...) and createStderrWriter(...) to assemble your own output object. Use createOutput() for a ready pair of channels.
import { createStderrWriter, createStdoutWriter } from 'icore';
const stdout = createStdoutWriter();
const stderr = createStderrWriter();
await stdout.write('ok\n');
await stderr.write('warning\n');
Wait for a stream
createBackpressureTextWriter(...) waits when the destination cannot accept the next chunk yet.
import { createBackpressureTextWriter } from 'icore';
const writer = createBackpressureTextWriter(process.stdout);
await writer.write('large chunk\n');
When you pass a Node.js stream directly, a paused write rejects if error or close arrives before drain. A stream that has already ended or been destroyed rejects before writing.
Pass an asynchronous destination
For destinations outside Node.js streams, supply a write(...) function that returns a promise. The application defines the reliability and ordering of delivery.
const writer = createBackpressureTextWriter({
async write(chunk) {
await sendToRemoteConsole(chunk);
}
});
await writer.write('forwarded output\n');