Add support for Chakra Host in sys.

This commit is contained in:
Paul van Brenk 2015-12-01 16:18:06 -08:00
parent fb76dc95de
commit 956a6b720a

View file

@ -47,6 +47,21 @@ namespace ts {
constructor(o: any);
}
declare var ChakraHost: {
args: string[];
currentDirectory: string;
executingFile: string;
echo(s: string): void;
quit(exitCode?: number): void;
fileExists(path: string): boolean;
directoryExists(path: string): boolean;
createDirectory(path: string): void;
resolvePath(path: string): string;
readFile(path: string): string;
writeFile(path: string, contents: string): void;
readDirectory(path: string, extension?: string, exclude?: string[]): string[];
}
export var sys: System = (function () {
function getWScriptSystem(): System {
@ -281,7 +296,7 @@ namespace ts {
// REVIEW: for now this implementation uses polling.
// The advantage of polling is that it works reliably
// on all os and with network mounted files.
// For 90 referenced files, the average time to detect
// For 90 referenced files, the average time to detect
// changes is 2*msInterval (by default 5 seconds).
// The overhead of this is .04 percent (1/2500) with
// average pause of < 1 millisecond (and max
@ -406,7 +421,7 @@ namespace ts {
};
},
watchDirectory: (path, callback, recursive) => {
// Node 4.0 `fs.watch` function supports the "recursive" option on both OSX and Windows
// Node 4.0 `fs.watch` function supports the "recursive" option on both OSX and Windows
// (ref: https://github.com/nodejs/node/pull/2649 and https://github.com/Microsoft/TypeScript/issues/4643)
return _fs.watch(
path,
@ -454,6 +469,53 @@ namespace ts {
}
};
}
function getChakraSystem(): System {
return {
newLine: '\r\n',
args: ChakraHost.args,
useCaseSensitiveFileNames: false,
write(message: string) {
ChakraHost.echo(message);
},
readFile(path: string, encoding?: string) {
// encoding is automatically handled by the implementation in ChakraHost
return ChakraHost.readFile(path);
},
writeFile(path: string, data: string, writeByteOrderMark?: boolean) {
// If a BOM is required, emit one
if (writeByteOrderMark) {
data = "\uFEFF" + data;
}
ChakraHost.writeFile(path, data);
},
resolvePath(path: string) {
return ChakraHost.resolvePath(path);
},
fileExists(path: string) {
return ChakraHost.fileExists(path);
},
directoryExists(path: string) {
return ChakraHost.directoryExists(path);
},
createDirectory(path: string) {
ChakraHost.createDirectory(path);
},
getExecutingFilePath() {
return ChakraHost.executingFile;
},
getCurrentDirectory() {
return ChakraHost.currentDirectory;
},
readDirectory(path: string, extension?: string, exclude?: string[]) {
return ChakraHost.readDirectory(path, extension, exclude);
},
exit(exitCode?: number) {
ChakraHost.quit(exitCode);
}
};
}
if (typeof WScript !== "undefined" && typeof ActiveXObject === "function") {
return getWScriptSystem();
}
@ -462,8 +524,13 @@ namespace ts {
// process.browser check excludes webpack and browserify
return getNodeSystem();
}
else if (typeof ChakraHost !== "undefined") {
return getChakraSystem();
}
else {
return undefined; // Unsupported host
}
})();
}