* Transforms any optional dependencies declared in the graph to non-optional dependency, if the target * can already be reached from the root project. * * @public
()
| 318 | * @public |
| 319 | */ |
| 320 | async resolveOptionalDependencies() { |
| 321 | this._checkSealed(); |
| 322 | if (!this._hasUnresolvedOptionalDependencies) { |
| 323 | log.verbose(`Skipping resolution of optional dependencies since none have been declared`); |
| 324 | return; |
| 325 | } |
| 326 | log.verbose(`Resolving optional dependencies...`); |
| 327 | |
| 328 | // First collect all projects that are currently reachable from the root project (=all non-optional projects) |
| 329 | const resolvedProjects = new Set(); |
| 330 | await this.traverseBreadthFirst(({project}) => { |
| 331 | resolvedProjects.add(project.getName()); |
| 332 | }); |
| 333 | |
| 334 | let unresolvedOptDeps = false; |
| 335 | for (const [fromProjectName, optDependencies] of this._optAdjList) { |
| 336 | for (const toProjectName of optDependencies) { |
| 337 | if (resolvedProjects.has(toProjectName)) { |
| 338 | // Target node is already reachable in the graph |
| 339 | // => Resolve optional dependency |
| 340 | log.verbose(`Resolving optional dependency from ${fromProjectName} to ${toProjectName}...`); |
| 341 | |
| 342 | if (this._adjList.get(toProjectName).has(fromProjectName)) { |
| 343 | log.verbose( |
| 344 | ` Cyclic optional dependency detected: ${toProjectName} already has a non-optional ` + |
| 345 | `dependency to ${fromProjectName}`); |
| 346 | log.verbose( |
| 347 | ` Optional dependency from ${fromProjectName} to ${toProjectName} ` + |
| 348 | `will not be declared as it would introduce a cycle`); |
| 349 | unresolvedOptDeps = true; |
| 350 | } else { |
| 351 | this.declareDependency(fromProjectName, toProjectName); |
| 352 | // This optional dependency has now been resolved |
| 353 | // => Remove it from the list of optional dependencies |
| 354 | optDependencies.delete(toProjectName); |
| 355 | } |
| 356 | } else { |
| 357 | unresolvedOptDeps = true; |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | if (!unresolvedOptDeps) { |
| 362 | this._hasUnresolvedOptionalDependencies = false; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * Callback for graph traversal operations |
no test coverage detected