fix(node-utils): avoid find-up sync API in monorepo root lookup

vite8^2
xingyu4j 2026-03-15 20:50:07 +08:00
parent 26e9aa244b
commit 70dad0f600
1 changed files with 16 additions and 9 deletions

View File

@ -1,24 +1,31 @@
import type { Package } from '@manypkg/get-packages'; import type { Package } from '@manypkg/get-packages';
import { dirname } from 'node:path'; import { existsSync } from 'node:fs';
import { dirname, join, resolve } from 'node:path';
import * as manypkg from '@manypkg/get-packages'; import * as manypkg from '@manypkg/get-packages';
import * as findUp from 'find-up';
const { getPackages: getPackagesFunc, getPackagesSync: getPackagesSyncFunc } = const { getPackages: getPackagesFunc, getPackagesSync: getPackagesSyncFunc } =
manypkg; manypkg;
const { findUpSync } = findUp;
/** /**
* *
* @param cwd * @param cwd
*/ */
function findMonorepoRoot(cwd: string = process.cwd()) { function findMonorepoRoot(cwd: string = process.cwd()) {
const lockFile = findUpSync('pnpm-lock.yaml', { let currentDir = resolve(cwd);
cwd,
type: 'file', while (true) {
}); if (existsSync(join(currentDir, 'pnpm-lock.yaml'))) {
return dirname(lockFile || ''); return currentDir;
}
const parentDir = dirname(currentDir);
if (parentDir === currentDir) {
return '';
}
currentDir = parentDir;
}
} }
/** /**