| 410 | * @returns @zh 所有依赖 ID 的集合 @en Set of all dependency IDs |
| 411 | */ |
| 412 | export function getAllDependencies<T extends IDependable>( |
| 413 | itemId: string, |
| 414 | items: T[], |
| 415 | options: { resolveId?: (id: string) => string } = {} |
| 416 | ): Set<string> { |
| 417 | const { resolveId = resolveDependencyId } = options; |
| 418 | const itemMap = new Map<string, T>(); |
| 419 | for (const item of items) { |
| 420 | itemMap.set(item.id, item); |
| 421 | } |
| 422 | |
| 423 | const allDeps = new Set<string>(); |
| 424 | const visited = new Set<string>(); |
| 425 | |
| 426 | const collect = (id: string) => { |
| 427 | if (visited.has(id)) return; |
| 428 | visited.add(id); |
| 429 | |
| 430 | const item = itemMap.get(id); |
| 431 | if (!item) return; |
| 432 | |
| 433 | for (const dep of item.dependencies || []) { |
| 434 | const depId = resolveId(dep); |
| 435 | allDeps.add(depId); |
| 436 | collect(depId); |
| 437 | } |
| 438 | }; |
| 439 | |
| 440 | collect(itemId); |
| 441 | return allDeps; |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * @zh 获取依赖于指定项目的所有项目(反向依赖) |