@@ -47,6 +47,21 @@ namespace ts {
4747 constructor ( o : any ) ;
4848 }
4949
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+
5065 export var sys : System = ( function ( ) {
5166
5267 function getWScriptSystem ( ) : System {
@@ -194,6 +209,7 @@ namespace ts {
194209 }
195210 } ;
196211 }
212+
197213 function getNodeSystem ( ) : System {
198214 const _fs = require ( "fs" ) ;
199215 const _path = require ( "path" ) ;
@@ -281,7 +297,7 @@ namespace ts {
281297 // REVIEW: for now this implementation uses polling.
282298 // The advantage of polling is that it works reliably
283299 // 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
285301 // changes is 2*msInterval (by default 5 seconds).
286302 // The overhead of this is .04 percent (1/2500) with
287303 // average pause of < 1 millisecond (and max
@@ -406,7 +422,7 @@ namespace ts {
406422 } ;
407423 } ,
408424 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
410426 // (ref: https://github.com/nodejs/node/pull/2649 and https://github.com/Microsoft/TypeScript/issues/4643)
411427 return _fs . watch (
412428 path ,
@@ -454,6 +470,37 @@ namespace ts {
454470 }
455471 } ;
456472 }
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+
457504 if ( typeof WScript !== "undefined" && typeof ActiveXObject === "function" ) {
458505 return getWScriptSystem ( ) ;
459506 }
@@ -462,8 +509,13 @@ namespace ts {
462509 // process.browser check excludes webpack and browserify
463510 return getNodeSystem ( ) ;
464511 }
512+ else if ( typeof ChakraHost !== "undefined" ) {
513+ return getChakraSystem ( ) ;
514+ }
465515 else {
466516 return undefined ; // Unsupported host
467517 }
468518 } ) ( ) ;
469519}
520+
521+
0 commit comments