From bd3e854b31781edfd03f087faf6de21b2bf1b783 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 13 Apr 2018 20:51:09 -0700 Subject: [PATCH] Automatically configure tsc output and provide a new 'diagnosticStyle' option. --- src/compiler/commandLineParser.ts | 8 ++++++++ src/compiler/sys.ts | 4 ++++ src/compiler/tsc.ts | 11 +++++++++-- src/compiler/types.ts | 6 ++++-- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 470bd111492..252ea9e8df0 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -56,6 +56,14 @@ namespace ts { category: Diagnostics.Command_line_Options, description: Diagnostics.Stylize_errors_and_messages_using_color_and_context_experimental }, + { + name: "diagnosticStyle", + type: createMapFromTemplate({ + auto: DiagnosticStyle.Auto, + pretty: DiagnosticStyle.Pretty, + simple: DiagnosticStyle.Simple, + }), + }, { name: "preserveWatchOutput", type: "boolean", diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index c37315a2e56..2a4616732dd 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -428,6 +428,7 @@ namespace ts { newLine: string; useCaseSensitiveFileNames: boolean; write(s: string): void; + writeOutputIsTty?(): boolean; readFile(path: string, encoding?: string): string | undefined; getFileSize?(path: string): number; writeFile(path: string, data: string, writeByteOrderMark?: boolean): void; @@ -561,6 +562,9 @@ namespace ts { write(s: string): void { process.stdout.write(s); }, + writeOutputIsTty() { + return process.stdout.isTTY; + }, readFile, writeFile, watchFile: getWatchFile(), diff --git a/src/compiler/tsc.ts b/src/compiler/tsc.ts index f16ca98c93d..5012b73e98a 100644 --- a/src/compiler/tsc.ts +++ b/src/compiler/tsc.ts @@ -19,11 +19,18 @@ namespace ts { let reportDiagnostic = createDiagnosticReporter(sys); function updateReportDiagnostic(options: CompilerOptions) { - if (options.pretty) { + if (shouldBePretty(options)) { reportDiagnostic = createDiagnosticReporter(sys, /*pretty*/ true); } } + function shouldBePretty(options: CompilerOptions) { + if ((typeof options.pretty === "undefined" && typeof options.diagnosticStyle === "undefined") || options.diagnosticStyle === DiagnosticStyle.Auto) { + return !!sys.writeOutputIsTty && sys.writeOutputIsTty(); + } + return options.diagnosticStyle === DiagnosticStyle.Pretty || options.pretty; + } + function padLeft(s: string, length: number) { while (s.length < length) { s = " " + s; @@ -159,7 +166,7 @@ namespace ts { } function createWatchStatusReporter(options: CompilerOptions) { - return ts.createWatchStatusReporter(sys, !!options.pretty); + return ts.createWatchStatusReporter(sys, shouldBePretty(options)); } function createWatchOfConfigFile(configParseResult: ParsedCommandLine, optionsToExtend: CompilerOptions) { diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 987d403e7cd..802c175b573 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -4193,7 +4193,8 @@ namespace ts { preserveSymlinks?: boolean; /* @internal */ preserveWatchOutput?: boolean; project?: string; - /* @internal */ pretty?: DiagnosticStyle; + /* @internal */ pretty?: boolean; + /* @internal */ diagnosticStyle?: DiagnosticStyle; reactNamespace?: string; jsxFactory?: string; removeComments?: boolean; @@ -4293,8 +4294,9 @@ namespace ts { /* @internal */ export const enum DiagnosticStyle { - Simple, + Auto, Pretty, + Simple, } /** Either a parsed command line or a parsed tsconfig.json */