(workspaceYaml: string | null, rootPackageJson: string | null)
| 106 | * .yarnrc.yml for Yarn — both use the same `catalog` / `catalogs` shape. |
| 107 | */ |
| 108 | export function parseCatalogs(workspaceYaml: string | null, rootPackageJson: string | null): CatalogMap { |
| 109 | const catalogs: CatalogMap = new Map(); |
| 110 | |
| 111 | const addNamed = (raw: Record<string, Record<string, string>>): void => { |
| 112 | for (const [name, deps] of Object.entries(raw)) { |
| 113 | catalogs.set(normalizeCatalogName(name), deps); |
| 114 | } |
| 115 | }; |
| 116 | |
| 117 | if (workspaceYaml) { |
| 118 | try { |
| 119 | const parsed = yaml.load(workspaceYaml) as { |
| 120 | catalog?: Record<string, string>; |
| 121 | catalogs?: Record<string, Record<string, string>>; |
| 122 | } | null; |
| 123 | |
| 124 | if (parsed?.catalog) { |
| 125 | catalogs.set('', parsed.catalog); // default catalog |
| 126 | } |
| 127 | if (parsed?.catalogs) { |
| 128 | addNamed(parsed.catalogs); |
| 129 | } |
| 130 | } catch { |
| 131 | // ignore malformed yaml |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | if (rootPackageJson) { |
| 136 | try { |
| 137 | const pkg = JSON.parse(rootPackageJson) as Record<string, unknown>; |
| 138 | |
| 139 | // Top-level catalog/catalogs (used by bun, yarn, and proposed npm) |
| 140 | if (pkg.catalog && typeof pkg.catalog === 'object') { |
| 141 | catalogs.set('', pkg.catalog as Record<string, string>); |
| 142 | } |
| 143 | if (pkg.catalogs && typeof pkg.catalogs === 'object') { |
| 144 | addNamed(pkg.catalogs as Record<string, Record<string, string>>); |
| 145 | } |
| 146 | |
| 147 | // Inside workspaces object (bun style) |
| 148 | const workspaces = pkg.workspaces; |
| 149 | if (workspaces && typeof workspaces === 'object' && !Array.isArray(workspaces)) { |
| 150 | const ws = workspaces as Record<string, unknown>; |
| 151 | if (ws.catalog && typeof ws.catalog === 'object') { |
| 152 | catalogs.set('', ws.catalog as Record<string, string>); |
| 153 | } |
| 154 | if (ws.catalogs && typeof ws.catalogs === 'object') { |
| 155 | addNamed(ws.catalogs as Record<string, Record<string, string>>); |
| 156 | } |
| 157 | } |
| 158 | } catch { |
| 159 | // ignore malformed json |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | return catalogs; |
| 164 | } |
| 165 |
no test coverage detected
searching dependent graphs…