From cca8619351498a850e9f8fb39cbb693f8e06a141 Mon Sep 17 00:00:00 2001 From: joeduffy Date: Mon, 16 Jan 2017 15:18:57 -0800 Subject: [PATCH] Fix output truncation issue --- tools/mujs/cmd/index.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/tools/mujs/cmd/index.ts b/tools/mujs/cmd/index.ts index 3cdd049cf..7fb2b6609 100644 --- a/tools/mujs/cmd/index.ts +++ b/tools/mujs/cmd/index.ts @@ -63,13 +63,26 @@ async function main(args: string[]): Promise { return 0; } +// exit is a workaround for nodejs/node#6456, a longstanding issue wherein there's no good way to synchronize with the +// flushing of console's asynchronous buffered output. This leads to truncated output, particularly when redirecting. +// As a workaround, we will write to process.stdout/stderr and only invoke process.exit after they have run. +function exit(code: number): void { + process.stdout.write("", () => { + process.stderr.write("", () => { + process.exit(code); + }); + }); +} + // Fire off the main process, and log any errors that go unhandled. main(process.argv.slice(2)).then( - (code: number) => process.exit(code), + (code: number) => { + exit(code); + }, (err: Error) => { console.error("Unhandled exception:"); console.error(err.stack); - process.exit(-1); + exit(-1); }, );