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

Function loadWithManifests

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

Source from the content-addressed store, hash-verified

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