( path: string, host: WorkspaceHost, format?: WorkspaceFormat, // return type will eventually have a `diagnostics` property as well )
| 58 | * the `workspace` property. |
| 59 | */ |
| 60 | export async function readWorkspace( |
| 61 | path: string, |
| 62 | host: WorkspaceHost, |
| 63 | format?: WorkspaceFormat, |
| 64 | // return type will eventually have a `diagnostics` property as well |
| 65 | ): Promise<{ workspace: WorkspaceDefinition }> { |
| 66 | if (await host.isDirectory(path)) { |
| 67 | // TODO: Warn if multiple found (requires diagnostics support) |
| 68 | const directory = normalize(path); |
| 69 | let found = false; |
| 70 | for (const [name, nameFormat] of Object.entries(workspaceFiles)) { |
| 71 | if (format !== undefined && format !== nameFormat) { |
| 72 | continue; |
| 73 | } |
| 74 | |
| 75 | const potential = getSystemPath(join(directory, name)); |
| 76 | if (await host.isFile(potential)) { |
| 77 | path = potential; |
| 78 | format = nameFormat; |
| 79 | found = true; |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | if (!found) { |
| 84 | throw new Error( |
| 85 | 'Unable to locate a workspace file for workspace path. Are you missing an `angular.json`' + |
| 86 | ' or `.angular.json` file?', |
| 87 | ); |
| 88 | } |
| 89 | } else if (format === undefined) { |
| 90 | const filename = basename(normalize(path)); |
| 91 | if (filename in workspaceFiles) { |
| 92 | format = workspaceFiles[filename]; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | if (format === undefined) { |
| 97 | throw new Error('Unable to determine format for workspace path.'); |
| 98 | } |
| 99 | |
| 100 | let workspace; |
| 101 | switch (format) { |
| 102 | case WorkspaceFormat.JSON: |
| 103 | workspace = await readJsonWorkspace(path, host); |
| 104 | break; |
| 105 | default: |
| 106 | throw new Error('Unsupported workspace format.'); |
| 107 | } |
| 108 | |
| 109 | formatLookup.set(workspace, WorkspaceFormat.JSON); |
| 110 | |
| 111 | return { workspace }; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Writes a `WorkspaceDefinition` to the underlying storage via the provided `WorkspaceHost`. |
no test coverage detected