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 {
|
2015-12-23 00:26:21 +01:00
|
|
|
export type FileWatcherCallback = (path: string, removed?: boolean) => void;
|
2016-01-08 07:48:17 +01:00
|
|
|
export type DirectoryWatcherCallback = (path: string) => void;
|
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;
|
|
|
|
writeFile(path: string, data: string, writeByteOrderMark?: boolean): void;
|
2016-01-14 09:34:43 +01:00
|
|
|
watchFile?(path: Path, 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;
|
2015-05-16 20:38:28 +02:00
|
|
|
readDirectory(path: string, extension?: string, exclude?: string[]): string[];
|
2015-04-09 23:18:32 +02:00
|
|
|
getMemoryUsage?(): number;
|
2014-12-06 01:33:39 +01:00
|
|
|
exit(exitCode?: number): void;
|
|
|
|
}
|
|
|
|
|
2015-10-02 00:40:13 +02:00
|
|
|
interface WatchedFile {
|
2016-01-13 22:18:28 +01:00
|
|
|
filePath: Path;
|
2015-12-23 00:26:21 +01:00
|
|
|
callback: FileWatcherCallback;
|
2015-12-10 01:21:04 +01:00
|
|
|
mtime?: Date;
|
2015-10-02 00:40:13 +02:00
|
|
|
}
|
|
|
|
|
2014-12-06 01:33:39 +01:00
|
|
|
export interface FileWatcher {
|
|
|
|
close(): void;
|
|
|
|
}
|
2015-12-23 00:38:52 +01:00
|
|
|
|
2016-01-08 07:48:17 +01:00
|
|
|
export interface DirectoryWatcher extends FileWatcher {
|
2016-01-11 20:35:46 +01:00
|
|
|
directoryPath: Path;
|
2015-12-23 00:26:21 +01:00
|
|
|
referenceCount: number;
|
|
|
|
}
|
2014-12-06 01:33:39 +01:00
|
|
|
|
2016-02-06 09:51:25 +01:00
|
|
|
interface OutputFingerprint {
|
|
|
|
hash: string;
|
|
|
|
mtime: Date;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface OutputFingerprintMap {
|
|
|
|
[fileName: string]: OutputFingerprint;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
readDirectory(path: string, extension?: string, exclude?: 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;
|
2015-12-02 02:44:43 +01:00
|
|
|
};
|
2015-12-02 01:18:06 +01:00
|
|
|
|
2014-12-06 01:33:39 +01:00
|
|
|
export var sys: System = (function () {
|
|
|
|
|
|
|
|
function getWScriptSystem(): System {
|
|
|
|
|
2015-11-04 23:02:33 +01:00
|
|
|
const fso = new ActiveXObject("Scripting.FileSystemObject");
|
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-05-16 20:38:28 +02:00
|
|
|
function getCanonicalPath(path: string): string {
|
|
|
|
return path.toLowerCase();
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2015-05-16 20:38:28 +02:00
|
|
|
function readDirectory(path: string, extension?: string, exclude?: string[]): string[] {
|
2015-11-04 23:02:33 +01:00
|
|
|
const result: string[] = [];
|
2015-05-16 20:38:28 +02:00
|
|
|
exclude = map(exclude, s => getCanonicalPath(combinePaths(path, s)));
|
2015-01-15 22:22:23 +01:00
|
|
|
visitDirectory(path);
|
|
|
|
return result;
|
|
|
|
function visitDirectory(path: string) {
|
2015-11-04 23:02:33 +01:00
|
|
|
const folder = fso.GetFolder(path || ".");
|
|
|
|
const files = getNames(folder.files);
|
|
|
|
for (const current of files) {
|
|
|
|
const name = combinePaths(path, current);
|
2015-05-16 20:38:28 +02:00
|
|
|
if ((!extension || fileExtensionIs(name, extension)) && !contains(exclude, getCanonicalPath(name))) {
|
|
|
|
result.push(name);
|
2015-01-15 22:22:23 +01:00
|
|
|
}
|
|
|
|
}
|
2015-11-04 23:02:33 +01:00
|
|
|
const subfolders = getNames(folder.subfolders);
|
|
|
|
for (const current of subfolders) {
|
|
|
|
const name = combinePaths(path, current);
|
2015-05-16 20:38:28 +02:00
|
|
|
if (!contains(exclude, getCanonicalPath(name))) {
|
|
|
|
visitDirectory(name);
|
|
|
|
}
|
2015-01-15 22:22:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-06 01:33:39 +01:00
|
|
|
return {
|
|
|
|
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);
|
|
|
|
},
|
|
|
|
createDirectory(directoryName: string) {
|
|
|
|
if (!this.directoryExists(directoryName)) {
|
|
|
|
fso.CreateFolder(directoryName);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
getExecutingFilePath() {
|
|
|
|
return WScript.ScriptFullName;
|
|
|
|
},
|
|
|
|
getCurrentDirectory() {
|
|
|
|
return new ActiveXObject("WScript.Shell").CurrentDirectory;
|
|
|
|
},
|
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
|
|
|
};
|
|
|
|
}
|
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
|
|
|
|
2015-10-15 00:10:05 +02:00
|
|
|
// average async stat takes about 30 microseconds
|
|
|
|
// set chunk size to do 30 files in < 1 millisecond
|
2015-12-10 01:21:04 +01:00
|
|
|
function createPollingWatchedFileSet(interval = 2500, chunkSize = 30) {
|
2015-10-15 00:10:05 +02:00
|
|
|
let watchedFiles: WatchedFile[] = [];
|
|
|
|
let nextFileToCheck = 0;
|
|
|
|
let watchTimer: any;
|
|
|
|
|
|
|
|
function getModifiedTime(fileName: string): Date {
|
2015-10-02 00:40:13 +02:00
|
|
|
return _fs.statSync(fileName).mtime;
|
|
|
|
}
|
|
|
|
|
2015-10-15 00:10:05 +02:00
|
|
|
function poll(checkedIndex: number) {
|
2015-11-04 23:02:33 +01:00
|
|
|
const watchedFile = watchedFiles[checkedIndex];
|
2015-10-02 00:40:13 +02:00
|
|
|
if (!watchedFile) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-13 22:18:28 +01:00
|
|
|
_fs.stat(watchedFile.filePath, (err: any, stats: any) => {
|
2015-10-02 00:40:13 +02:00
|
|
|
if (err) {
|
2016-01-13 22:18:28 +01:00
|
|
|
watchedFile.callback(watchedFile.filePath);
|
2015-10-02 00:40:13 +02:00
|
|
|
}
|
|
|
|
else if (watchedFile.mtime.getTime() !== stats.mtime.getTime()) {
|
2016-01-13 22:18:28 +01:00
|
|
|
watchedFile.mtime = getModifiedTime(watchedFile.filePath);
|
|
|
|
watchedFile.callback(watchedFile.filePath, watchedFile.mtime.getTime() === 0);
|
2015-10-02 00:40:13 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// this implementation uses polling and
|
|
|
|
// stat due to inconsistencies of fs.watch
|
|
|
|
// and efficiency of stat on modern filesystems
|
2015-10-15 00:10:05 +02:00
|
|
|
function startWatchTimer() {
|
|
|
|
watchTimer = setInterval(() => {
|
2015-10-06 21:33:06 +02:00
|
|
|
let count = 0;
|
2015-10-15 00:10:05 +02:00
|
|
|
let nextToCheck = nextFileToCheck;
|
2015-10-06 21:33:06 +02:00
|
|
|
let firstCheck = -1;
|
2015-10-15 00:10:05 +02:00
|
|
|
while ((count < chunkSize) && (nextToCheck !== firstCheck)) {
|
|
|
|
poll(nextToCheck);
|
2015-10-02 00:40:13 +02:00
|
|
|
if (firstCheck < 0) {
|
|
|
|
firstCheck = nextToCheck;
|
|
|
|
}
|
|
|
|
nextToCheck++;
|
2015-10-15 00:10:05 +02:00
|
|
|
if (nextToCheck === watchedFiles.length) {
|
2015-10-02 00:40:13 +02:00
|
|
|
nextToCheck = 0;
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
}
|
2015-10-15 00:10:05 +02:00
|
|
|
nextFileToCheck = nextToCheck;
|
|
|
|
}, interval);
|
2015-10-02 00:40:13 +02:00
|
|
|
}
|
|
|
|
|
2016-01-13 22:18:28 +01:00
|
|
|
function addFile(filePath: Path, callback: FileWatcherCallback): WatchedFile {
|
2015-11-04 23:02:33 +01:00
|
|
|
const file: WatchedFile = {
|
2016-01-13 22:18:28 +01:00
|
|
|
filePath,
|
2015-10-02 00:40:13 +02:00
|
|
|
callback,
|
2016-01-13 22:18:28 +01:00
|
|
|
mtime: getModifiedTime(filePath)
|
2015-10-02 00:40:13 +02:00
|
|
|
};
|
|
|
|
|
2015-10-15 00:10:05 +02:00
|
|
|
watchedFiles.push(file);
|
|
|
|
if (watchedFiles.length === 1) {
|
|
|
|
startWatchTimer();
|
2015-10-02 00:40:13 +02:00
|
|
|
}
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2015-10-15 00:10:05 +02:00
|
|
|
function removeFile(file: WatchedFile) {
|
|
|
|
watchedFiles = copyListRemovingItem(file, watchedFiles);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
getModifiedTime: getModifiedTime,
|
|
|
|
poll: poll,
|
|
|
|
startWatchTimer: startWatchTimer,
|
|
|
|
addFile: addFile,
|
|
|
|
removeFile: removeFile
|
2015-10-15 01:57:08 +02:00
|
|
|
};
|
2015-10-02 00:40:13 +02:00
|
|
|
}
|
2015-12-11 02:59:07 +01:00
|
|
|
|
2015-12-10 01:21:04 +01:00
|
|
|
function createWatchedFileSet() {
|
2016-01-08 07:48:17 +01:00
|
|
|
const dirWatchers = createFileMap<DirectoryWatcher>();
|
2015-12-28 23:05:32 +01:00
|
|
|
// One file can have multiple watchers
|
|
|
|
const fileWatcherCallbacks = createFileMap<FileWatcherCallback[]>();
|
2016-01-11 20:35:46 +01:00
|
|
|
return { addFile, removeFile };
|
2015-12-23 00:26:21 +01:00
|
|
|
|
2016-01-13 23:02:34 +01:00
|
|
|
function reduceDirWatcherRefCountForFile(filePath: Path) {
|
|
|
|
const dirPath = getDirectoryPath(filePath);
|
|
|
|
if (dirWatchers.contains(dirPath)) {
|
|
|
|
const watcher = dirWatchers.get(dirPath);
|
|
|
|
watcher.referenceCount -= 1;
|
|
|
|
if (watcher.referenceCount <= 0) {
|
|
|
|
watcher.close();
|
|
|
|
dirWatchers.remove(dirPath);
|
|
|
|
}
|
2015-12-23 00:26:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-11 20:35:46 +01:00
|
|
|
function addDirWatcher(dirPath: Path): void {
|
|
|
|
if (dirWatchers.contains(dirPath)) {
|
|
|
|
const watcher = dirWatchers.get(dirPath);
|
|
|
|
watcher.referenceCount += 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const watcher: DirectoryWatcher = _fs.watch(
|
2016-01-12 09:17:38 +01:00
|
|
|
dirPath,
|
|
|
|
{ persistent: true },
|
2016-01-11 20:35:46 +01:00
|
|
|
(eventName: string, relativeFileName: string) => fileEventHandler(eventName, relativeFileName, dirPath)
|
|
|
|
);
|
|
|
|
watcher.referenceCount = 1;
|
|
|
|
dirWatchers.set(dirPath, watcher);
|
|
|
|
return;
|
|
|
|
}
|
2015-12-23 00:26:21 +01:00
|
|
|
|
2016-01-11 20:35:46 +01:00
|
|
|
function addFileWatcherCallback(filePath: Path, callback: FileWatcherCallback): void {
|
|
|
|
if (fileWatcherCallbacks.contains(filePath)) {
|
|
|
|
fileWatcherCallbacks.get(filePath).push(callback);
|
2015-12-23 00:38:52 +01:00
|
|
|
}
|
|
|
|
else {
|
2016-01-11 20:35:46 +01:00
|
|
|
fileWatcherCallbacks.set(filePath, [callback]);
|
2015-12-23 00:26:21 +01:00
|
|
|
}
|
|
|
|
}
|
2015-12-11 02:59:07 +01:00
|
|
|
|
2016-01-13 22:18:28 +01:00
|
|
|
function addFile(filePath: Path, callback: FileWatcherCallback): WatchedFile {
|
2016-01-11 20:35:46 +01:00
|
|
|
addFileWatcherCallback(filePath, callback);
|
|
|
|
addDirWatcher(getDirectoryPath(filePath));
|
2015-12-28 23:05:32 +01:00
|
|
|
|
2016-01-13 22:18:28 +01:00
|
|
|
return { filePath, callback };
|
2015-12-10 01:21:04 +01:00
|
|
|
}
|
2015-12-11 02:59:07 +01:00
|
|
|
|
2016-01-11 20:35:46 +01:00
|
|
|
function removeFile(watchedFile: WatchedFile) {
|
2016-01-13 23:02:34 +01:00
|
|
|
removeFileWatcherCallback(watchedFile.filePath, watchedFile.callback);
|
|
|
|
reduceDirWatcherRefCountForFile(watchedFile.filePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeFileWatcherCallback(filePath: Path, callback: FileWatcherCallback) {
|
2015-12-28 23:05:32 +01:00
|
|
|
if (fileWatcherCallbacks.contains(filePath)) {
|
2016-01-13 23:02:34 +01:00
|
|
|
const newCallbacks = copyListRemovingItem(callback, fileWatcherCallbacks.get(filePath));
|
2015-12-28 23:05:32 +01:00
|
|
|
if (newCallbacks.length === 0) {
|
|
|
|
fileWatcherCallbacks.remove(filePath);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fileWatcherCallbacks.set(filePath, newCallbacks);
|
|
|
|
}
|
2015-12-15 17:39:51 +01:00
|
|
|
}
|
2015-12-10 01:21:04 +01:00
|
|
|
}
|
2015-12-11 02:59:07 +01:00
|
|
|
|
2016-01-16 01:55:25 +01:00
|
|
|
function fileEventHandler(eventName: string, relativeFileName: string, baseDirPath: Path) {
|
2015-12-23 00:26:21 +01:00
|
|
|
// When files are deleted from disk, the triggered "rename" event would have a relativefileName of "undefined"
|
2016-01-16 01:55:25 +01:00
|
|
|
const filePath = typeof relativeFileName !== "string"
|
2016-01-15 23:58:04 +01:00
|
|
|
? undefined
|
2016-01-16 01:55:25 +01:00
|
|
|
: toPath(relativeFileName, baseDirPath, createGetCanonicalFileName(sys.useCaseSensitiveFileNames));
|
2016-01-11 20:35:46 +01:00
|
|
|
if (eventName === "change" && fileWatcherCallbacks.contains(filePath)) {
|
2015-12-28 23:05:32 +01:00
|
|
|
for (const fileCallback of fileWatcherCallbacks.get(filePath)) {
|
|
|
|
fileCallback(filePath);
|
|
|
|
}
|
2015-12-10 01:21:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-02 00:40:13 +02:00
|
|
|
|
|
|
|
// REVIEW: for now this implementation uses polling.
|
|
|
|
// The advantage of polling is that it works reliably
|
|
|
|
// on all os and with network mounted files.
|
2015-12-02 01:18:06 +01:00
|
|
|
// For 90 referenced files, the average time to detect
|
2015-10-02 00:40:13 +02:00
|
|
|
// 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
|
|
|
|
// pause less than 1.5 milliseconds); question is
|
|
|
|
// do we anticipate reference sets in the 100s and
|
|
|
|
// do we care about waiting 10-20 seconds to detect
|
|
|
|
// changes for large reference sets? If so, do we want
|
|
|
|
// to increase the chunk size or decrease the interval
|
|
|
|
// time dynamically to match the large reference set?
|
2015-12-10 01:21:04 +01:00
|
|
|
const pollingWatchedFileSet = createPollingWatchedFileSet();
|
2015-12-15 17:39:51 +01:00
|
|
|
const watchedFileSet = createWatchedFileSet();
|
2015-10-02 00:25:43 +02:00
|
|
|
|
2015-12-23 01:00:49 +01:00
|
|
|
function isNode4OrLater(): boolean {
|
|
|
|
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 {
|
|
|
|
if (!_fs.existsSync(fileName)) {
|
|
|
|
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
|
|
|
|
2016-02-06 09:51:25 +01:00
|
|
|
const outputFingerprintMap: OutputFingerprintMap = {};
|
|
|
|
|
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
|
|
|
|
2016-02-06 09:51:25 +01:00
|
|
|
const md5 = getMd5(data);
|
|
|
|
const mtimeBefore = _fs.existsSync(fileName) && _fs.statSync(fileName).mtime;
|
|
|
|
|
|
|
|
if (mtimeBefore && outputFingerprintMap.hasOwnProperty(fileName)) {
|
|
|
|
const fingerprint = outputFingerprintMap[fileName];
|
|
|
|
|
|
|
|
// If output has not been changed, and the file has no external modification
|
|
|
|
if (fingerprint.hash === md5 && fingerprint.mtime.getTime() === mtimeBefore.getTime()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2016-02-06 09:51:25 +01:00
|
|
|
|
|
|
|
const mtimeAfter = _fs.statSync(fileName).mtime;
|
|
|
|
|
|
|
|
outputFingerprintMap[fileName] = {
|
|
|
|
hash: md5,
|
|
|
|
mtime: mtimeAfter
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function getMd5(data: string): string {
|
|
|
|
const hash = _crypto.createHash("md5");
|
|
|
|
hash.update(data);
|
|
|
|
return hash.digest("hex");
|
2014-12-06 01:33:39 +01:00
|
|
|
}
|
2014-07-13 01:04:16 +02:00
|
|
|
|
2015-05-16 20:38:28 +02:00
|
|
|
function getCanonicalPath(path: string): string {
|
2016-01-15 23:58:04 +01:00
|
|
|
return useCaseSensitiveFileNames ? path : path.toLowerCase();
|
2015-05-16 20:38:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function readDirectory(path: string, extension?: string, exclude?: string[]): string[] {
|
2015-11-04 23:02:33 +01:00
|
|
|
const result: string[] = [];
|
2015-05-16 20:38:28 +02:00
|
|
|
exclude = map(exclude, s => getCanonicalPath(combinePaths(path, s)));
|
2015-01-15 22:22:23 +01:00
|
|
|
visitDirectory(path);
|
|
|
|
return result;
|
|
|
|
function visitDirectory(path: string) {
|
2015-11-04 23:02:33 +01:00
|
|
|
const files = _fs.readdirSync(path || ".").sort();
|
|
|
|
const directories: string[] = [];
|
|
|
|
for (const current of files) {
|
|
|
|
const name = combinePaths(path, current);
|
2015-05-16 20:38:28 +02:00
|
|
|
if (!contains(exclude, getCanonicalPath(name))) {
|
2015-11-04 23:02:33 +01:00
|
|
|
const stat = _fs.statSync(name);
|
2015-05-16 20:38:28 +02:00
|
|
|
if (stat.isFile()) {
|
|
|
|
if (!extension || fileExtensionIs(name, extension)) {
|
|
|
|
result.push(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (stat.isDirectory()) {
|
|
|
|
directories.push(name);
|
2015-01-15 22:22:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-04 23:02:33 +01:00
|
|
|
for (const current of directories) {
|
2015-03-13 18:36:29 +01:00
|
|
|
visitDirectory(current);
|
2015-01-15 22:22:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-06 01:33:39 +01:00
|
|
|
return {
|
|
|
|
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,
|
2016-01-14 09:34:43 +01:00
|
|
|
watchFile: (filePath, callback) => {
|
2015-10-02 00:40:13 +02:00
|
|
|
// Node 4.0 stablized the `fs.watch` function on Windows which avoids polling
|
2015-10-02 00:25:43 +02:00
|
|
|
// and is more efficient than `fs.watchFile` (ref: https://github.com/nodejs/node/pull/2649
|
|
|
|
// and https://github.com/Microsoft/TypeScript/issues/4643), therefore
|
|
|
|
// if the current node.js version is newer than 4, use `fs.watch` instead.
|
2015-12-15 17:39:51 +01:00
|
|
|
const watchSet = isNode4OrLater() ? watchedFileSet : pollingWatchedFileSet;
|
2016-01-14 09:34:43 +01:00
|
|
|
const watchedFile = watchSet.addFile(filePath, callback);
|
2014-12-06 01:33:39 +01:00
|
|
|
return {
|
2015-12-15 17:39:51 +01:00
|
|
|
close: () => watchSet.removeFile(watchedFile)
|
2014-12-06 01:33:39 +01:00
|
|
|
};
|
|
|
|
},
|
2015-10-02 20:49:30 +02:00
|
|
|
watchDirectory: (path, callback, recursive) => {
|
2016-01-11 20:35:46 +01:00
|
|
|
// 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)
|
|
|
|
let options: any;
|
|
|
|
if (isNode4OrLater() && (process.platform === "win32" || process.platform === "darwin")) {
|
|
|
|
options = { persistent: true, recursive: !!recursive };
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
options = { persistent: true };
|
|
|
|
}
|
|
|
|
|
|
|
|
return _fs.watch(
|
|
|
|
path,
|
|
|
|
options,
|
|
|
|
(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")
|
|
|
|
if (eventName === "rename") {
|
|
|
|
// When deleting a file, the passed baseFileName is null
|
|
|
|
callback(!relativeFileName ? relativeFileName : normalizePath(combinePaths(path, relativeFileName)));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
2014-12-06 01:33:39 +01: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);
|
2014-08-01 06:58:05 +02:00
|
|
|
}
|
2014-12-06 01:33:39 +01:00
|
|
|
},
|
|
|
|
getExecutingFilePath() {
|
|
|
|
return __filename;
|
|
|
|
},
|
|
|
|
getCurrentDirectory() {
|
|
|
|
return process.cwd();
|
|
|
|
},
|
2015-01-15 22:22:23 +01:00
|
|
|
readDirectory,
|
2014-12-06 01:33:39 +01:00
|
|
|
getMemoryUsage() {
|
|
|
|
if (global.gc) {
|
|
|
|
global.gc();
|
|
|
|
}
|
|
|
|
return process.memoryUsage().heapUsed;
|
|
|
|
},
|
|
|
|
exit(exitCode?: number): void {
|
|
|
|
process.exit(exitCode);
|
2014-08-14 19:52:24 +02:00
|
|
|
}
|
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 {
|
|
|
|
|
|
|
|
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,
|
|
|
|
readDirectory: ChakraHost.readDirectory,
|
|
|
|
exit: ChakraHost.quit,
|
2015-12-02 01:18:06 +01:00
|
|
|
};
|
|
|
|
}
|
2015-12-02 02:44:43 +01:00
|
|
|
|
2014-12-06 01:33:39 +01:00
|
|
|
if (typeof WScript !== "undefined" && typeof ActiveXObject === "function") {
|
|
|
|
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();
|
|
|
|
}
|
2015-12-02 01:18:06 +01:00
|
|
|
else if (typeof ChakraHost !== "undefined") {
|
|
|
|
return getChakraSystem();
|
|
|
|
}
|
2014-12-06 01:33:39 +01:00
|
|
|
else {
|
|
|
|
return undefined; // Unsupported host
|
|
|
|
}
|
|
|
|
})();
|
2015-07-09 02:42:26 +02:00
|
|
|
}
|