2014-07-16 19:49:11 +02:00
|
|
|
/// <reference path="diagnosticInformationMap.generated.ts"/>
|
|
|
|
|
2014-07-13 01:04:16 +02:00
|
|
|
interface System {
|
|
|
|
args: string[];
|
|
|
|
newLine: string;
|
2014-07-14 19:45:24 +02:00
|
|
|
useCaseSensitiveFileNames: boolean;
|
2014-07-13 01:04:16 +02:00
|
|
|
write(s: string): void;
|
|
|
|
writeErr(s: string): void;
|
2014-07-16 19:49:11 +02:00
|
|
|
readFile(fileName: string, encoding?: string): string;
|
|
|
|
writeFile(fileName: string, data: string): void;
|
2014-07-13 01:04:16 +02:00
|
|
|
resolvePath(path: string): string;
|
|
|
|
fileExists(path: string): boolean;
|
|
|
|
directoryExists(path: string): boolean;
|
|
|
|
createDirectory(directoryName: string): void;
|
|
|
|
getExecutingFilePath(): string;
|
|
|
|
getCurrentDirectory(): string;
|
|
|
|
getMemoryUsage(): number;
|
|
|
|
exit(exitCode?: number): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
declare var require: any;
|
|
|
|
declare var module: any;
|
|
|
|
declare var process: any;
|
|
|
|
declare var global: any;
|
|
|
|
|
|
|
|
var sys: System = (function () {
|
2014-07-14 19:45:24 +02:00
|
|
|
|
2014-07-13 01:04:16 +02:00
|
|
|
function getWScriptSystem(): System {
|
2014-07-14 19:45:24 +02:00
|
|
|
|
2014-07-13 01:04:16 +02:00
|
|
|
var fso = new ActiveXObject("Scripting.FileSystemObject");
|
2014-07-14 19:45:24 +02:00
|
|
|
|
|
|
|
var fileStream = new ActiveXObject("ADODB.Stream");
|
2014-07-16 20:46:25 +02:00
|
|
|
fileStream.Type = 2 /*text*/;
|
2014-07-16 19:49:11 +02:00
|
|
|
|
2014-07-14 19:45:24 +02:00
|
|
|
var binaryStream = new ActiveXObject("ADODB.Stream");
|
2014-07-16 20:46:25 +02:00
|
|
|
binaryStream.Type = 1 /*binary*/;
|
2014-07-14 19:45:24 +02:00
|
|
|
|
2014-07-13 01:04:16 +02:00
|
|
|
var args: string[] = [];
|
|
|
|
for (var i = 0; i < WScript.Arguments.length; i++) {
|
|
|
|
args[i] = WScript.Arguments.Item(i);
|
|
|
|
}
|
|
|
|
|
2014-07-16 19:49:11 +02:00
|
|
|
function readFile(fileName: string, encoding?: string): string {
|
|
|
|
if (!fso.FileExists(fileName)) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
fileStream.Open();
|
|
|
|
try {
|
|
|
|
if (encoding) {
|
|
|
|
fileStream.Charset = encoding;
|
2014-07-14 19:45:24 +02:00
|
|
|
fileStream.LoadFromFile(fileName);
|
2014-07-16 19:49:11 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Load file and read the first two bytes into a string with no interpretation
|
2014-07-14 19:45:24 +02:00
|
|
|
fileStream.Charset = "x-ansi";
|
2014-07-16 19:49:11 +02:00
|
|
|
fileStream.LoadFromFile(fileName);
|
2014-07-14 19:45:24 +02:00
|
|
|
var 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";
|
|
|
|
}
|
2014-07-16 19:49:11 +02:00
|
|
|
// ReadText method always strips byte order mark from resulting string
|
|
|
|
return fileStream.ReadText();
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
throw e.number === -2147024809 ? new Error(ts.Diagnostics.Unsupported_file_encoding.key) : e;
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
fileStream.Close();
|
2014-07-13 01:04:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-16 19:49:11 +02:00
|
|
|
function writeFile(fileName: string, data: string): void {
|
2014-07-14 19:45:24 +02:00
|
|
|
fileStream.Open();
|
|
|
|
binaryStream.Open();
|
|
|
|
try {
|
2014-07-16 19:49:11 +02:00
|
|
|
// Write characters in UTF-8 encoding
|
2014-07-14 19:45:24 +02:00
|
|
|
fileStream.Charset = "utf-8";
|
|
|
|
fileStream.WriteText(data);
|
2014-07-16 19:49:11 +02:00
|
|
|
// Skip byte order mark and copy remaining data to binary stream
|
2014-07-14 19:45:24 +02:00
|
|
|
fileStream.Position = 3;
|
|
|
|
fileStream.CopyTo(binaryStream);
|
|
|
|
binaryStream.SaveToFile(fileName, 2 /*overwrite*/);
|
|
|
|
}
|
2014-07-16 19:49:11 +02:00
|
|
|
finally {
|
2014-07-14 19:45:24 +02:00
|
|
|
binaryStream.Close();
|
|
|
|
fileStream.Close();
|
|
|
|
}
|
2014-07-13 01:04:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
args: args,
|
|
|
|
newLine: "\r\n",
|
2014-07-14 19:45:24 +02:00
|
|
|
useCaseSensitiveFileNames: false,
|
2014-07-13 01:04:16 +02:00
|
|
|
write(s: string): void {
|
|
|
|
WScript.StdOut.Write(s);
|
|
|
|
},
|
|
|
|
writeErr(s: string): void {
|
|
|
|
WScript.StdErr.Write(s);
|
|
|
|
},
|
2014-07-14 19:45:24 +02:00
|
|
|
readFile: readFile,
|
|
|
|
writeFile: writeFile,
|
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 new ActiveXObject("WScript.Shell").CurrentDirectory;
|
|
|
|
},
|
|
|
|
getMemoryUsage() {
|
|
|
|
return 0;
|
|
|
|
},
|
|
|
|
exit(exitCode?: number): void {
|
|
|
|
WScript.Quit(exitCode);
|
2014-07-14 19:45:24 +02:00
|
|
|
}
|
2014-07-13 01:04:16 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
function getNodeSystem(): System {
|
|
|
|
var _fs = require("fs");
|
|
|
|
var _path = require("path");
|
2014-07-14 19:45:24 +02:00
|
|
|
var _os = require('os');
|
|
|
|
|
2014-07-14 20:16:21 +02:00
|
|
|
var platform: string = _os.platform();
|
|
|
|
// win32\win64 are case insensitive platforms, MacOS (darwin) by default is also case insensitive
|
|
|
|
var useCaseSensitiveFileNames = platform !== "win32" && platform !== "win64" && platform !== "darwin";
|
|
|
|
|
2014-07-16 19:49:11 +02:00
|
|
|
function readFile(fileName: string, encoding?: string): string {
|
|
|
|
if (!_fs.existsSync(fileName)) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
var buffer = _fs.readFileSync(fileName);
|
|
|
|
var len = buffer.length;
|
|
|
|
if (len >= 2 && buffer[0] === 0xFE && buffer[1] === 0xFF) {
|
|
|
|
// Big endian UTF-16 byte order mark detected. Since big endian is not supported by node.js,
|
|
|
|
// flip all byte pairs and treat as little endian.
|
|
|
|
len &= ~1;
|
|
|
|
for (var i = 0; i < len; i += 2) {
|
|
|
|
var temp = buffer[i];
|
|
|
|
buffer[i] = buffer[i + 1];
|
|
|
|
buffer[i + 1] = temp;
|
2014-07-14 19:45:24 +02:00
|
|
|
}
|
2014-07-16 19:49:11 +02:00
|
|
|
return buffer.toString("utf16le", 2);
|
2014-07-14 19:45:24 +02:00
|
|
|
}
|
2014-07-16 19:49:11 +02:00
|
|
|
if (len >= 2 && buffer[0] === 0xFF && buffer[1] === 0xFE) {
|
|
|
|
// Little endian UTF-16 byte order mark detected
|
|
|
|
return buffer.toString("utf16le", 2);
|
2014-07-14 19:45:24 +02:00
|
|
|
}
|
2014-07-16 19:49:11 +02:00
|
|
|
if (len >= 3 && buffer[0] === 0xEF && buffer[1] === 0xBB && buffer[2] === 0xBF) {
|
|
|
|
// UTF-8 byte order mark detected
|
|
|
|
return buffer.toString("utf8", 3);
|
2014-07-14 19:45:24 +02:00
|
|
|
}
|
2014-07-16 19:49:11 +02:00
|
|
|
// Default is UTF-8 with no byte order mark
|
|
|
|
return buffer.toString("utf8");
|
|
|
|
}
|
|
|
|
|
|
|
|
function writeFile(fileName: string, data: string): void {
|
|
|
|
_fs.writeFileSync(fileName, data, "utf8");
|
2014-07-14 19:45:24 +02:00
|
|
|
}
|
2014-07-13 01:04:16 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
args: process.argv.slice(2),
|
|
|
|
newLine: _os.EOL,
|
2014-07-14 20:16:21 +02:00
|
|
|
useCaseSensitiveFileNames: useCaseSensitiveFileNames,
|
2014-07-13 01:04:16 +02:00
|
|
|
write(s: string): void {
|
|
|
|
process.stdout.write(s);
|
|
|
|
},
|
|
|
|
writeErr(s: string): void {
|
|
|
|
process.stderr.write(s);
|
|
|
|
},
|
2014-07-14 19:45:24 +02:00
|
|
|
readFile: readFile,
|
|
|
|
writeFile: writeFile,
|
2014-07-13 01:04:16 +02:00
|
|
|
resolvePath: function (path: string): string {
|
|
|
|
return _path.resolve(path);
|
|
|
|
},
|
|
|
|
fileExists(path: string): boolean {
|
|
|
|
return _fs.existsSync(path);
|
|
|
|
},
|
|
|
|
directoryExists(path: string) {
|
|
|
|
return _fs.existsSync(path) && _fs.statSync(path).isDirectory();
|
|
|
|
},
|
|
|
|
createDirectory(directoryName: string) {
|
|
|
|
if (!this.directoryExists(directoryName)) {
|
|
|
|
_fs.mkdirSync(directoryName);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
getExecutingFilePath() {
|
|
|
|
return process.mainModule.filename;
|
|
|
|
},
|
|
|
|
getCurrentDirectory() {
|
|
|
|
return (<any>process).cwd();
|
|
|
|
},
|
|
|
|
getMemoryUsage() {
|
|
|
|
global.gc();
|
|
|
|
return process.memoryUsage().heapUsed;
|
|
|
|
},
|
|
|
|
exit(exitCode?: number): void {
|
|
|
|
process.exit(exitCode);
|
2014-07-14 19:45:24 +02:00
|
|
|
}
|
2014-07-13 01:04:16 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
if (typeof WScript !== "undefined" && typeof ActiveXObject === "function") {
|
|
|
|
return getWScriptSystem();
|
|
|
|
}
|
|
|
|
else if (typeof module !== "undefined" && module.exports) {
|
|
|
|
return getNodeSystem();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return undefined; // Unsupported host
|
|
|
|
}
|
2014-07-16 19:49:11 +02:00
|
|
|
})();
|