|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as path from 'path'; |
| 3 | + |
| 4 | + |
| 5 | +export interface RefactoryHost { |
| 6 | + readonly basePath: string; |
| 7 | + |
| 8 | + stat(filePath: string): fs.Stats | null; |
| 9 | + |
| 10 | + write(filePath: string, content: string): void; |
| 11 | + read(filePath: string): string; |
| 12 | + exists(filePath: string): boolean; |
| 13 | + |
| 14 | + list(dirPath: string): string[]; |
| 15 | + isDirectory(dirPath: string): boolean; |
| 16 | +} |
| 17 | + |
| 18 | + |
| 19 | +export class InvalidRefactoryHost implements RefactoryHost { |
| 20 | + get basePath(): string { throw new Error('Unimplemented: basePath'); } |
| 21 | + |
| 22 | + write(filePath: string, content: string): void { throw new Error('Unimplemented: write'); } |
| 23 | + read(filePath: string): string { throw new Error('Unimplemented: read'); } |
| 24 | + exists(filePath: string): boolean { throw new Error('Unimplemented: exists'); } |
| 25 | + |
| 26 | + list(dirPath: string): string[] { throw new Error('Unimplemented: list'); } |
| 27 | + isDirectory(dirPath: string): boolean { throw new Error('Unimplemented: isDirectory'); } |
| 28 | + |
| 29 | + stat(dirPath: string): fs.Stats { throw new Error('Unimplemented: stat'); } |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | +export class NullRefactoryHost implements RefactoryHost { |
| 34 | + constructor(public readonly basePath: string) {} |
| 35 | + |
| 36 | + write(filePath: string, content: string): void { throw new Error('Unimplemented: write'); } |
| 37 | + read(filePath: string): string { throw new Error('Unimplemented: read'); } |
| 38 | + exists(filePath: string): boolean { return false; } |
| 39 | + |
| 40 | + list(dirPath: string): string[] { return []; } |
| 41 | + isDirectory(dirPath: string): boolean { return false; } |
| 42 | + |
| 43 | + stat(dirPath: string): fs.Stats { return null; } |
| 44 | +} |
| 45 | + |
| 46 | + |
| 47 | +export class NodeRefactoryHost implements RefactoryHost { |
| 48 | + constructor(private _basePath: string = '/') {} |
| 49 | + private _normalize(p: string) { |
| 50 | + return path.normalize(path.isAbsolute(p) ? p : path.join(this._basePath, p)); |
| 51 | + } |
| 52 | + |
| 53 | + get basePath(): string { return this._basePath; } |
| 54 | + |
| 55 | + stat(filePath: string): fs.Stats { |
| 56 | + return fs.statSync(filePath); |
| 57 | + } |
| 58 | + |
| 59 | + write(filePath: string, content: string): void { |
| 60 | + fs.writeFileSync(this._normalize(filePath), content, 'utf8'); |
| 61 | + } |
| 62 | + read(filePath: string): string { |
| 63 | + return fs.readFileSync(this._normalize(filePath), 'utf8'); |
| 64 | + } |
| 65 | + exists(filePath: string): boolean { |
| 66 | + return fs.existsSync(this._normalize(filePath)); |
| 67 | + } |
| 68 | + list(dirPath: string): string[] { |
| 69 | + return fs.readdirSync(this._normalize(dirPath)); |
| 70 | + } |
| 71 | + isDirectory(dirPath: string): boolean { |
| 72 | + return fs.statSync(this._normalize(dirPath)).isDirectory(); |
| 73 | + } |
| 74 | +} |
0 commit comments