|
| 1 | +import { Tree } from '@angular-devkit/schematics'; |
| 2 | +import * as ts from 'typescript/lib/tsserverlibrary'; |
| 3 | + |
| 4 | +export class ServerHost implements ts.server.ServerHost { |
| 5 | + readonly args: string[]; |
| 6 | + readonly newLine: string; |
| 7 | + readonly useCaseSensitiveFileNames: boolean; |
| 8 | + |
| 9 | + constructor(private host: Tree) { |
| 10 | + this.args = ts.sys.args; |
| 11 | + this.newLine = ts.sys.newLine; |
| 12 | + this.useCaseSensitiveFileNames = ts.sys.useCaseSensitiveFileNames; |
| 13 | + } |
| 14 | + |
| 15 | + public readFile(path: string, encoding?: string): string | undefined { |
| 16 | + let content; |
| 17 | + try { |
| 18 | + content = this.host.read(path).toString(encoding); |
| 19 | + } finally { |
| 20 | + return content || ts.sys.readFile(path, encoding); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + public getFileSize(path: string): number { |
| 25 | + return ts.sys.getFileSize(path); |
| 26 | + } |
| 27 | + |
| 28 | + public watchFile(path: string, callback: ts.FileWatcherCallback, pollingInterval?: number): |
| 29 | + ts.FileWatcher { |
| 30 | + return ts.sys.watchFile(path, callback, pollingInterval); |
| 31 | + } |
| 32 | + |
| 33 | + public watchDirectory(path: string, callback: ts.DirectoryWatcherCallback, recursive?: boolean): |
| 34 | + ts.FileWatcher { |
| 35 | + return ts.sys.watchDirectory(path, callback, recursive); |
| 36 | + } |
| 37 | + |
| 38 | + public resolvePath(path: string): string { |
| 39 | + return ts.sys.resolvePath(path); |
| 40 | + } |
| 41 | + |
| 42 | + public fileExists(path: string): boolean { |
| 43 | + return this.host.exists(path); |
| 44 | + } |
| 45 | + |
| 46 | + public directoryExists(path: string): boolean { |
| 47 | + let exists: boolean; |
| 48 | + try { |
| 49 | + exists = this.host.getDir(path) !== void 0; |
| 50 | + } finally { |
| 51 | + return exists || this.fileExists(path); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public getExecutingFilePath(): string { |
| 56 | + return ts.sys.getExecutingFilePath(); |
| 57 | + } |
| 58 | + |
| 59 | + public getCurrentDirectory(): string { |
| 60 | + return this.host.root.path; |
| 61 | + } |
| 62 | + |
| 63 | + public getDirectories(path: string): string[] { |
| 64 | + return this.host.getDir(path).subdirs; |
| 65 | + } |
| 66 | + |
| 67 | + public readDirectory(path: string): string[] { |
| 68 | + return this.host.getDir(path).subfiles; |
| 69 | + } |
| 70 | + |
| 71 | + public require(initialPath: string, moduleName: string) { |
| 72 | + try { |
| 73 | + const modulePath = require.resolve(moduleName, { |
| 74 | + paths: [initialPath], |
| 75 | + }); |
| 76 | + return { |
| 77 | + module: require(modulePath), |
| 78 | + error: undefined, |
| 79 | + }; |
| 80 | + } catch (e) { |
| 81 | + return { |
| 82 | + module: undefined, |
| 83 | + error: e as Error, |
| 84 | + }; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + public getModifiedTime(path: string): Date | undefined { |
| 89 | + return ts.sys.getModifiedTime(path); |
| 90 | + } |
| 91 | + |
| 92 | + public realpath(path: string): string { |
| 93 | + return ts.sys.realpath(path); |
| 94 | + } |
| 95 | + |
| 96 | + public createSHA256Hash(data: string): string { |
| 97 | + return ts.sys.createSHA256Hash(data); |
| 98 | + } |
| 99 | + |
| 100 | + //#region Not implemented methods |
| 101 | + |
| 102 | + public write(data: string): void { |
| 103 | + throw new Error('Method "write" not implemented.'); |
| 104 | + // ts.sys.write(data); |
| 105 | + } |
| 106 | + |
| 107 | + public writeOutputIsTTY(): boolean { |
| 108 | + throw new Error('Method "writeOutputIsTTY" not implemented.'); |
| 109 | + // return ts.sys.writeOutputIsTTY(); |
| 110 | + } |
| 111 | + |
| 112 | + public writeFile(path: string, data: string, writeByteOrderMark?: boolean): void { |
| 113 | + throw new Error('Method "writeFile" not implemented.'); |
| 114 | + // return ts.sys.writeFile(path, data, writeByteOrderMark); |
| 115 | + } |
| 116 | + |
| 117 | + public createDirectory(path: string): void { |
| 118 | + throw new Error('Method "createDirectory" not implemented.'); |
| 119 | + // return ts.sys.createDirectory(path); |
| 120 | + } |
| 121 | + |
| 122 | + public setModifiedTime(path: string, time: Date): void { |
| 123 | + throw new Error('Method "setModifiedTime" not implemented.'); |
| 124 | + // return ts.sys.setModifiedTime(path, time); |
| 125 | + } |
| 126 | + |
| 127 | + public deleteFile(path: string): void { |
| 128 | + throw new Error('Method "deleteFile" not implemented.'); |
| 129 | + // return ts.sys.deleteFile(path); |
| 130 | + } |
| 131 | + |
| 132 | + public createHash(data: string): string { |
| 133 | + throw new Error('Method "createHash" not implemented.'); |
| 134 | + // return ts.sys.createHash(data); |
| 135 | + } |
| 136 | + |
| 137 | + public getMemoryUsage(): number { |
| 138 | + throw new Error('Method "getMemoryUsage" not implemented.'); |
| 139 | + // return ts.sys.getMemoryUsage(); |
| 140 | + } |
| 141 | + |
| 142 | + public exit(exitCode?: number): void { |
| 143 | + throw new Error('Method "exit" not implemented.'); |
| 144 | + // return ts.sys.exit(exitCode); |
| 145 | + } |
| 146 | + |
| 147 | + public setTimeout(callback: (...args: any[]) => void, ms: number, ...args: any[]): any { |
| 148 | + throw new Error('Method "setTimeout" not implemented.'); |
| 149 | + // return ts.sys.setTimeout(callback, ms, ...args); |
| 150 | + } |
| 151 | + |
| 152 | + public clearTimeout(timeoutId: any): void { |
| 153 | + throw new Error('Method "clearTimeout" not implemented.'); |
| 154 | + // return ts.sys.clearTimeout(timeoutId); |
| 155 | + } |
| 156 | + |
| 157 | + public clearScreen(): void { |
| 158 | + throw new Error('Method "clearScreen" not implemented.'); |
| 159 | + // return ts.sys.clearScreen(); |
| 160 | + } |
| 161 | + |
| 162 | + public base64decode(input: string): string { |
| 163 | + throw new Error('Method "base64decode" not implemented.'); |
| 164 | + // return ts.sys.base64decode(input); |
| 165 | + } |
| 166 | + |
| 167 | + public base64encode(input: string): string { |
| 168 | + throw new Error('Method "base64encode" not implemented.'); |
| 169 | + // return ts.sys.base64encode(input); |
| 170 | + } |
| 171 | + |
| 172 | + public setImmediate(callback: (...args: any[]) => void, ...args: any[]): any { |
| 173 | + throw new Error('Method "setImmediate" not implemented.'); |
| 174 | + // return setImmediate(callback, ...args); |
| 175 | + } |
| 176 | + |
| 177 | + public clearImmediate(timeoutId: any): void { |
| 178 | + throw new Error('Method "clearImmediate" not implemented.'); |
| 179 | + // return clearImmediate(timeoutId); |
| 180 | + } |
| 181 | + |
| 182 | + //#endregion |
| 183 | +} |
0 commit comments