MCPcopy Index your code
hub / github.com/getsentry/sentry-javascript / lookupPackageJson

Function lookupPackageJson

packages/bundler-plugins/src/core/utils.ts:141–174  ·  view source on GitHub ↗
(cwd: string, stopAt: string)

Source from the content-addressed store, hash-verified

139}
140
141function lookupPackageJson(cwd: string, stopAt: string): PackageJson | undefined {
142 const jsonPath = findUp.sync(
143 dirName => {
144 // Stop if we reach this dir
145 if (path.normalize(dirName) === stopAt) {
146 return findUp.stop;
147 }
148
149 return findUp.sync.exists(`${dirName}/package.json`) ? 'package.json' : undefined;
150 },
151 { cwd },
152 );
153
154 if (!jsonPath) {
155 return undefined;
156 }
157
158 try {
159 const jsonStr = fs.readFileSync(jsonPath, 'utf8');
160 const json = JSON.parse(jsonStr) as PackageJson;
161
162 // Ensure it is an actual package.json
163 // This is very much not bulletproof, but should be good enough
164 if ('name' in json || 'private' in json) {
165 return json;
166 }
167 } catch {
168 // Ignore and walk up
169 }
170
171 // Continue up the tree, if we find a fitting package.json
172 const newCwd = path.dirname(path.resolve(`${jsonPath}/..`));
173 return lookupPackageJson(newCwd, stopAt);
174}
175
176/**
177 * Deterministically hashes a string and turns the hash into a uuid.

Callers 1

getPackageJsonFunction · 0.85

Calls 1

resolveMethod · 0.65

Tested by

no test coverage detected