| 17 | */ |
| 18 | @Service() |
| 19 | export class TypingsLoader { |
| 20 | private readonly librariesToGetTypesFrom = [ |
| 21 | '@angular/common', |
| 22 | '@angular/core', |
| 23 | '@angular/forms', |
| 24 | '@angular/router', |
| 25 | '@angular/platform-browser', |
| 26 | '@angular/material', |
| 27 | '@angular/cdk', |
| 28 | '@angular/aria', |
| 29 | ]; |
| 30 | |
| 31 | private webContainer: WebContainer | undefined; |
| 32 | |
| 33 | private readonly _typings = signal<Typing[]>([]); |
| 34 | readonly typings = this._typings.asReadonly(); |
| 35 | readonly typings$ = toObservable(this._typings); |
| 36 | |
| 37 | /** |
| 38 | * Retrieve types from the predefined libraries and set the types files and contents in the `typings` signal |
| 39 | */ |
| 40 | async retrieveTypeDefinitions(webContainer: WebContainer): Promise<void> { |
| 41 | this.webContainer = webContainer; |
| 42 | |
| 43 | const typesDefinitions: Typing[] = []; |
| 44 | |
| 45 | try { |
| 46 | const filesToRead = await this.getFilesToRead(); |
| 47 | |
| 48 | if (filesToRead && filesToRead.length > 0) { |
| 49 | await Promise.all( |
| 50 | filesToRead.map((path) => |
| 51 | webContainer.fs.readFile(path, 'utf-8').then((content) => { |
| 52 | typesDefinitions.push({path, content}); |
| 53 | }), |
| 54 | ), |
| 55 | ); |
| 56 | |
| 57 | this._typings.set(typesDefinitions); |
| 58 | } |
| 59 | } catch (error: any) { |
| 60 | // ignore "ENOENT" errors as this can happen while reading files and resetting the WebContainer |
| 61 | if (error?.message.startsWith('ENOENT')) { |
| 62 | return; |
| 63 | } else { |
| 64 | throw error; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Get the list of files to read the types definitions from the predefined libraries |
| 71 | */ |
| 72 | private async getFilesToRead() { |
| 73 | if (!this.webContainer) return; |
| 74 | |
| 75 | const filesToRead: string[] = []; |
| 76 | const directoriesToRead: string[] = []; |
nothing calls this directly
no test coverage detected
searching dependent graphs…