2015-01-16 03:18:19 +01:00
|
|
|
/// <reference path="core.ts"/>
|
2014-07-16 19:49:11 +02:00
|
|
|
|
2015-06-12 18:01:48 +02:00
|
|
|
namespace ts {
|
2016-03-23 20:49:34 +01:00
|
|
|
export type FileWatcherCallback = (fileName: string, removed?: boolean) => void;
|
2016-06-07 11:14:56 +02:00
|
|
|
export type DirectoryWatcherCallback = (fileName: string) => void;
|
2016-04-22 19:20:41 +02:00
|
|
|
export interface WatchedFile {
|
|
|
|
fileName: string;
|
|
|
|
callback: FileWatcherCallback;
|
|
|
|
mtime?: Date;
|
|
|
|
}
|
2015-12-22 01:29:04 +01:00
|
|
|
|
2014-12-06 01:33:39 +01:00
|
|
|
export interface System {
|
|
|
|
args: string[];
|
|
|
|
newLine: string;
|
|
|
|
useCaseSensitiveFileNames: boolean;
|
|
|
|
write(s: string): void;
|
2015-04-09 23:18:32 +02:00
|
|
|
readFile(path: string, encoding?: string): string;
|
2016-03-17 23:55:53 +01:00
|
|
|
getFileSize?(path: string): number;
|
2015-04-09 23:18:32 +02:00
|
|
|
writeFile(path: string, data: string, writeByteOrderMark?: boolean): void;
|
2016-03-23 20:49:34 +01:00
|
|
|
watchFile?(path: string, callback: FileWatcherCallback): FileWatcher;
|
2016-01-08 07:48:17 +01:00
|
|
|
watchDirectory?(path: string, callback: DirectoryWatcherCallback, recursive?: boolean): FileWatcher;
|
2014-12-06 01:33:39 +01:00
|
|
|
resolvePath(path: string): string;
|
|
|
|
fileExists(path: string): boolean;
|
|
|
|
directoryExists(path: string): boolean;
|
2015-04-09 23:18:32 +02:00
|
|
|
createDirectory(path: string): void;
|
2014-12-06 01:33:39 +01:00
|
|
|
getExecutingFilePath(): string;
|
|
|
|
getCurrentDirectory(): string;
|
2016-05-18 00:41:31 +02:00
|
|
|
getDirectories(path: string): string[];
|
2015-12-15 00:21:12 +01:00
|
|
|
readDirectory(path: string, extensions?: string[], exclude?: string[], include?: string[]): string[];
|
2016-02-09 15:23:43 +01:00
|
|
|
getModifiedTime?(path: string): Date;
|
|
|
|
createHash?(data: string): string;
|
2015-04-09 23:18:32 +02:00
|
|
|
getMemoryUsage?(): number;
|
2014-12-06 01:33:39 +01:00
|
|
|
exit(exitCode?: number): void;
|
2016-05-05 22:38:09 +02:00
|
|
|
realpath?(path: string): string;
|
2015-10-02 00:40:13 +02:00
|
|
|
}
|
|
|
|
|
2014-12-06 01:33:39 +01:00
|
|
|
export interface FileWatcher {
|
|
|
|
close(): void;
|
|
|
|
}
|
|
|
|
|
2016-01-08 07:48:17 +01:00
|
|
|
export interface DirectoryWatcher extends FileWatcher {
|
2016-03-23 20:49:34 +01:00
|
|
|
directoryName: string;
|
2015-12-23 00:26:21 +01:00
|
|
|
referenceCount: number;
|
|
|
|
}
|
2014-12-06 01:33:39 +01:00
|
|
|
|
|
|
|
declare var require: any;
|
|
|
|
declare var module: any;
|
|
|
|
declare var process: any;
|
|
|
|
declare var global: any;
|
|
|
|
declare var __filename: string;
|
2015-10-01 01:10:52 +02:00
|
|
|
declare var Buffer: {
|
|
|
|
new (str: string, encoding?: string): any;
|
2015-07-24 00:18:48 +02:00
|
|
|
};
|
2014-12-06 01:33:39 +01:00
|
|
|
|
2015-01-15 22:22:23 +01:00
|
|
|
declare class Enumerator {
|
|
|
|
public atEnd(): boolean;
|
|
|
|
public moveNext(): boolean;
|
|
|
|
public item(): any;
|
|
|
|
constructor(o: any);
|
|
|
|
}
|
|
|
|
|
2015-12-02 01:18:06 +01:00
|
|
|
declare var ChakraHost: {
|
|
|
|
args: string[];
|
|
|
|
currentDirectory: string;
|
|
|
|
executingFile: string;
|
2015-12-12 01:32:44 +01:00
|
|
|
newLine?: string;
|
|
|
|
useCaseSensitiveFileNames?: boolean;
|
2015-12-02 01:18:06 +01:00
|
|
|
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;
|
2016-05-18 00:41:31 +02:00
|
|
|
getDirectories(path: string): string[];
|
2016-05-31 19:11:04 +02:00
|
|
|
readDirectory(path: string, extensions?: string[], basePaths?: string[], excludeEx?: string, includeFileEx?: string, includeDirEx?: string): string[];
|
2015-12-23 00:26:21 +01:00
|
|
|
watchFile?(path: string, callback: FileWatcherCallback): FileWatcher;
|
2016-01-08 07:48:17 +01:00
|
|
|
watchDirectory?(path: string, callback: DirectoryWatcherCallback, recursive?: boolean): FileWatcher;
|
2016-05-05 22:38:09 +02:00
|
|
|
realpath(path: string): string;
|
2015-12-02 02:44:43 +01:00
|
|
|
};
|
2015-12-02 01:18:06 +01:00
|
|
|
|
2016-03-12 00:56:36 +01:00
|
|
|
export var sys: System = (function() {
|
2014-12-06 01:33:39 +01:00
|
|
|
|
|
|
|
function getWScriptSystem(): System {
|
|
|
|
|
2015-11-04 23:02:33 +01:00
|
|
|
const fso = new ActiveXObject("Scripting.FileSystemObject");
|
2015-12-15 00:21:12 +01:00
|
|
|
const shell = new ActiveXObject("WScript.Shell");
|
2014-07-13 01:04:16 +02:00
|
|
|
|
2015-11-04 23:02:33 +01:00
|
|
|
const fileStream = new ActiveXObject("ADODB.Stream");
|
2014-12-06 01:33:39 +01:00
|
|
|
fileStream.Type = 2 /*text*/;
|
|
|
|
|
2015-11-04 23:02:33 +01:00
|
|
|
const binaryStream = new ActiveXObject("ADODB.Stream");
|
2014-12-06 01:33:39 +01:00
|
|
|
binaryStream.Type = 1 /*binary*/;
|
|
|
|
|
2015-11-04 23:02:33 +01:00
|
|
|
const args: string[] = [];
|
2015-07-09 02:42:26 +02:00
|
|
|
for (let i = 0; i < WScript.Arguments.length; i++) {
|
2014-12-06 01:33:39 +01:00
|
|
|
args[i] = WScript.Arguments.Item(i);
|
2014-07-16 19:49:11 +02:00
|
|
|
}
|
2014-12-06 01:33:39 +01:00
|
|
|
|
|
|
|
function readFile(fileName: string, encoding?: string): string {
|
|
|
|
if (!fso.FileExists(fileName)) {
|
|
|
|
return undefined;
|
2014-07-16 19:49:11 +02:00
|
|
|
}
|
2014-12-06 01:33:39 +01:00
|
|
|
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);
|
2015-11-04 23:02:33 +01:00
|
|
|
const bom = fileStream.ReadText(2) || "";
|
2014-12-06 01:33:39 +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();
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
fileStream.Close();
|
2014-07-14 19:45:24 +02:00
|
|
|
}
|
2014-07-13 01:04:16 +02:00
|
|
|
}
|
|
|
|
|
2014-12-06 01:33:39 +01:00
|
|
|
function writeFile(fileName: string, data: string, writeByteOrderMark?: boolean): void {
|
|
|
|
fileStream.Open();
|
|
|
|
binaryStream.Open();
|
|
|
|
try {
|
|
|
|
// Write characters in UTF-8 encoding
|
|
|
|
fileStream.Charset = "utf-8";
|
|
|
|
fileStream.WriteText(data);
|
|
|
|
// If we don't want the BOM, then skip it by setting the starting location to 3 (size of BOM).
|
|
|
|
// If not, start from position 0, as the BOM will be added automatically when charset==utf8.
|
|
|
|
if (writeByteOrderMark) {
|
|
|
|
fileStream.Position = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fileStream.Position = 3;
|
|
|
|
}
|
|
|
|
fileStream.CopyTo(binaryStream);
|
|
|
|
binaryStream.SaveToFile(fileName, 2 /*overwrite*/);
|
2014-08-06 20:32:51 +02:00
|
|
|
}
|
2014-12-06 01:33:39 +01:00
|
|
|
finally {
|
|
|
|
binaryStream.Close();
|
|
|
|
fileStream.Close();
|
2014-08-06 20:32:51 +02:00
|
|
|
}
|
2014-07-14 19:45:24 +02:00
|
|
|
}
|
2014-07-13 01:04:16 +02:00
|
|
|
|
2015-10-01 01:10:52 +02:00
|
|
|
function getNames(collection: any): string[] {
|
2015-11-04 23:02:33 +01:00
|
|
|
const result: string[] = [];
|
2015-07-09 02:42:26 +02:00
|
|
|
for (let e = new Enumerator(collection); !e.atEnd(); e.moveNext()) {
|
2015-01-15 22:22:23 +01:00
|
|
|
result.push(e.item().Name);
|
|
|
|
}
|
|
|
|
return result.sort();
|
|
|
|
}
|
|
|
|
|
2016-05-18 00:41:31 +02:00
|
|
|
function getDirectories(path: string): string[] {
|
|
|
|
const folder = fso.GetFolder(path);
|
|
|
|
return getNames(folder.subfolders);
|
|
|
|
}
|
|
|
|
|
2015-12-15 00:21:12 +01:00
|
|
|
function getAccessibleFileSystemEntries(path: string): FileSystemEntries {
|
|
|
|
try {
|
2015-11-04 23:02:33 +01:00
|
|
|
const folder = fso.GetFolder(path || ".");
|
|
|
|
const files = getNames(folder.files);
|
2015-12-15 00:21:12 +01:00
|
|
|
const directories = getNames(folder.subfolders);
|
|
|
|
return { files, directories };
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
return { files: [], directories: [] };
|
2015-01-15 22:22:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-15 00:21:12 +01:00
|
|
|
function readDirectory(path: string, extensions?: string[], excludes?: string[], includes?: string[]): string[] {
|
|
|
|
return matchFiles(path, extensions, excludes, includes, /*useCaseSensitiveFileNames*/ false, shell.CurrentDirectory, getAccessibleFileSystemEntries);
|
2015-12-03 19:44:24 +01:00
|
|
|
}
|
|
|
|
|
2016-07-11 20:30:22 +02:00
|
|
|
const wscriptSystem: System = {
|
2014-12-06 01:33:39 +01:00
|
|
|
args,
|
|
|
|
newLine: "\r\n",
|
|
|
|
useCaseSensitiveFileNames: false,
|
|
|
|
write(s: string): void {
|
|
|
|
WScript.StdOut.Write(s);
|
|
|
|
},
|
|
|
|
readFile,
|
|
|
|
writeFile,
|
|
|
|
resolvePath(path: string): string {
|
|
|
|
return fso.GetAbsolutePathName(path);
|
|
|
|
},
|
|
|
|
fileExists(path: string): boolean {
|
|
|
|
return fso.FileExists(path);
|
|
|
|
},
|
|
|
|
directoryExists(path: string) {
|
|
|
|
return fso.FolderExists(path);
|
|
|
|
},
|
2016-07-11 20:30:22 +02:00
|
|
|
createDirectory(directoryName: string) {
|
|
|
|
if (!wscriptSystem.directoryExists(directoryName)) {
|
2014-12-06 01:33:39 +01:00
|
|
|
fso.CreateFolder(directoryName);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
getExecutingFilePath() {
|
|
|
|
return WScript.ScriptFullName;
|
|
|
|
},
|
|
|
|
getCurrentDirectory() {
|
2015-12-15 00:21:12 +01:00
|
|
|
return shell.CurrentDirectory;
|
2014-12-06 01:33:39 +01:00
|
|
|
},
|
2016-05-18 00:41:31 +02:00
|
|
|
getDirectories,
|
2015-01-15 22:22:23 +01:00
|
|
|
readDirectory,
|
2014-12-06 01:33:39 +01:00
|
|
|
exit(exitCode?: number): void {
|
|
|
|
try {
|
|
|
|
WScript.Quit(exitCode);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
}
|
2014-07-13 01:04:16 +02:00
|
|
|
}
|
2014-12-06 01:33:39 +01:00
|
|
|
};
|
2016-07-11 20:30:22 +02:00
|
|
|
return wscriptSystem;
|
2014-12-06 01:33:39 +01:00
|
|
|
}
|
2015-12-02 02:44:43 +01:00
|
|
|
|
2014-12-06 01:33:39 +01:00
|
|
|
function getNodeSystem(): System {
|
2015-07-24 00:18:48 +02:00
|
|
|
const _fs = require("fs");
|
|
|
|
const _path = require("path");
|
2015-07-24 02:30:31 +02:00
|
|
|
const _os = require("os");
|
2016-02-06 09:51:25 +01:00
|
|
|
const _crypto = require("crypto");
|
2014-12-06 01:33:39 +01:00
|
|
|
|
2016-04-22 19:20:41 +02:00
|
|
|
const useNonPollingWatchers = process.env["TSC_NONPOLLING_WATCHER"];
|
|
|
|
|
|
|
|
function createWatchedFileSet() {
|
2016-08-11 01:47:06 +02:00
|
|
|
const dirWatchers = createMap<DirectoryWatcher>();
|
2016-04-22 19:20:41 +02:00
|
|
|
// One file can have multiple watchers
|
2016-08-11 01:47:06 +02:00
|
|
|
const fileWatcherCallbacks = createMap<FileWatcherCallback[]>();
|
2016-04-22 19:20:41 +02:00
|
|
|
return { addFile, removeFile };
|
|
|
|
|
|
|
|
function reduceDirWatcherRefCountForFile(fileName: string) {
|
|
|
|
const dirName = getDirectoryPath(fileName);
|
2016-08-11 01:47:06 +02:00
|
|
|
const watcher = dirWatchers[dirName];
|
|
|
|
if (watcher) {
|
2016-04-22 19:20:41 +02:00
|
|
|
watcher.referenceCount -= 1;
|
|
|
|
if (watcher.referenceCount <= 0) {
|
|
|
|
watcher.close();
|
|
|
|
delete dirWatchers[dirName];
|
|
|
|
}
|
|
|
|
}
|
2015-10-02 00:40:13 +02:00
|
|
|
}
|
|
|
|
|
2016-04-22 19:20:41 +02:00
|
|
|
function addDirWatcher(dirPath: string): void {
|
2016-08-11 01:47:06 +02:00
|
|
|
let watcher = dirWatchers[dirPath];
|
|
|
|
if (watcher) {
|
2016-04-22 19:20:41 +02:00
|
|
|
watcher.referenceCount += 1;
|
2015-10-02 00:40:13 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-08-11 01:47:06 +02:00
|
|
|
watcher = _fs.watch(
|
2016-04-22 19:20:41 +02:00
|
|
|
dirPath,
|
|
|
|
{ persistent: true },
|
|
|
|
(eventName: string, relativeFileName: string) => fileEventHandler(eventName, relativeFileName, dirPath)
|
|
|
|
);
|
|
|
|
watcher.referenceCount = 1;
|
|
|
|
dirWatchers[dirPath] = watcher;
|
|
|
|
return;
|
2015-10-02 00:40:13 +02:00
|
|
|
}
|
|
|
|
|
2016-04-22 19:20:41 +02:00
|
|
|
function addFileWatcherCallback(filePath: string, callback: FileWatcherCallback): void {
|
2016-08-15 22:45:33 +02:00
|
|
|
multiMapAdd(fileWatcherCallbacks, filePath, callback);
|
2015-10-02 00:40:13 +02:00
|
|
|
}
|
|
|
|
|
2016-04-22 19:20:41 +02:00
|
|
|
function addFile(fileName: string, callback: FileWatcherCallback): WatchedFile {
|
|
|
|
addFileWatcherCallback(fileName, callback);
|
|
|
|
addDirWatcher(getDirectoryPath(fileName));
|
2015-10-02 00:40:13 +02:00
|
|
|
|
2016-04-22 19:20:41 +02:00
|
|
|
return { fileName, callback };
|
2015-10-02 00:40:13 +02:00
|
|
|
}
|
|
|
|
|
2016-04-22 19:20:41 +02:00
|
|
|
function removeFile(watchedFile: WatchedFile) {
|
|
|
|
removeFileWatcherCallback(watchedFile.fileName, watchedFile.callback);
|
|
|
|
reduceDirWatcherRefCountForFile(watchedFile.fileName);
|
2015-10-15 00:10:05 +02:00
|
|
|
}
|
|
|
|
|
2016-04-22 19:20:41 +02:00
|
|
|
function removeFileWatcherCallback(filePath: string, callback: FileWatcherCallback) {
|
2016-08-11 01:47:06 +02:00
|
|
|
const callbacks = fileWatcherCallbacks[filePath];
|
|
|
|
if (callbacks) {
|
2016-09-01 21:40:31 +02:00
|
|
|
unorderedRemoveItem(callbacks, callback);
|
2016-08-15 20:00:08 +02:00
|
|
|
if (callbacks.length === 0) {
|
2016-04-22 19:20:41 +02:00
|
|
|
delete fileWatcherCallbacks[filePath];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-02 00:40:13 +02:00
|
|
|
|
2016-04-22 19:20:41 +02:00
|
|
|
function fileEventHandler(eventName: string, relativeFileName: string, baseDirPath: string) {
|
|
|
|
// When files are deleted from disk, the triggered "rename" event would have a relativefileName of "undefined"
|
|
|
|
const fileName = typeof relativeFileName !== "string"
|
|
|
|
? undefined
|
|
|
|
: ts.getNormalizedAbsolutePath(relativeFileName, baseDirPath);
|
|
|
|
// Some applications save a working file via rename operations
|
2016-08-11 01:47:06 +02:00
|
|
|
if ((eventName === "change" || eventName === "rename") && fileWatcherCallbacks[fileName]) {
|
2016-04-22 19:20:41 +02:00
|
|
|
for (const fileCallback of fileWatcherCallbacks[fileName]) {
|
|
|
|
fileCallback(fileName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-04 23:02:33 +01:00
|
|
|
const watchedFileSet = createWatchedFileSet();
|
2015-10-02 00:25:43 +02:00
|
|
|
|
2015-12-23 01:00:49 +01:00
|
|
|
function isNode4OrLater(): boolean {
|
2016-04-19 23:25:57 +02:00
|
|
|
return parseInt(process.version.charAt(1)) >= 4;
|
|
|
|
}
|
2014-12-06 01:33:39 +01:00
|
|
|
|
2015-07-24 00:18:48 +02:00
|
|
|
const platform: string = _os.platform();
|
2014-12-06 01:33:39 +01:00
|
|
|
// win32\win64 are case insensitive platforms, MacOS (darwin) by default is also case insensitive
|
2015-07-24 00:18:48 +02:00
|
|
|
const useCaseSensitiveFileNames = platform !== "win32" && platform !== "win64" && platform !== "darwin";
|
2014-12-06 01:33:39 +01:00
|
|
|
|
|
|
|
function readFile(fileName: string, encoding?: string): string {
|
2016-02-12 01:59:54 +01:00
|
|
|
if (!fileExists(fileName)) {
|
2014-12-06 01:33:39 +01:00
|
|
|
return undefined;
|
2014-07-25 20:01:09 +02:00
|
|
|
}
|
2015-11-04 23:02:33 +01:00
|
|
|
const buffer = _fs.readFileSync(fileName);
|
2015-07-09 02:42:26 +02:00
|
|
|
let len = buffer.length;
|
2014-12-06 01:33:39 +01:00
|
|
|
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;
|
2015-07-09 02:42:26 +02:00
|
|
|
for (let i = 0; i < len; i += 2) {
|
2015-11-04 23:02:33 +01:00
|
|
|
const temp = buffer[i];
|
2014-12-06 01:33:39 +01:00
|
|
|
buffer[i] = buffer[i + 1];
|
|
|
|
buffer[i + 1] = temp;
|
|
|
|
}
|
|
|
|
return buffer.toString("utf16le", 2);
|
2014-07-25 20:01:09 +02:00
|
|
|
}
|
2014-12-06 01:33:39 +01: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-12-06 01:33:39 +01:00
|
|
|
if (len >= 3 && buffer[0] === 0xEF && buffer[1] === 0xBB && buffer[2] === 0xBF) {
|
|
|
|
// UTF-8 byte order mark detected
|
|
|
|
return buffer.toString("utf8", 3);
|
|
|
|
}
|
|
|
|
// Default is UTF-8 with no byte order mark
|
|
|
|
return buffer.toString("utf8");
|
2014-07-14 19:45:24 +02:00
|
|
|
}
|
2014-07-16 19:49:11 +02:00
|
|
|
|
2014-12-06 01:33:39 +01:00
|
|
|
function writeFile(fileName: string, data: string, writeByteOrderMark?: boolean): void {
|
|
|
|
// If a BOM is required, emit one
|
|
|
|
if (writeByteOrderMark) {
|
2015-07-24 02:30:31 +02:00
|
|
|
data = "\uFEFF" + data;
|
2014-12-06 01:33:39 +01:00
|
|
|
}
|
2014-08-06 20:32:51 +02:00
|
|
|
|
2015-10-29 19:13:54 +01:00
|
|
|
let fd: number;
|
|
|
|
|
|
|
|
try {
|
|
|
|
fd = _fs.openSync(fileName, "w");
|
|
|
|
_fs.writeSync(fd, data, undefined, "utf8");
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
if (fd !== undefined) {
|
|
|
|
_fs.closeSync(fd);
|
|
|
|
}
|
|
|
|
}
|
2014-12-06 01:33:39 +01:00
|
|
|
}
|
2014-07-13 01:04:16 +02:00
|
|
|
|
2015-12-15 00:21:12 +01:00
|
|
|
function getAccessibleFileSystemEntries(path: string): FileSystemEntries {
|
|
|
|
try {
|
|
|
|
const entries = _fs.readdirSync(path || ".").sort();
|
|
|
|
const files: string[] = [];
|
2015-11-04 23:02:33 +01:00
|
|
|
const directories: string[] = [];
|
2015-12-15 00:21:12 +01:00
|
|
|
for (const entry of entries) {
|
2016-05-26 02:07:36 +02:00
|
|
|
// This is necessary because on some file system node fails to exclude
|
2016-05-26 02:06:50 +02:00
|
|
|
// "." and "..". See https://github.com/nodejs/node/issues/4002
|
|
|
|
if (entry === "." || entry === "..") {
|
|
|
|
continue;
|
|
|
|
}
|
2015-12-15 00:21:12 +01:00
|
|
|
const name = combinePaths(path, entry);
|
2016-06-18 02:11:13 +02:00
|
|
|
|
|
|
|
let stat: any;
|
|
|
|
try {
|
|
|
|
stat = _fs.statSync(name);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-12-15 00:21:12 +01:00
|
|
|
if (stat.isFile()) {
|
|
|
|
files.push(entry);
|
|
|
|
}
|
|
|
|
else if (stat.isDirectory()) {
|
|
|
|
directories.push(entry);
|
2015-01-15 22:22:23 +01:00
|
|
|
}
|
|
|
|
}
|
2015-12-15 00:21:12 +01:00
|
|
|
return { files, directories };
|
2015-01-15 22:22:23 +01:00
|
|
|
}
|
2015-12-15 00:21:12 +01:00
|
|
|
catch (e) {
|
|
|
|
return { files: [], directories: [] };
|
2015-12-03 19:44:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-15 00:21:12 +01:00
|
|
|
function readDirectory(path: string, extensions?: string[], excludes?: string[], includes?: string[]): string[] {
|
|
|
|
return matchFiles(path, extensions, excludes, includes, useCaseSensitiveFileNames, process.cwd(), getAccessibleFileSystemEntries);
|
2015-12-03 19:44:24 +01:00
|
|
|
}
|
2015-05-16 20:38:28 +02:00
|
|
|
|
2016-02-12 01:59:54 +01:00
|
|
|
const enum FileSystemEntryKind {
|
|
|
|
File,
|
|
|
|
Directory
|
|
|
|
}
|
2016-02-12 02:29:01 +01:00
|
|
|
|
2016-02-12 01:59:54 +01:00
|
|
|
function fileSystemEntryExists(path: string, entryKind: FileSystemEntryKind): boolean {
|
|
|
|
try {
|
|
|
|
const stat = _fs.statSync(path);
|
|
|
|
switch (entryKind) {
|
|
|
|
case FileSystemEntryKind.File: return stat.isFile();
|
|
|
|
case FileSystemEntryKind.Directory: return stat.isDirectory();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function fileExists(path: string): boolean {
|
|
|
|
return fileSystemEntryExists(path, FileSystemEntryKind.File);
|
|
|
|
}
|
|
|
|
|
|
|
|
function directoryExists(path: string): boolean {
|
|
|
|
return fileSystemEntryExists(path, FileSystemEntryKind.Directory);
|
|
|
|
}
|
2016-05-26 19:17:43 +02:00
|
|
|
|
2016-05-18 00:41:31 +02:00
|
|
|
function getDirectories(path: string): string[] {
|
2016-05-24 01:20:13 +02:00
|
|
|
return filter<string>(_fs.readdirSync(path), p => fileSystemEntryExists(combinePaths(path, p), FileSystemEntryKind.Directory));
|
2016-05-26 19:17:43 +02:00
|
|
|
}
|
2015-12-03 19:44:24 +01:00
|
|
|
|
2016-07-11 20:30:22 +02:00
|
|
|
const nodeSystem: System = {
|
2014-12-06 01:33:39 +01:00
|
|
|
args: process.argv.slice(2),
|
|
|
|
newLine: _os.EOL,
|
|
|
|
useCaseSensitiveFileNames: useCaseSensitiveFileNames,
|
2015-10-06 08:37:50 +02:00
|
|
|
write(s: string): void {
|
2015-07-24 21:18:18 +02:00
|
|
|
process.stdout.write(s);
|
2014-12-06 01:33:39 +01:00
|
|
|
},
|
|
|
|
readFile,
|
|
|
|
writeFile,
|
|
|
|
watchFile: (fileName, callback) => {
|
2016-04-22 19:20:41 +02:00
|
|
|
if (useNonPollingWatchers) {
|
|
|
|
const watchedFile = watchedFileSet.addFile(fileName, callback);
|
|
|
|
return {
|
|
|
|
close: () => watchedFileSet.removeFile(watchedFile)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
_fs.watchFile(fileName, { persistent: true, interval: 250 }, fileChanged);
|
|
|
|
return {
|
|
|
|
close: () => _fs.unwatchFile(fileName, fileChanged)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function fileChanged(curr: any, prev: any) {
|
|
|
|
if (+curr.mtime <= +prev.mtime) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(fileName);
|
|
|
|
}
|
2014-12-06 01:33:39 +01:00
|
|
|
},
|
2016-03-23 20:49:34 +01:00
|
|
|
watchDirectory: (directoryName, callback, recursive) => {
|
2015-12-03 19:44:24 +01:00
|
|
|
// Node 4.0 `fs.watch` function supports the "recursive" option on both OSX and Windows
|
2015-10-02 20:49:30 +02:00
|
|
|
// (ref: https://github.com/nodejs/node/pull/2649 and https://github.com/Microsoft/TypeScript/issues/4643)
|
2016-01-11 20:35:46 +01:00
|
|
|
let options: any;
|
|
|
|
if (isNode4OrLater() && (process.platform === "win32" || process.platform === "darwin")) {
|
|
|
|
options = { persistent: true, recursive: !!recursive };
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
options = { persistent: true };
|
|
|
|
}
|
|
|
|
|
2015-10-05 23:07:51 +02:00
|
|
|
return _fs.watch(
|
2016-03-23 20:49:34 +01:00
|
|
|
directoryName,
|
2016-01-11 20:35:46 +01:00
|
|
|
options,
|
2015-10-05 23:07:51 +02:00
|
|
|
(eventName: string, relativeFileName: string) => {
|
|
|
|
// In watchDirectory we only care about adding and removing files (when event name is
|
|
|
|
// "rename"); changes made within files are handled by corresponding fileWatchers (when
|
|
|
|
// event name is "change")
|
2015-10-15 00:10:05 +02:00
|
|
|
if (eventName === "rename") {
|
2015-10-05 23:07:51 +02:00
|
|
|
// When deleting a file, the passed baseFileName is null
|
2016-03-23 20:49:34 +01:00
|
|
|
callback(!relativeFileName ? relativeFileName : normalizePath(combinePaths(directoryName, relativeFileName)));
|
2015-10-05 23:07:51 +02:00
|
|
|
};
|
2014-12-06 01:33:39 +01:00
|
|
|
}
|
2015-10-05 23:07:51 +02:00
|
|
|
);
|
2014-12-06 01:33:39 +01:00
|
|
|
},
|
2016-03-12 00:56:36 +01:00
|
|
|
resolvePath: function(path: string): string {
|
2014-12-06 01:33:39 +01:00
|
|
|
return _path.resolve(path);
|
|
|
|
},
|
2016-02-12 01:59:54 +01:00
|
|
|
fileExists,
|
|
|
|
directoryExists,
|
2016-07-11 20:30:22 +02:00
|
|
|
createDirectory(directoryName: string) {
|
|
|
|
if (!nodeSystem.directoryExists(directoryName)) {
|
2014-12-06 01:33:39 +01:00
|
|
|
_fs.mkdirSync(directoryName);
|
2014-08-01 06:58:05 +02:00
|
|
|
}
|
2014-12-06 01:33:39 +01:00
|
|
|
},
|
|
|
|
getExecutingFilePath() {
|
|
|
|
return __filename;
|
|
|
|
},
|
|
|
|
getCurrentDirectory() {
|
|
|
|
return process.cwd();
|
|
|
|
},
|
2016-05-18 00:41:31 +02:00
|
|
|
getDirectories,
|
2015-01-15 22:22:23 +01:00
|
|
|
readDirectory,
|
2016-02-09 15:23:43 +01:00
|
|
|
getModifiedTime(path) {
|
2016-02-10 01:47:52 +01:00
|
|
|
try {
|
|
|
|
return _fs.statSync(path).mtime;
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2016-02-09 15:23:43 +01:00
|
|
|
},
|
|
|
|
createHash(data) {
|
2016-02-10 01:50:22 +01:00
|
|
|
const hash = _crypto.createHash("md5");
|
2016-02-09 15:23:43 +01:00
|
|
|
hash.update(data);
|
|
|
|
return hash.digest("hex");
|
|
|
|
},
|
2014-12-06 01:33:39 +01:00
|
|
|
getMemoryUsage() {
|
|
|
|
if (global.gc) {
|
|
|
|
global.gc();
|
|
|
|
}
|
|
|
|
return process.memoryUsage().heapUsed;
|
|
|
|
},
|
2016-03-17 23:55:53 +01:00
|
|
|
getFileSize(path) {
|
|
|
|
try {
|
|
|
|
const stat = _fs.statSync(path);
|
|
|
|
if (stat.isFile()) {
|
|
|
|
return stat.size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (e) { }
|
|
|
|
return 0;
|
|
|
|
},
|
2014-12-06 01:33:39 +01:00
|
|
|
exit(exitCode?: number): void {
|
|
|
|
process.exit(exitCode);
|
2016-05-05 22:38:09 +02:00
|
|
|
},
|
|
|
|
realpath(path: string): string {
|
|
|
|
return _fs.realpathSync(path);
|
2014-08-14 19:52:24 +02:00
|
|
|
}
|
2014-12-06 01:33:39 +01:00
|
|
|
};
|
2016-07-11 20:30:22 +02:00
|
|
|
return nodeSystem;
|
2014-12-06 01:33:39 +01:00
|
|
|
}
|
2015-12-02 02:44:43 +01:00
|
|
|
|
2015-12-02 01:18:06 +01:00
|
|
|
function getChakraSystem(): System {
|
2016-05-05 22:38:09 +02:00
|
|
|
const realpath = ChakraHost.realpath && ((path: string) => ChakraHost.realpath(path));
|
2015-12-02 01:18:06 +01:00
|
|
|
return {
|
2015-12-12 01:19:08 +01:00
|
|
|
newLine: ChakraHost.newLine || "\r\n",
|
2015-12-02 01:18:06 +01:00
|
|
|
args: ChakraHost.args,
|
2015-12-12 01:19:08 +01:00
|
|
|
useCaseSensitiveFileNames: !!ChakraHost.useCaseSensitiveFileNames,
|
2015-12-02 20:49:54 +01:00
|
|
|
write: ChakraHost.echo,
|
2015-12-02 01:18:06 +01:00
|
|
|
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);
|
|
|
|
},
|
2015-12-02 20:49:54 +01:00
|
|
|
resolvePath: ChakraHost.resolvePath,
|
|
|
|
fileExists: ChakraHost.fileExists,
|
|
|
|
directoryExists: ChakraHost.directoryExists,
|
|
|
|
createDirectory: ChakraHost.createDirectory,
|
|
|
|
getExecutingFilePath: () => ChakraHost.executingFile,
|
|
|
|
getCurrentDirectory: () => ChakraHost.currentDirectory,
|
2016-05-18 00:41:31 +02:00
|
|
|
getDirectories: ChakraHost.getDirectories,
|
2016-05-31 19:11:04 +02:00
|
|
|
readDirectory: (path: string, extensions?: string[], excludes?: string[], includes?: string[]) => {
|
|
|
|
const pattern = getFileMatcherPatterns(path, extensions, excludes, includes, !!ChakraHost.useCaseSensitiveFileNames, ChakraHost.currentDirectory);
|
|
|
|
return ChakraHost.readDirectory(path, extensions, pattern.basePaths, pattern.excludePattern, pattern.includeFilePattern, pattern.includeDirectoryPattern);
|
|
|
|
},
|
2015-12-02 20:49:54 +01:00
|
|
|
exit: ChakraHost.quit,
|
2016-05-05 22:38:09 +02:00
|
|
|
realpath
|
2015-12-02 01:18:06 +01:00
|
|
|
};
|
|
|
|
}
|
2015-12-02 02:44:43 +01:00
|
|
|
|
2016-04-10 13:07:47 +02:00
|
|
|
if (typeof ChakraHost !== "undefined") {
|
|
|
|
return getChakraSystem();
|
|
|
|
}
|
|
|
|
else if (typeof WScript !== "undefined" && typeof ActiveXObject === "function") {
|
2014-12-06 01:33:39 +01:00
|
|
|
return getWScriptSystem();
|
|
|
|
}
|
2015-08-15 05:53:38 +02:00
|
|
|
else if (typeof process !== "undefined" && process.nextTick && !process.browser && typeof require !== "undefined") {
|
2015-08-13 22:14:38 +02:00
|
|
|
// process and process.nextTick checks if current environment is node-like
|
|
|
|
// process.browser check excludes webpack and browserify
|
2014-12-06 01:33:39 +01:00
|
|
|
return getNodeSystem();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return undefined; // Unsupported host
|
|
|
|
}
|
|
|
|
})();
|
2016-01-05 19:11:44 +01:00
|
|
|
}
|