Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/bundler-plugins/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@
"@babel/core": "^7.18.5",
"@sentry/core": "10.67.0",
"dotenv": "^17.4.2",
"find-up": "^5.0.0",
"glob": "^13.0.6",
"magic-string": "~0.30.8",
"sentry": "^0.44.0",
Expand Down
37 changes: 25 additions & 12 deletions packages/bundler-plugins/src/core/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* oxlint-disable max-lines */
import findUp from 'find-up';
import path from 'path';
import fs from 'fs';
import os from 'os';
Expand Down Expand Up @@ -138,18 +137,32 @@ export function getDependencies(packageJson: PackageJson): {
return { deps, depsVersions };
}

function lookupPackageJson(cwd: string, stopAt: string): PackageJson | undefined {
const jsonPath = findUp.sync(
dirName => {
// Stop if we reach this dir
if (path.normalize(dirName) === stopAt) {
return findUp.stop;
}
function findPackageJson(cwd: string, stopAt: string): string | undefined {
let dirName = path.resolve(cwd);

return findUp.sync.exists(`${dirName}/package.json`) ? 'package.json' : undefined;
},
{ cwd },
);
for (;;) {
// Stop if we reach this dir
if (path.normalize(dirName) === stopAt) {
return undefined;
}

const jsonPath = path.join(dirName, 'package.json');
if (fs.existsSync(jsonPath)) {
return jsonPath;
}

const parentDirName = path.dirname(dirName);
// We reached the file system root
if (parentDirName === dirName) {
return undefined;
}

dirName = parentDirName;
}
}

function lookupPackageJson(cwd: string, stopAt: string): PackageJson | undefined {
const jsonPath = findPackageJson(cwd, stopAt);

if (!jsonPath) {
return undefined;
Expand Down
15 changes: 15 additions & 0 deletions packages/bundler-plugins/test/core/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ describe('getPackageJson', () => {

expect(packageJson).toBeUndefined();
});

test('it stops at the file system root when `stopAt` is never reached', () => {
const existsSyncSpy = vi.spyOn(fs, 'existsSync').mockReturnValue(false);

try {
const packageJson = getPackageJson({
cwd: getCwdFor('/fixtures/no-valid-package/deeply/nested'),
stopAt: getCwdFor('/fixtures/this-directory-is-not-an-ancestor'),
});

expect(packageJson).toBeUndefined();
} finally {
existsSyncSpy.mockRestore();
}
});
});

describe('parseMajorVersion', () => {
Expand Down
Loading