2017-11-13 01:23:08 +01:00
|
|
|
/// <reference path="./core.ts" />
|
2018-02-06 07:27:55 +01:00
|
|
|
/// <reference path="./assert.ts" />
|
2017-11-22 04:47:13 +01:00
|
|
|
/// <reference path="./utils.ts" />
|
|
|
|
/// <reference path="./vfs.ts" />
|
2017-11-13 01:23:08 +01:00
|
|
|
|
2017-11-23 03:59:59 +01:00
|
|
|
// NOTE: The contents of this file are all exported from the namespace 'fakes'. This is to
|
2017-11-13 01:23:08 +01:00
|
|
|
// support the eventual conversion of harness into a modular system.
|
|
|
|
|
2017-11-23 03:59:59 +01:00
|
|
|
// harness fakes
|
|
|
|
namespace fakes {
|
2018-02-06 07:27:55 +01:00
|
|
|
const processExitSentinel = new Error("System exit");
|
|
|
|
|
|
|
|
export interface ServerHostOptions {
|
2017-11-13 01:23:08 +01:00
|
|
|
/**
|
2017-11-22 04:47:13 +01:00
|
|
|
* The virtual path to tsc.js. If not specified, a default of `"/.ts/tsc.js"` is used.
|
2017-11-13 01:23:08 +01:00
|
|
|
*/
|
2017-11-22 04:47:13 +01:00
|
|
|
executingFilePath?: string;
|
|
|
|
newLine?: "\r\n" | "\n";
|
|
|
|
safeList?: boolean;
|
|
|
|
lib?: boolean;
|
2017-11-28 00:00:05 +01:00
|
|
|
dos?: boolean;
|
2017-11-13 01:23:08 +01:00
|
|
|
}
|
|
|
|
|
2018-02-06 07:27:55 +01:00
|
|
|
export class ServerHost implements ts.server.ServerHost, ts.FormatDiagnosticsHost {
|
2018-01-20 06:58:46 +01:00
|
|
|
public readonly vfs: vfs.FileSystem;
|
2017-11-13 01:23:08 +01:00
|
|
|
public exitCode: number;
|
|
|
|
private readonly _output: string[] = [];
|
|
|
|
private readonly _executingFilePath: string;
|
|
|
|
private readonly _getCanonicalFileName: (file: string) => string;
|
2018-02-06 07:27:55 +01:00
|
|
|
|
|
|
|
constructor(vfs: vfs.FileSystem, options: ServerHostOptions = {}) {
|
2017-11-22 04:47:13 +01:00
|
|
|
const {
|
2017-11-28 00:00:05 +01:00
|
|
|
dos = false,
|
|
|
|
executingFilePath = dos
|
2018-02-06 07:27:55 +01:00
|
|
|
? vfsutils.dosTscPath
|
|
|
|
: vfsutils.tscPath,
|
2017-11-22 04:47:13 +01:00
|
|
|
newLine = "\n",
|
|
|
|
safeList = false,
|
2018-01-20 06:58:46 +01:00
|
|
|
lib = false,
|
2017-11-22 04:47:13 +01:00
|
|
|
} = options;
|
|
|
|
|
2018-02-06 07:27:55 +01:00
|
|
|
this.vfs = vfs.isReadonly ? vfs.shadow() : vfs;
|
2018-01-20 06:58:46 +01:00
|
|
|
this.useCaseSensitiveFileNames = !this.vfs.ignoreCase;
|
2017-11-13 01:23:08 +01:00
|
|
|
this.newLine = newLine;
|
|
|
|
this._executingFilePath = executingFilePath;
|
|
|
|
this._getCanonicalFileName = ts.createGetCanonicalFileName(this.useCaseSensitiveFileNames);
|
2017-11-22 04:47:13 +01:00
|
|
|
|
|
|
|
if (safeList) {
|
2018-02-06 07:27:55 +01:00
|
|
|
const safelistPath = dos ? vfsutils.dosSafelistPath : vfsutils.safelistPath;
|
|
|
|
this.vfs.mkdirpSync(vpath.dirname(safelistPath));
|
|
|
|
this.vfs.writeFileSync(safelistPath, vfsutils.safelistContent);
|
2017-11-22 04:47:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (lib) {
|
2018-02-06 07:27:55 +01:00
|
|
|
const libPath = dos ? vfsutils.dosLibPath : vfsutils.libPath;
|
2018-01-20 06:58:46 +01:00
|
|
|
this.vfs.mkdirpSync(vpath.dirname(libPath));
|
2018-02-06 07:27:55 +01:00
|
|
|
this.vfs.writeFileSync(libPath, vfsutils.emptyLibContent);
|
2017-11-22 04:47:13 +01:00
|
|
|
}
|
2017-11-13 01:23:08 +01:00
|
|
|
}
|
|
|
|
|
2018-02-06 07:27:55 +01:00
|
|
|
// #region System members
|
2017-11-13 01:23:08 +01:00
|
|
|
public readonly newLine: string;
|
|
|
|
public readonly useCaseSensitiveFileNames: boolean;
|
|
|
|
|
|
|
|
public write(message: string) {
|
|
|
|
this._output.push(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
public readFile(path: string) {
|
2018-01-20 06:58:46 +01:00
|
|
|
return vfsutils.readFile(this.vfs, path);
|
2017-11-13 01:23:08 +01:00
|
|
|
}
|
|
|
|
|
2017-11-22 04:47:13 +01:00
|
|
|
public writeFile(path: string, data: string, writeByteOrderMark?: boolean): void {
|
2018-01-20 06:58:46 +01:00
|
|
|
vfsutils.writeFile(this.vfs, path, data, writeByteOrderMark);
|
2017-11-13 01:23:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public fileExists(path: string) {
|
2018-01-20 06:58:46 +01:00
|
|
|
return vfsutils.fileExists(this.vfs, path);
|
2017-11-13 01:23:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public directoryExists(path: string) {
|
2018-01-20 06:58:46 +01:00
|
|
|
return vfsutils.directoryExists(this.vfs, path);
|
2017-11-13 01:23:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public createDirectory(path: string): void {
|
2018-01-20 06:58:46 +01:00
|
|
|
this.vfs.mkdirpSync(path);
|
2017-11-13 01:23:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public getCurrentDirectory() {
|
2018-01-20 06:58:46 +01:00
|
|
|
return this.vfs.cwd();
|
2017-11-13 01:23:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public getDirectories(path: string) {
|
2018-01-20 06:58:46 +01:00
|
|
|
return vfsutils.getDirectories(this.vfs, path);
|
2017-11-13 01:23:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public readDirectory(path: string, extensions?: ReadonlyArray<string>, exclude?: ReadonlyArray<string>, include?: ReadonlyArray<string>, depth?: number): string[] {
|
2018-01-20 06:58:46 +01:00
|
|
|
return ts.matchFiles(path, extensions, exclude, include, this.useCaseSensitiveFileNames, this.vfs.cwd(), depth, path => {
|
|
|
|
return vfsutils.getAccessibleFileSystemEntries(this.vfs, path);
|
2017-11-13 01:23:08 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public exit(exitCode?: number) {
|
|
|
|
this.exitCode = exitCode;
|
2018-02-06 07:27:55 +01:00
|
|
|
throw processExitSentinel;
|
2017-11-28 00:00:05 +01:00
|
|
|
}
|
|
|
|
|
2017-11-13 01:23:08 +01:00
|
|
|
public readonly args: string[] = [];
|
|
|
|
|
|
|
|
public getFileSize(path: string) {
|
2018-01-20 06:58:46 +01:00
|
|
|
return vfsutils.getFileSize(this.vfs, path);
|
2017-11-13 01:23:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public watchFile(path: string, cb: ts.FileWatcherCallback) {
|
2018-02-06 07:27:55 +01:00
|
|
|
return vfsutils.watchFile(this.vfs, path, cb);
|
2017-11-13 01:23:08 +01:00
|
|
|
}
|
|
|
|
|
2018-01-20 06:58:46 +01:00
|
|
|
public watchDirectory(path: string, cb: ts.DirectoryWatcherCallback, recursive?: boolean): ts.FileWatcher {
|
2018-02-06 07:27:55 +01:00
|
|
|
return vfsutils.watchDirectory(this.vfs, path, cb, recursive);
|
2017-11-13 01:23:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public resolvePath(path: string) {
|
2018-01-20 06:58:46 +01:00
|
|
|
return vpath.resolve(this.vfs.cwd(), path);
|
2017-11-13 01:23:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public getExecutingFilePath() {
|
|
|
|
return this._executingFilePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
public getModifiedTime(path: string) {
|
2018-01-20 06:58:46 +01:00
|
|
|
return vfsutils.getModifiedTime(this.vfs, path);
|
2017-11-13 01:23:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public createHash(data: string): string {
|
|
|
|
return core.sha1(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public realpath(path: string) {
|
2018-01-22 08:25:40 +01:00
|
|
|
try {
|
|
|
|
return this.vfs.realpathSync(path);
|
|
|
|
}
|
|
|
|
catch {
|
|
|
|
return path;
|
|
|
|
}
|
2017-11-13 01:23:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public getEnvironmentVariable(_name: string): string | undefined {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2018-02-06 07:27:55 +01:00
|
|
|
public setTimeout(_callback: (...args: any[]) => void, _timeout: number, ..._args: any[]) {
|
|
|
|
return ts.notImplemented();
|
2017-11-13 01:23:08 +01:00
|
|
|
}
|
2017-12-18 21:12:51 +01:00
|
|
|
|
2018-02-06 07:27:55 +01:00
|
|
|
public clearTimeout(_timeoutId: any): void {
|
|
|
|
return ts.notImplemented();
|
2017-12-18 21:12:51 +01:00
|
|
|
}
|
2017-11-13 01:23:08 +01:00
|
|
|
// #endregion System members
|
|
|
|
|
|
|
|
// #region FormatDiagnosticsHost members
|
|
|
|
public getNewLine() {
|
|
|
|
return this.newLine;
|
|
|
|
}
|
|
|
|
|
|
|
|
public getCanonicalFileName(fileName: string) {
|
|
|
|
return this._getCanonicalFileName(fileName);
|
|
|
|
}
|
|
|
|
// #endregion FormatDiagnosticsHost members
|
|
|
|
|
|
|
|
// #region ServerHost members
|
2018-02-06 07:27:55 +01:00
|
|
|
public setImmediate(_callback: (...args: any[]) => void, ..._args: any[]): any {
|
|
|
|
return ts.notImplemented();
|
2017-11-13 01:23:08 +01:00
|
|
|
}
|
|
|
|
|
2018-02-06 07:27:55 +01:00
|
|
|
public clearImmediate(_timeoutId: any): void {
|
|
|
|
return ts.notImplemented();
|
2017-11-13 01:23:08 +01:00
|
|
|
}
|
|
|
|
// #endregion ServerHost members
|
2017-11-28 02:01:15 +01:00
|
|
|
}
|
2018-02-06 07:27:55 +01:00
|
|
|
}
|
2017-11-28 02:01:15 +01:00
|
|
|
|