Skip to content

Commit 4847b59

Browse files
committed
fix: Fix dependsOn requiring a full id (type.name). Instead modify it to only require type and match multiple resources. A full qualified name (type.name) will match only one resource.
1 parent 1436ff1 commit 4847b59

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

src/entities/project.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ ${JSON.stringify(projectConfigs, null, 2)}`);
248248

249249
for (const r of this.resourceConfigs) {
250250
// User specified dependencies are hard dependencies. They must be present.
251-
r.addDependenciesFromDependsOn((id) => resourceMap.has(id));
251+
r.addDependenciesFromDependsOn((idOrType) => this.getMatchingResourceIds(resourceMap, idOrType));
252252
r.addDependenciesBasedOnParameters((id) => resourceMap.has(id));
253253

254254
// Plugin dependencies are soft dependencies. They only activate if the dependent resource is present.
@@ -299,4 +299,28 @@ ${JSON.stringify(projectConfigs, null, 2)}`);
299299
}
300300
}
301301
}
302+
303+
/**
304+
* This function supports both full (type.name) and partial IDs (type) when matching. It's meant
305+
* for the dependsOn field to simplify dependency resolution for. users.
306+
* @param resourceMap
307+
* @param idOrType
308+
* @private
309+
*/
310+
private getMatchingResourceIds(
311+
resourceMap: Map<string, ResourceConfig>,
312+
idOrType: string
313+
): string[] {
314+
const hasName = idOrType.includes('.');
315+
316+
if (hasName) {
317+
// Full ID (type.name): return exact match or empty array
318+
return resourceMap.has(idOrType) ? [idOrType] : [];
319+
} else {
320+
// Partial ID (type only): return all resources with this type
321+
return [...resourceMap.values()]
322+
.filter((resource) => resource.type === idOrType)
323+
.map((resource) => resource.id);
324+
}
325+
}
302326
}

src/entities/resource-config.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,15 @@ export class ResourceConfig implements ConfigBlock {
100100
this.raw[name] = value;
101101
}
102102

103-
addDependenciesFromDependsOn(resourceExists: (id: string) => boolean) {
104-
for (const id of this.dependsOn) {
105-
if (!resourceExists(id)) {
106-
throw new Error(`Reference ${id} is not a valid resource`);
103+
addDependenciesFromDependsOn(getMatchingResourceIds: (idOrType: string) => string[]) {
104+
for (const idOrType of this.dependsOn) {
105+
const matchingIds = getMatchingResourceIds(idOrType);
106+
107+
if (matchingIds.length === 0) {
108+
throw new Error(`Reference ${idOrType} is not a valid resource`);
107109
}
108110

109-
this.dependencyIds.push(id);
111+
this.dependencyIds.push(...matchingIds);
110112
}
111113
}
112114

0 commit comments

Comments
 (0)