2014-08-07 08:21:53 +02:00
|
|
|
/// <reference path='..\..\src\compiler\tsc.ts'/>
|
2014-07-13 01:04:16 +02:00
|
|
|
|
2016-07-27 16:26:28 +02:00
|
|
|
namespace TypeScript.WebTsc {
|
2014-07-13 01:04:16 +02:00
|
|
|
|
|
|
|
declare var RealActiveXObject: { new (s: string): any };
|
|
|
|
|
2015-01-19 23:58:34 +01:00
|
|
|
function getWScriptSystem() {
|
2016-07-27 16:26:28 +02:00
|
|
|
const fso = new RealActiveXObject("Scripting.FileSystemObject");
|
2015-01-19 23:58:34 +01:00
|
|
|
|
2016-07-27 16:26:28 +02:00
|
|
|
const fileStream = new ActiveXObject("ADODB.Stream");
|
2015-01-19 23:58:34 +01:00
|
|
|
fileStream.Type = 2 /*text*/;
|
|
|
|
|
2016-07-27 16:26:28 +02:00
|
|
|
const args: string[] = [];
|
|
|
|
for (let i = 0; i < WScript.Arguments.length; i++) {
|
2014-07-13 01:04:16 +02:00
|
|
|
args[i] = WScript.Arguments.Item(i);
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
args: args,
|
|
|
|
newLine: "\r\n",
|
|
|
|
write(s: string): void {
|
|
|
|
WScript.StdOut.Write(s);
|
|
|
|
},
|
|
|
|
writeErr(s: string): void {
|
|
|
|
WScript.StdErr.Write(s);
|
|
|
|
},
|
2015-01-19 23:58:34 +01:00
|
|
|
readFile(fileName: string, encoding?: string): string {
|
|
|
|
if (!fso.FileExists(fileName)) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
fileStream.Open();
|
2014-07-13 01:04:16 +02:00
|
|
|
try {
|
2015-01-19 23:58:34 +01:00
|
|
|
if (encoding) {
|
|
|
|
fileStream.Charset = encoding;
|
|
|
|
fileStream.LoadFromFile(fileName);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Load file and read the first two bytes into a string with no interpretation
|
|
|
|
fileStream.Charset = "x-ansi";
|
|
|
|
fileStream.LoadFromFile(fileName);
|
2016-07-27 16:26:28 +02:00
|
|
|
const bom = fileStream.ReadText(2) || "";
|
2015-01-19 23:58:34 +01:00
|
|
|
// Position must be at 0 before encoding can be changed
|
|
|
|
fileStream.Position = 0;
|
|
|
|
// [0xFF,0xFE] and [0xFE,0xFF] mean utf-16 (little or big endian), otherwise default to utf-8
|
|
|
|
fileStream.Charset = bom.length >= 2 && (bom.charCodeAt(0) === 0xFF && bom.charCodeAt(1) === 0xFE || bom.charCodeAt(0) === 0xFE && bom.charCodeAt(1) === 0xFF) ? "unicode" : "utf-8";
|
|
|
|
}
|
|
|
|
// ReadText method always strips byte order mark from resulting string
|
|
|
|
return fileStream.ReadText();
|
2014-07-13 01:04:16 +02:00
|
|
|
}
|
|
|
|
catch (e) {
|
2015-01-19 23:58:34 +01:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
fileStream.Close();
|
2014-07-13 01:04:16 +02:00
|
|
|
}
|
|
|
|
},
|
2014-07-14 23:44:56 +02:00
|
|
|
writeFile(fileName: string, data: string): boolean {
|
2016-07-27 16:26:28 +02:00
|
|
|
const f = fso.CreateTextFile(fileName, true);
|
2014-07-13 01:04:16 +02:00
|
|
|
f.Write(data);
|
|
|
|
f.Close();
|
2014-07-14 23:44:56 +02:00
|
|
|
return true;
|
2014-07-13 01:04:16 +02:00
|
|
|
},
|
|
|
|
resolvePath(path: string): string {
|
|
|
|
return fso.GetAbsolutePathName(path);
|
|
|
|
},
|
|
|
|
fileExists(path: string): boolean {
|
|
|
|
return fso.FileExists(path);
|
|
|
|
},
|
|
|
|
directoryExists(path: string) {
|
|
|
|
return fso.FolderExists(path);
|
|
|
|
},
|
|
|
|
createDirectory(directoryName: string) {
|
|
|
|
if (!this.directoryExists(directoryName)) {
|
|
|
|
fso.CreateFolder(directoryName);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
getExecutingFilePath() {
|
|
|
|
return WScript.ScriptFullName;
|
|
|
|
},
|
|
|
|
getCurrentDirectory() {
|
|
|
|
return "";
|
|
|
|
},
|
|
|
|
getMemoryUsage() {
|
|
|
|
return 0;
|
|
|
|
},
|
|
|
|
exit(exitCode?: number): void {
|
|
|
|
WScript.Quit(exitCode);
|
|
|
|
},
|
|
|
|
useCaseSensitiveFileNames: false
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function prepareCompiler(currentDir: string, stdOut: ITextWriter, stdErr: ITextWriter) {
|
2016-07-27 16:26:28 +02:00
|
|
|
const shell = new RealActiveXObject("WScript.Shell");
|
2014-07-13 01:04:16 +02:00
|
|
|
shell.CurrentDirectory = currentDir;
|
|
|
|
WScript.ScriptFullName = currentDir + "\\tc.js";
|
|
|
|
WScript.StdOut = stdOut;
|
|
|
|
WScript.StdErr = stdErr;
|
|
|
|
sys = getWScriptSystem();
|
|
|
|
|
2016-07-27 16:26:28 +02:00
|
|
|
return (commandLine: string) => {
|
2014-07-13 01:04:16 +02:00
|
|
|
ts.executeCommandLine(commandLine.split(" "));
|
2016-07-27 16:26:28 +02:00
|
|
|
};
|
2014-07-13 01:04:16 +02:00
|
|
|
}
|
|
|
|
}
|