@@ -147,15 +147,17 @@ namespace ts {
147
147
148
148
export function executeCommandLine ( args : string [ ] ) : void {
149
149
let commandLine = parseCommandLine ( args ) ;
150
- let configFileName : string ; // Configuration file name (if any)
151
- let configFileWatcher : FileWatcher ; // Configuration file watcher
152
- let directoryWatcher : FileWatcher ; // Directory watcher to monitor source file addition/removal
153
- let cachedProgram : Program ; // Program cached from last compilation
154
- let rootFileNames : string [ ] ; // Root fileNames for compilation
155
- let compilerOptions : CompilerOptions ; // Compiler options for compilation
156
- let compilerHost : CompilerHost ; // Compiler host
157
- let hostGetSourceFile : typeof compilerHost . getSourceFile ; // getSourceFile method from default host
158
- let timerHandle : number ; // Handle for 0.25s wait timer
150
+ let configFileName : string ; // Configuration file name (if any)
151
+ let cachedConfigFileText : string ; // Cached configuration file text, used for reparsing (if any)
152
+ let configFileWatcher : FileWatcher ; // Configuration file watcher
153
+ let directoryWatcher : FileWatcher ; // Directory watcher to monitor source file addition/removal
154
+ let cachedProgram : Program ; // Program cached from last compilation
155
+ let rootFileNames : string [ ] ; // Root fileNames for compilation
156
+ let compilerOptions : CompilerOptions ; // Compiler options for compilation
157
+ let compilerHost : CompilerHost ; // Compiler host
158
+ let hostGetSourceFile : typeof compilerHost . getSourceFile ; // getSourceFile method from default host
159
+ let timerHandleForRecompilation : number ; // Handle for 0.25s wait timer to trigger recompilation
160
+ let timerHandleForDirectoryChanges : number ; // Handle for 0.25s wait timer to trigger directory change handler
159
161
160
162
if ( commandLine . options . locale ) {
161
163
if ( ! isJSONSupported ( ) ) {
@@ -232,16 +234,22 @@ namespace ts {
232
234
233
235
performCompilation ( ) ;
234
236
235
- function configFileToParsedCommandLine ( configFilename : string ) : ParsedCommandLine {
236
- let result = readConfigFile ( configFileName , sys . readFile ) ;
237
- if ( result . error ) {
238
- reportWatchDiagnostic ( result . error ) ;
239
- sys . exit ( ExitStatus . DiagnosticsPresent_OutputsSkipped ) ;
240
- return ;
237
+ function parseConfigFile ( ) : ParsedCommandLine {
238
+ if ( ! cachedConfigFileText ) {
239
+ try {
240
+ cachedConfigFileText = sys . readFile ( configFileName ) ;
241
+ }
242
+ catch ( e ) {
243
+ let error = createCompilerDiagnostic ( Diagnostics . Cannot_read_file_0_Colon_1 , configFileName , e . message ) ;
244
+ reportWatchDiagnostic ( error ) ;
245
+ sys . exit ( ExitStatus . DiagnosticsPresent_OutputsSkipped ) ;
246
+ return ;
247
+ }
241
248
}
242
249
250
+ let result = parseConfigFileTextToJson ( configFileName , cachedConfigFileText ) ;
243
251
let configObject = result . config ;
244
- let configParseResult = parseConfigFile ( configObject , sys , getDirectoryPath ( configFileName ) ) ;
252
+ let configParseResult = parseJsonConfigFileContent ( configObject , sys , getDirectoryPath ( configFileName ) ) ;
245
253
if ( configParseResult . errors . length > 0 ) {
246
254
reportDiagnostics ( configParseResult . errors ) ;
247
255
sys . exit ( ExitStatus . DiagnosticsPresent_OutputsSkipped ) ;
@@ -255,7 +263,7 @@ namespace ts {
255
263
256
264
if ( ! cachedProgram ) {
257
265
if ( configFileName ) {
258
- let configParseResult = configFileToParsedCommandLine ( configFileName ) ;
266
+ let configParseResult = parseConfigFile ( ) ;
259
267
rootFileNames = configParseResult . fileNames ;
260
268
compilerOptions = extend ( commandLine . options , configParseResult . options ) ;
261
269
}
@@ -322,42 +330,54 @@ namespace ts {
322
330
rootFileNames . splice ( index , 1 ) ;
323
331
}
324
332
}
325
- startTimer ( ) ;
333
+ startTimerForRecompilation ( ) ;
326
334
}
327
335
328
336
// If the configuration file changes, forget cached program and start the recompilation timer
329
337
function configFileChanged ( ) {
330
338
setCachedProgram ( undefined ) ;
331
- startTimer ( ) ;
339
+ cachedConfigFileText = undefined ;
340
+ startTimerForRecompilation ( ) ;
332
341
}
333
342
334
343
function watchedDirectoryChanged ( fileName : string ) {
335
344
if ( fileName && ! ts . isSupportedSourceFileName ( fileName ) ) {
336
345
return ;
337
346
}
338
347
339
- let parsedCommandLine = configFileToParsedCommandLine ( configFileName ) ;
340
- let newFileNames = parsedCommandLine . fileNames . map ( compilerHost . getCanonicalFileName ) ;
341
- let canonicalRootFileNames = rootFileNames . map ( compilerHost . getCanonicalFileName ) ;
348
+ startTimerForHandlingDirectoryChanges ( ) ;
349
+ }
350
+
351
+ function startTimerForHandlingDirectoryChanges ( ) {
352
+ if ( timerHandleForDirectoryChanges ) {
353
+ clearTimeout ( timerHandleForDirectoryChanges ) ;
354
+ }
355
+ timerHandleForDirectoryChanges = setTimeout ( directoryChangeHandler , 250 ) ;
356
+ }
357
+
358
+ function directoryChangeHandler ( ) {
359
+ let parsedCommandLine = parseConfigFile ( ) ;
360
+ let newFileNames = ts . map ( parsedCommandLine . fileNames , compilerHost . getCanonicalFileName ) ;
361
+ let canonicalRootFileNames = ts . map ( rootFileNames , compilerHost . getCanonicalFileName ) ;
342
362
343
- if ( ! doTwoArraysHaveTheSameElements ( newFileNames , canonicalRootFileNames ) ) {
363
+ if ( ! arrayStructurallyIsEqualTo ( newFileNames , canonicalRootFileNames ) ) {
344
364
setCachedProgram ( undefined ) ;
345
- startTimer ( ) ;
365
+ startTimerForRecompilation ( ) ;
346
366
}
347
367
}
348
368
349
369
// Upon detecting a file change, wait for 250ms and then perform a recompilation. This gives batch
350
370
// operations (such as saving all modified files in an editor) a chance to complete before we kick
351
371
// off a new compilation.
352
- function startTimer ( ) {
353
- if ( timerHandle ) {
354
- clearTimeout ( timerHandle ) ;
372
+ function startTimerForRecompilation ( ) {
373
+ if ( timerHandleForRecompilation ) {
374
+ clearTimeout ( timerHandleForRecompilation ) ;
355
375
}
356
- timerHandle = setTimeout ( recompile , 250 ) ;
376
+ timerHandleForRecompilation = setTimeout ( recompile , 250 ) ;
357
377
}
358
378
359
379
function recompile ( ) {
360
- timerHandle = undefined ;
380
+ timerHandleForRecompilation = undefined ;
361
381
reportWatchDiagnostic ( createCompilerDiagnostic ( Diagnostics . File_change_detected_Starting_incremental_compilation ) ) ;
362
382
performCompilation ( ) ;
363
383
}
0 commit comments