Skip to content

Commit ec41e6c

Browse files
committed
fix(@angular/cli): chunk log so output is piped properly
Previously the output of some commands with long one-line log would not be drained properly when piping it, causing it to be truncated.
1 parent 84e4e8d commit ec41e6c

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

packages/angular/cli/lib/cli/index.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,32 @@ function initializeLogging(logger: logging.Logger) {
9393
break;
9494
}
9595

96-
output.write(color(entry.message) + '\n');
96+
// If we do console.log(message) or process.stdout.write(message + '\n'), the process might
97+
// stop before the whole message is written and the stream is flushed. This happens when
98+
// streams are asynchronous.
99+
//
100+
// NodeJS IO streams are different depending on platform and usage. In POSIX environment,
101+
// for example, they're asynchronous when writing to a pipe, but synchronous when writing
102+
// to a TTY. In windows, it's the other way around. You can verify which is which with
103+
// stream.isTTY and platform, but this is not good enough.
104+
// In the async case, one should wait for the callback before sending more data or
105+
// continuing the process. In our case it would be rather hard to do (but not impossible).
106+
//
107+
// Instead we take the easy way out and simply chunk the message and call the write
108+
// function while the buffer drain itself asynchronously. With a smaller chunk size than
109+
// the buffer, we are mostly certain that it works. In this case, the chunk has been picked
110+
// as half a page size (4096/2 = 2048), minus some bytes for the color formatting.
111+
// On POSIX it seems the buffer is 2 pages (8192), but just to be sure (could be different
112+
// by platform).
113+
//
114+
// For more details, see https://nodejs.org/api/process.html#process_a_note_on_process_i_o
115+
const chunkSize = 2000; // Small chunk.
116+
let message = entry.message;
117+
while (message) {
118+
const chunk = message.slice(0, chunkSize);
119+
message = message.slice(chunkSize);
120+
output.write(color(chunk));
121+
}
122+
output.write('\n');
97123
});
98124
}

0 commit comments

Comments
 (0)