|
1 | 1 | import { ProjectGraph } from '../config/project-graph'; |
2 | 2 |
|
| 3 | +function findMatchingSegments( |
| 4 | + s: string, |
| 5 | + projectGraph: ProjectGraph |
| 6 | +): [string, string?, string?] | undefined { |
| 7 | + const projectNames = Object.keys(projectGraph.nodes); |
| 8 | + // return project if matching |
| 9 | + if (projectNames.includes(s)) { |
| 10 | + return [s]; |
| 11 | + } |
| 12 | + if (!s.includes(':')) { |
| 13 | + return; |
| 14 | + } |
| 15 | + for (const projectName of projectNames) { |
| 16 | + for (const [targetName, targetConfig] of Object.entries( |
| 17 | + projectGraph.nodes[projectName].data.targets || {} |
| 18 | + )) { |
| 19 | + if (s === `${projectName}:${targetName}`) { |
| 20 | + return [projectName, targetName]; |
| 21 | + } |
| 22 | + if (targetConfig.configurations) { |
| 23 | + for (const configurationName of Object.keys( |
| 24 | + targetConfig.configurations |
| 25 | + )) { |
| 26 | + if (s === `${projectName}:${targetName}:${configurationName}`) { |
| 27 | + return [projectName, targetName, configurationName]; |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
3 | 35 | export function splitTarget( |
4 | 36 | s: string, |
5 | 37 | projectGraph: ProjectGraph |
6 | 38 | ): [project: string, target?: string, configuration?: string] { |
7 | | - let [project, ...segments] = splitByColons(s); |
8 | | - const validTargets = projectGraph.nodes[project] |
9 | | - ? projectGraph.nodes[project].data.targets |
10 | | - : {}; |
11 | | - const validTargetNames = new Set(Object.keys(validTargets ?? {})); |
| 39 | + const matchingSegments = findMatchingSegments(s, projectGraph); |
| 40 | + if (matchingSegments) { |
| 41 | + return matchingSegments; |
| 42 | + } |
| 43 | + if (s.indexOf(':') > 0) { |
| 44 | + let [project, ...segments] = splitByColons(s); |
| 45 | + // if only configuration cannot be matched, try to match project and target |
| 46 | + const configuration = segments[segments.length - 1]; |
| 47 | + const rest = s.slice(0, -(configuration.length + 1)); |
| 48 | + const matchingSegments = findMatchingSegments(rest, projectGraph); |
| 49 | + if (matchingSegments && matchingSegments.length === 2) { |
| 50 | + return [...(matchingSegments as [string, string]), configuration]; |
| 51 | + } |
| 52 | + // no project-target pair found, do the naive matching |
| 53 | + const validTargets = projectGraph.nodes[project] |
| 54 | + ? projectGraph.nodes[project].data.targets |
| 55 | + : {}; |
| 56 | + const validTargetNames = new Set(Object.keys(validTargets ?? {})); |
12 | 57 |
|
13 | | - return [project, ...groupJointSegments(segments, validTargetNames)] as [ |
14 | | - string, |
15 | | - string?, |
16 | | - string? |
17 | | - ]; |
| 58 | + return [project, ...groupJointSegments(segments, validTargetNames)] as [ |
| 59 | + string, |
| 60 | + string?, |
| 61 | + string? |
| 62 | + ]; |
| 63 | + } |
| 64 | + // we don't know what to do with the string, return as is |
| 65 | + return [s]; |
18 | 66 | } |
19 | 67 |
|
20 | 68 | function groupJointSegments(segments: string[], validTargetNames: Set<string>) { |
|
0 commit comments