Open History to see the last commit
1import * as Schema from "effect/Schema";
2
3class CatalogDependencyResolutionError extends Schema.TaggedError<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 // An override key can be a selector such as `@scope/parent>effect`; like
36 // pnpm, a bare `catalog:` there means the catalog entry of the package
37 // the selector ends in.
38 const lookupKey = catalogKey.length > 0 ? catalogKey : (name.split(">").at(-1) ?? name);
39 const resolved = catalog[lookupKey];
40
41 if (typeof resolved !== "string" || resolved.length === 0) {
42 throw new CatalogDependencyResolutionError({
43 workspacePackage,
44 dependencyName: name,
45 catalogSpec: spec,
46 catalogKey: lookupKey,
47 });
48 }
49
50 return [name, resolved];
51 }),
52 );
53}