MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / loadWithManifests

Function loadWithManifests

src/resolution/workspace-packages.ts:147–207  ·  view source on GitHub ↗
(projectRoot: string, manifests: readonly string[])

Source from the content-addressed store, hash-verified

145}
146
147function loadWithManifests(projectRoot: string, manifests: readonly string[]): WorkspacePackages | null {
148 const byName = new Map<string, string>();
149
150 const patterns = readWorkspaceGlobs(projectRoot);
151 // What a negated glob matches (`!examples/with-nx`, pnpm's `!**/example`) is
152 // no member, wherever the glob is listed.
153 const excluded = patterns.filter((p) => p.startsWith('!')).map((p) => p.slice(1));
154 for (const pattern of patterns) {
155 if (pattern.startsWith('!')) continue;
156 for (const dir of expandWorkspaceGlob(projectRoot, pattern)) {
157 if (excluded.some((x) => workspaceGlobMatches(x, dir))) continue;
158 const pkgName = readPackageName(path.join(projectRoot, dir));
159 // First declaration wins — workspace patterns are tried in order.
160 if (pkgName && !byName.has(pkgName)) byName.set(pkgName, dir);
161 }
162 }
163
164 // HarmonyOS/OpenHarmony (ArkTS) modular projects: every module's
165 // oh-package.json5 declares its local siblings as `"data": "file:../../
166 // core/data"` dependencies, and code then imports the bare name
167 // (`import { CartRepository } from "data"`). Same monorepo problem as npm
168 // workspaces, different manifest. An npm package does the same with
169 // `"acme-shared": "file:../common"` and no root `workspaces` (#2456).
170 const { ohpm, npm, workspaceRoots } = collectPathDeps(projectRoot, manifests);
171 const entryByName = new Map<string, string>();
172 for (const [name, dir] of ohpm) {
173 if (byName.has(name)) continue;
174 byName.set(name, dir);
175 const entry = readOhpmMain(projectRoot, dir);
176 if (entry) entryByName.set(name, entry);
177 }
178 for (const [name, dir] of npm.get('') ?? []) {
179 if (!byName.has(name)) byName.set(name, dir);
180 }
181 npm.delete('');
182
183 // A member of the root's own workspace is installed with it, whatever its
184 // manifest declares: package managers don't nest workspaces.
185 const members = new Set(byName.values());
186 const nestedRoots = workspaceRoots.filter((dir) => !members.has(dir));
187
188 if (byName.size === 0 && npm.size === 0 && nestedRoots.length === 0) return null;
189
190 // What each npm-style package's manifest declares for its importers.
191 const entriesByDir = new Map<string, PackageEntries>();
192 const packageDirs = new Set(byName.values());
193 for (const names of npm.values()) for (const dir of names.values()) packageDirs.add(dir);
194 for (const dir of packageDirs) {
195 const entries = readPackageEntries(projectRoot, dir);
196 if (entries) entriesByDir.set(dir, entries);
197 }
198
199 logDebug('workspace packages loaded', { count: byName.size, scoped: npm.size, entries: entriesByDir.size });
200 return {
201 byName,
202 entryByName: entryByName.size > 0 ? entryByName : undefined,
203 entriesByDir: entriesByDir.size > 0 ? entriesByDir : undefined,
204 scopedByName: npm.size > 0 ? npm : undefined,

Callers 1

Calls 15

logDebugFunction · 0.90
readWorkspaceGlobsFunction · 0.85
expandWorkspaceGlobFunction · 0.85
workspaceGlobMatchesFunction · 0.85
readPackageNameFunction · 0.85
collectPathDepsFunction · 0.85
readOhpmMainFunction · 0.85
readPackageEntriesFunction · 0.85
mapMethod · 0.80
hasMethod · 0.80
getMethod · 0.65
deleteMethod · 0.65

Tested by

no test coverage detected