diff --git a/packages/jsii/bin/jsii.ts b/packages/jsii/bin/jsii.ts index fa7cfe0c5b..a3bc2f301e 100644 --- a/packages/jsii/bin/jsii.ts +++ b/packages/jsii/bin/jsii.ts @@ -2,6 +2,7 @@ import '@jsii/check-node/run'; import * as log4js from 'log4js'; import * as path from 'path'; +import * as util from 'util'; import * as yargs from 'yargs'; import { Compiler } from '../lib/compiler'; @@ -132,15 +133,26 @@ const warningTypes = Object.keys(enabledWarnings); }); function _configureLog4js(verbosity: number) { + const stderrColor = !!process.stderr.isTTY; + const stdoutColor = !!process.stdout.isTTY; + + log4js.addLayout('passThroughNoColor', () => { + return (loggingEvent) => stripAnsi(util.format(...loggingEvent.data)); + }); + log4js.configure({ appenders: { console: { type: 'stderr', - layout: { type: 'colored' }, + layout: { type: stderrColor ? 'colored' : 'basic' }, }, [utils.DIAGNOSTICS]: { type: 'stdout', - layout: { type: 'messagePassThrough' }, + layout: { + type: stdoutColor + ? 'messagePassThrough' + : ('passThroughNoColor' as any), + }, }, }, categories: { @@ -168,3 +180,11 @@ function _configureLog4js(verbosity: number) { } } } + +const ANSI_REGEX = + // eslint-disable-next-line no-control-regex + /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g; + +function stripAnsi(x: string): string { + return x.replace(ANSI_REGEX, ''); +}