( config: ReturnType<Config["loadConfigOrDefault"]>, workspaceId: string )
| 76 | * Detects cycles (max 32 hops). |
| 77 | */ |
| 78 | export function getTaskDepthFromConfig( |
| 79 | config: ReturnType<Config["loadConfigOrDefault"]>, |
| 80 | workspaceId: string |
| 81 | ): number { |
| 82 | const parentById = new Map<string, string | undefined>(); |
| 83 | for (const project of config.projects.values()) { |
| 84 | for (const workspace of project.workspaces) { |
| 85 | if (!workspace.id) continue; |
| 86 | parentById.set(workspace.id, workspace.parentWorkspaceId); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | let depth = 0; |
| 91 | let current = workspaceId; |
| 92 | for (let i = 0; i < 32; i++) { |
| 93 | const parent = parentById.get(current); |
| 94 | if (!parent) break; |
| 95 | depth += 1; |
| 96 | current = parent; |
| 97 | } |
| 98 | |
| 99 | if (depth >= 32) { |
| 100 | throw new Error( |
| 101 | `getTaskDepthFromConfig: possible parentWorkspaceId cycle starting at ${workspaceId}` |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | return depth; |
| 106 | } |
no test coverage detected