From 956a6b720abf65376e80de610f819ec06afcf260 Mon Sep 17 00:00:00 2001 From: Paul van Brenk Date: Tue, 1 Dec 2015 16:18:06 -0800 Subject: [PATCH 1/3] Add support for Chakra Host in sys. --- src/compiler/sys.ts | 71 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 69 insertions(+), 2 deletions(-) diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index 3a90ca42fa1..a05383f0b9a 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -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 } })(); } + + From 8d3e4f34756839b483ee3a7219c264c3e2b75366 Mon Sep 17 00:00:00 2001 From: Paul van Brenk Date: Tue, 1 Dec 2015 17:44:43 -0800 Subject: [PATCH 2/3] cr feedback --- src/compiler/sys.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index a05383f0b9a..3b75a26a9a0 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -60,7 +60,7 @@ namespace ts { readFile(path: string): string; writeFile(path: string, contents: string): void; readDirectory(path: string, extension?: string, exclude?: string[]): string[]; - } + }; export var sys: System = (function () { @@ -209,6 +209,7 @@ namespace ts { } }; } + function getNodeSystem(): System { const _fs = require("fs"); const _path = require("path"); @@ -469,10 +470,11 @@ namespace ts { } }; } + function getChakraSystem(): System { return { - newLine: '\r\n', + newLine: "\r\n", args: ChakraHost.args, useCaseSensitiveFileNames: false, write(message: string) { @@ -516,6 +518,7 @@ namespace ts { } }; } + if (typeof WScript !== "undefined" && typeof ActiveXObject === "function") { return getWScriptSystem(); } From dfb32c5daeec501ae0817dadceeda1cb0d7a35c5 Mon Sep 17 00:00:00 2001 From: Paul van Brenk Date: Wed, 2 Dec 2015 11:49:54 -0800 Subject: [PATCH 3/3] Simplified after CR feedback. --- src/compiler/sys.ts | 36 +++++++++--------------------------- 1 file changed, 9 insertions(+), 27 deletions(-) diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index 3b75a26a9a0..cd7580119b5 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -477,9 +477,7 @@ namespace ts { newLine: "\r\n", args: ChakraHost.args, useCaseSensitiveFileNames: false, - write(message: string) { - ChakraHost.echo(message); - }, + write: ChakraHost.echo, readFile(path: string, encoding?: string) { // encoding is automatically handled by the implementation in ChakraHost return ChakraHost.readFile(path); @@ -492,30 +490,14 @@ namespace ts { 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); - } + resolvePath: ChakraHost.resolvePath, + fileExists: ChakraHost.fileExists, + directoryExists: ChakraHost.directoryExists, + createDirectory: ChakraHost.createDirectory, + getExecutingFilePath: () => ChakraHost.executingFile, + getCurrentDirectory: () => ChakraHost.currentDirectory, + readDirectory: ChakraHost.readDirectory, + exit: ChakraHost.quit, }; }