TypeScript/src/harness/fakes.ts

182 lines
6.1 KiB
TypeScript
Raw Normal View History

/// <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-23 03:59:59 +01:00
// NOTE: The contents of this file are all exported from the namespace 'fakes'. This is to
// 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-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-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;
}
2018-02-06 07:27:55 +01:00
export class ServerHost implements ts.server.ServerHost, ts.FormatDiagnosticsHost {
public readonly vfs: vfs.FileSystem;
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,
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;
this.useCaseSensitiveFileNames = !this.vfs.ignoreCase;
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;
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
}
}
2018-02-06 07:27:55 +01:00
// #region System members
public readonly newLine: string;
public readonly useCaseSensitiveFileNames: boolean;
public write(message: string) {
this._output.push(message);
}
public readFile(path: string) {
return vfsutils.readFile(this.vfs, path);
}
2017-11-22 04:47:13 +01:00
public writeFile(path: string, data: string, writeByteOrderMark?: boolean): void {
vfsutils.writeFile(this.vfs, path, data, writeByteOrderMark);
}
public fileExists(path: string) {
return vfsutils.fileExists(this.vfs, path);
}
public directoryExists(path: string) {
return vfsutils.directoryExists(this.vfs, path);
}
public createDirectory(path: string): void {
this.vfs.mkdirpSync(path);
}
public getCurrentDirectory() {
return this.vfs.cwd();
}
public getDirectories(path: string) {
return vfsutils.getDirectories(this.vfs, path);
}
public readDirectory(path: string, extensions?: ReadonlyArray<string>, exclude?: ReadonlyArray<string>, include?: ReadonlyArray<string>, depth?: number): string[] {
return ts.matchFiles(path, extensions, exclude, include, this.useCaseSensitiveFileNames, this.vfs.cwd(), depth, path => {
return vfsutils.getAccessibleFileSystemEntries(this.vfs, path);
});
}
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
}
public readonly args: string[] = [];
public getFileSize(path: string) {
return vfsutils.getFileSize(this.vfs, path);
}
public watchFile(path: string, cb: ts.FileWatcherCallback) {
2018-02-06 07:27:55 +01:00
return vfsutils.watchFile(this.vfs, path, cb);
}
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);
}
public resolvePath(path: string) {
return vpath.resolve(this.vfs.cwd(), path);
}
public getExecutingFilePath() {
return this._executingFilePath;
}
public getModifiedTime(path: string) {
return vfsutils.getModifiedTime(this.vfs, path);
}
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;
}
}
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-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
}
// #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();
}
2018-02-06 07:27:55 +01:00
public clearImmediate(_timeoutId: any): void {
return ts.notImplemented();
}
// #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