forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwebtsc.ts
104 lines (97 loc) · 3.95 KB
/
webtsc.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/// <reference path='..\..\src\compiler\tsc.ts'/>
namespace TypeScript.WebTsc {
declare var RealActiveXObject: { new (s: string): any };
function getWScriptSystem() {
const fso = new RealActiveXObject("Scripting.FileSystemObject");
const fileStream = new ActiveXObject("ADODB.Stream");
fileStream.Type = 2 /*text*/;
const args: string[] = [];
for (let i = 0; i < WScript.Arguments.length; i++) {
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);
},
readFile(fileName: string, encoding?: string): string {
if (!fso.FileExists(fileName)) {
return undefined;
}
fileStream.Open();
try {
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);
const bom = fileStream.ReadText(2) || "";
// 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();
}
catch (e) {
throw e;
}
finally {
fileStream.Close();
}
},
writeFile(fileName: string, data: string): boolean {
const f = fso.CreateTextFile(fileName, true);
f.Write(data);
f.Close();
return true;
},
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) {
const shell = new RealActiveXObject("WScript.Shell");
shell.CurrentDirectory = currentDir;
WScript.ScriptFullName = currentDir + "\\tc.js";
WScript.StdOut = stdOut;
WScript.StdErr = stdErr;
sys = getWScriptSystem();
return (commandLine: string) => {
ts.executeCommandLine(commandLine.split(" "));
};
}
}