@@ -47,6 +47,21 @@ namespace ts {
47
47
constructor ( o : any ) ;
48
48
}
49
49
50
+ declare var ChakraHost : {
51
+ args : string [ ] ;
52
+ currentDirectory : string ;
53
+ executingFile : string ;
54
+ echo ( s : string ) : void ;
55
+ quit ( exitCode ?: number ) : void ;
56
+ fileExists ( path : string ) : boolean ;
57
+ directoryExists ( path : string ) : boolean ;
58
+ createDirectory ( path : string ) : void ;
59
+ resolvePath ( path : string ) : string ;
60
+ readFile ( path : string ) : string ;
61
+ writeFile ( path : string , contents : string ) : void ;
62
+ readDirectory ( path : string , extension ?: string , exclude ?: string [ ] ) : string [ ] ;
63
+ } ;
64
+
50
65
export var sys : System = ( function ( ) {
51
66
52
67
function getWScriptSystem ( ) : System {
@@ -194,6 +209,7 @@ namespace ts {
194
209
8000
}
195
210
} ;
196
211
}
212
+
197
213
function getNodeSystem ( ) : System {
198
214
const _fs = require ( "fs" ) ;
199
215
const _path = require ( "path" ) ;
@@ -281,7 +297,7 @@ namespace ts {
281
297
// REVIEW: for now this implementation uses polling.
282
298
// The advantage of polling is that it works reliably
283
299
// on all os and with network mounted files.
284
- // For 90 referenced files, the average time to detect
300
+ // For 90 referenced files, the average time to detect
285
301
// changes is 2*msInterval (by default 5 seconds).
286
302
// The overhead of this is .04 percent (1/2500) with
287
303
// average pause of < 1 millisecond (and max
@@ -406,7 +422,7 @@ namespace ts {
406
422
} ;
407
423
} ,
408
424
watchDirectory : ( path , callback , recursive ) => {
409
- // Node 4.0 `fs.watch` function supports the "recursive" option on both OSX and Windows
425
+ // Node 4.0 `fs.watch` function supports the "recursive" option on both OSX and Windows
410
426
// (ref: https://github.com/nodejs/node/pull/2649 and https://github.com/Microsoft/TypeScript/issues/4643)
411
427
return _fs . watch (
412
428
path ,
@@ -454,6 +470,37 @@ namespace ts {
454
470
}
455
471
} ;
456
472
}
473
+
474
+ function getChakraSystem ( ) : System {
475
+
476
+ return {
477
+ newLine : "\r\n" ,
478
+ args : ChakraHost . args ,
479
+ useCaseSensitiveFileNames : false ,
480
+ write : ChakraHost . echo ,
481
+ readFile ( path : string , encoding ?: string ) {
482
+ // encoding is automatically handled by the implementation in ChakraHost
483
+ return ChakraHost . readFile ( path ) ;
484
+ } ,
485
+ writeFile ( path : string , data : string , writeByteOrderMark ?: boolean ) {
486
+ // If a BOM is required, emit one
487
+ if ( writeByteOrderMark ) {
488
+ data = "\uFEFF" + data ;
489
+ }
490
+
491
+ ChakraHost . writeFile ( path , data ) ;
492
+ } ,
493
+ resolvePath : ChakraHost . resolvePath ,
494
+ fileExists : ChakraHost . fileExists ,
495
+ directoryExists : ChakraHost . directoryExists ,
496
+ createDirectory : ChakraHost . createDirectory ,
497
+ getExecutingFilePath : ( ) => ChakraHost . executingFile ,
498
+ getCurrentDirectory : ( ) => ChakraHost . currentDirectory ,
499
+ readDirectory : ChakraHost . readDirectory ,
500
+ exit : ChakraHost . quit ,
501
+ } ;
502
+ }
503
+
457
504
if ( typeof WScript !== "undefined" && typeof ActiveXObject === "function" ) {
458
505
return getWScriptSystem ( ) ;
459
506
}
@@ -462,8 +509,13 @@ namespace ts {
462
509
// process.browser check excludes webpack and browserify
463
510
return getNodeSystem ( ) ;
464
511
}
512
+ else if ( typeof ChakraHost !== "undefined" ) {
513
+ return getChakraSystem ( ) ;
514
+ }
465
515
else {
466
516
return undefined ; // Unsupported host
467
517
}
468
518
} ) ( ) ;
469
519
}
520
+
521
+
0 commit comments