Open History to see the last commit
1import * as Schema from "effect/Schema";
2
3export class CatalogDependencyResolutionError extends Schema.TaggedErrorClass<CatalogDependencyResolutionError>()(
4 "CatalogDependencyResolutionError",
5 {
6 workspacePackage: Schema.String,
7 dependencyName: Schema.String,
8 catalogSpec: Schema.String,
9 catalogKey: Schema.String,
10 },
11) {
12 override get message(): string {
13 return `Unable to resolve '${this.catalogSpec}' for ${this.workspacePackage} dependency '${this.dependencyName}'. Expected key '${this.catalogKey}' in root workspace catalog.`;
14 }
15}
16
17/**
18 * Resolve `catalog:` dependency specs using the workspace catalog.
19 *
20 * Pure function: returns a new record with every `catalog:…` value replaced by
21 * the concrete version string found in `catalog`. Throws on missing entries.
22 */
23export function resolveCatalogDependencies(
24 dependencies: Record<string, string>,
25 catalog: Record<string, string>,
26 workspacePackage: string,
27): Record<string, string> {
28 return Object.fromEntries(
29 Object.entries(dependencies).map(([name, spec]) => {
30 if (typeof spec !== "string" || !spec.startsWith("catalog:")) {
31 return [name, spec];
32 }
33
34 const catalogKey = spec.slice("catalog:".length).trim();
35 const lookupKey = catalogKey.length > 0 ? catalogKey : name;
36 const resolved = catalog[lookupKey];
37
38 if (typeof resolved !== "string" || resolved.length === 0) {
39 throw new CatalogDependencyResolutionError({
40 workspacePackage,
41 dependencyName: name,
42 catalogSpec: spec,
43 catalogKey: lookupKey,
44 });
45 }
46
47 return [name, resolved];
48 }),
49 );
50}