( level: 'local' | 'global', )
| 175 | ): Promise<AngularWorkspace | undefined>; |
| 176 | |
| 177 | export async function getWorkspace( |
| 178 | level: 'local' | 'global', |
| 179 | ): Promise<AngularWorkspace | undefined> { |
| 180 | if (cachedWorkspaces.has(level)) { |
| 181 | return cachedWorkspaces.get(level); |
| 182 | } |
| 183 | |
| 184 | const configPath = level === 'local' ? await projectFilePath() : globalFilePath(); |
| 185 | if (!configPath) { |
| 186 | if (level === 'global') { |
| 187 | // Unlike a local config, a global config is not mandatory. |
| 188 | // So we create an empty one in memory and keep it as such until it has been modified and saved. |
| 189 | const globalWorkspace = new AngularWorkspace( |
| 190 | { extensions: {}, projects: new workspaces.ProjectDefinitionCollection() }, |
| 191 | defaultGlobalFilePath, |
| 192 | ); |
| 193 | |
| 194 | cachedWorkspaces.set(level, globalWorkspace); |
| 195 | |
| 196 | return globalWorkspace; |
| 197 | } |
| 198 | |
| 199 | cachedWorkspaces.set(level, undefined); |
| 200 | |
| 201 | return undefined; |
| 202 | } |
| 203 | |
| 204 | try { |
| 205 | const workspace = await AngularWorkspace.load(configPath); |
| 206 | cachedWorkspaces.set(level, workspace); |
| 207 | |
| 208 | return workspace; |
| 209 | } catch (error) { |
| 210 | throw new Error(`Workspace config file cannot be loaded: ${configPath}`, { cause: error }); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * This method will load the workspace configuration in raw JSON format. |
no test coverage detected