Skip to content

Commit 956a6b7

Browse files
author
Paul van Brenk
committed
Add support for Chakra Host in sys.
1 parent fb76dc9 commit 956a6b7

1 file changed

Lines changed: 69 additions & 2 deletions

File tree

src/compiler/sys.ts

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {
@@ -281,7 +296,7 @@ namespace ts {
281296
// REVIEW: for now this implementation uses polling.
282297
// The advantage of polling is that it works reliably
283298
// on all os and with network mounted files.
284-
// For 90 referenced files, the average time to detect
299+
// For 90 referenced files, the average time to detect
285300
// changes is 2*msInterval (by default 5 seconds).
286301
// The overhead of this is .04 percent (1/2500) with
287302
// average pause of < 1 millisecond (and max
@@ -406,7 +421,7 @@ namespace ts {
406421
};
407422
},
408423
watchDirectory: (path, callback, recursive) => {
409-
// Node 4.0 `fs.watch` function supports the "recursive" option on both OSX and Windows
424+
// Node 4.0 `fs.watch` function supports the "recursive" option on both OSX and Windows
410425
// (ref: https://github.com/nodejs/node/pull/2649 and https://github.com/Microsoft/TypeScript/issues/4643)
411426
return _fs.watch(
412427
path,
@@ -454,6 +469,53 @@ namespace ts {
454469
}
455470
};
456471
}
472+
function getChakraSystem(): System {
473+
474+
return {
475+
newLine: '\r\n',
476+
args: ChakraHost.args,
477+
useCaseSensitiveFileNames: false,
478+
write(message: string) {
479+
ChakraHost.echo(message);
480+
},
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(path: string) {
494+
return ChakraHost.resolvePath(path);
495+
},
496+
fileExists(path: string) {
497+
return ChakraHost.fileExists(path);
498+
},
499+
directoryExists(path: string) {
500+
return ChakraHost.directoryExists(path);
501+
},
502+
createDirectory(path: string) {
503+
ChakraHost.createDirectory(path);
504+
},
505+
getExecutingFilePath() {
506+
return ChakraHost.executingFile;
507+
},
508+
getCurrentDirectory() {
509+
return ChakraHost.currentDirectory;
510+
},
511+
readDirectory(path: string, extension?: string, exclude?: string[]) {
512+
return ChakraHost.readDirectory(path, extension, exclude);
513+
},
514+
exit(exitCode?: number) {
515+
ChakraHost.quit(exitCode);
516+
}
517+
};
518+
}
457519
if (typeof WScript !== "undefined" && typeof ActiveXObject === "function") {
458520
return getWScriptSystem();
459521
}
@@ -462,8 +524,13 @@ namespace ts {
462524
// process.browser check excludes webpack and browserify
463525
return getNodeSystem();
464526
}
527+
else if (typeof ChakraHost !== "undefined") {
528+
return getChakraSystem();
529+
}
465530
else {
466531
return undefined; // Unsupported host
467532
}
468533
})();
469534
}
535+
536+

0 commit comments

Comments
 (0)