| 50 | } |
| 51 | |
| 52 | export class Project { |
| 53 | project: TsProject |
| 54 | parser: ReturnType<typeof createParser> |
| 55 | |
| 56 | get parserOptions() { |
| 57 | return this.options.parserOptions |
| 58 | } |
| 59 | |
| 60 | constructor(private options: ProjectOptions) { |
| 61 | const { parserOptions } = options |
| 62 | |
| 63 | this.project = createTsProject(options) |
| 64 | this.parser = createParser(parserOptions) |
| 65 | this.createSourceFiles() |
| 66 | } |
| 67 | |
| 68 | get files() { |
| 69 | return this.options.getFiles() |
| 70 | } |
| 71 | |
| 72 | getSourceFile = (filePath: string): SourceFile | undefined => { |
| 73 | return this.project.getSourceFile(filePath) |
| 74 | } |
| 75 | |
| 76 | createSourceFile = (filePath: string): SourceFile => { |
| 77 | const { readFile } = this.options |
| 78 | return this.project.createSourceFile(filePath, readFile(filePath), { |
| 79 | overwrite: true, |
| 80 | scriptKind: ScriptKind.TSX, |
| 81 | }) |
| 82 | } |
| 83 | |
| 84 | createSourceFiles = () => { |
| 85 | const files = this.getFiles() |
| 86 | for (const file of files) { |
| 87 | this.createSourceFile(file) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | addSourceFile = (filePath: string, content: string): SourceFile => { |
| 92 | return this.project.createSourceFile(filePath, content, { |
| 93 | overwrite: true, |
| 94 | scriptKind: ScriptKind.TSX, |
| 95 | }) |
| 96 | } |
| 97 | |
| 98 | removeSourceFile = (filePath: string): boolean => { |
| 99 | const sourceFile = this.project.getSourceFile(filePath) |
| 100 | if (sourceFile) { |
| 101 | return this.project.removeSourceFile(sourceFile) |
| 102 | } |
| 103 | return false |
| 104 | } |
| 105 | |
| 106 | reloadSourceFile = (filePath: string): FileSystemRefreshResult | undefined => { |
| 107 | return this.getSourceFile(filePath)?.refreshFromFileSystemSync() |
| 108 | } |
| 109 |
nothing calls this directly
no test coverage detected