@@ -7,6 +7,7 @@ import { BaseError } from 'make-error'
77import sourceMapSupport = require( 'source-map-support' )
88import extend = require( 'xtend' )
99import arrify = require( 'arrify' )
10+ import chalk = require( 'chalk' )
1011
1112/**
1213 * Export the current version.
@@ -25,6 +26,7 @@ export interface Options {
2526 compiler ?: string
2627 configFile ?: string
2728 ignoreWarnings ?: string [ ]
29+ isRepl ?: boolean
2830}
2931
3032/**
@@ -74,8 +76,10 @@ export function register (opts?: Options) {
7476 const ts : typeof TS = require ( options . compiler )
7577 const config = readConfig ( options . configFile , ts )
7678
79+ // Render the configuration errors and exit the script.
7780 if ( config . errors . length ) {
78- throw new TypeScriptError ( config . errors , ts )
81+ console . error ( formatDiagnostics ( config . errors , ts ) )
82+ process . exit ( 1 )
7983 }
8084
8185 const serviceHost : TS . LanguageServiceHost = {
@@ -133,7 +137,14 @@ export function register (opts?: Options) {
133137 const diagnostics = getDiagnostics ( service , fileName , options )
134138
135139 if ( diagnostics . length ) {
136- throw new TypeScriptError ( diagnostics , ts )
140+ const message = formatDiagnostics ( diagnostics , ts )
141+
142+ if ( opts . isRepl ) {
143+ throw new TypeScriptError ( message )
144+ }
145+
146+ console . error ( message )
147+ process . exit ( 1 )
137148 }
138149
139150 return contents
@@ -181,14 +192,23 @@ export function formatDiagnostic (diagnostic: TS.Diagnostic, ts: typeof TS, cwd:
181192 const message = ts . flattenDiagnosticMessageText ( diagnostic . messageText , '\n' )
182193
183194 if ( diagnostic . file ) {
195+ const path = relative ( cwd , diagnostic . file . fileName )
184196 const { line, character } = diagnostic . file . getLineAndCharacterOfPosition ( diagnostic . start )
185197
186- return `${ relative ( cwd , diagnostic . file . fileName ) } (${ line + 1 } ,${ character + 1 } ): ${ message } (${ diagnostic . code } )`
198+ return `${ chalk . gray ( ` ${ path } (${ line + 1 } ,${ character + 1 } ):` ) } ${ message } (${ diagnostic . code } )`
187199 }
188200
189201 return `${ message } (${ diagnostic . code } )`
190202}
191203
204+ /**
205+ * Format diagnostics into friendlier errors.
206+ */
207+ function formatDiagnostics ( diagnostics : TS . Diagnostic [ ] , ts : typeof TS ) {
208+ return chalk . red ( '⨯ Unable to compile TypeScript' ) +
209+ EOL + EOL + diagnostics . map ( d => formatDiagnostic ( d , ts ) ) . join ( EOL )
210+ }
211+
192212/**
193213 * Sanitize the source map content.
194214 */
@@ -208,14 +228,4 @@ export class TypeScriptError extends BaseError {
208228
209229 name = 'TypeScriptError'
210230
211- message : string
212- diagnosticMessages : string [ ]
213-
214- constructor ( public diagnostics : TS . Diagnostic [ ] , ts : typeof TS ) {
215- super ( )
216-
217- this . diagnosticMessages = diagnostics . map ( d => formatDiagnostic ( d , ts ) )
218- this . message = [ 'Unable to compile TypeScript' ] . concat ( this . diagnosticMessages ) . join ( EOL )
219- }
220-
221231}
0 commit comments