Skip to content

Commit

Permalink
fix: use find-up to find correct package manager in mono repo (zensta…
Browse files Browse the repository at this point in the history
  • Loading branch information
jiashengguo authored Mar 9, 2023
1 parent 9d45384 commit 5e4e1d6
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions packages/schema/src/utils/pkg-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,36 @@ import { execSync } from './exec-utils';

export type PackageManagers = 'npm' | 'yarn' | 'pnpm';

function findUp(names: string[], cwd: string): string | undefined {
let dir = cwd;
// eslint-disable-next-line no-constant-condition
while (true) {
const target = names.find((name) => fs.existsSync(path.join(dir, name)));
if (target) return target;

const up = path.resolve(dir, '..');
if (up === dir) return undefined; // it'll fail anyway
dir = up;
}
}

function getPackageManager(projectPath = '.'): PackageManagers {
if (fs.existsSync(path.join(projectPath, 'yarn.lock'))) {
return 'yarn';
} else if (fs.existsSync(path.join(projectPath, 'pnpm-lock.yaml'))) {
return 'pnpm';
} else {
const lockFile = findUp(['yarn.lock', 'pnpm-lock.yaml', 'package-lock.json'], projectPath);

if (!lockFile) {
// default use npm
return 'npm';
}
}

switch (path.basename(lockFile)) {
case 'yarn.lock':
return 'yarn';
case 'pnpm-lock.yaml':
return 'pnpm';
default:
return 'npm';
}
}
export function installPackage(
pkg: string,
dev: boolean,
Expand Down

0 comments on commit 5e4e1d6

Please sign in to comment.