Skip to content
Open
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
32 changes: 24 additions & 8 deletions packages/core/src/lib/detect-package-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type JsPackageManager = 'npm' | 'pnpm' | 'yarn' | 'bun' | 'deno';

type JsPackageManagerMeta = {
name: JsPackageManager;
lockfile: string;
lockfile: string | string[];
add: string;
exec: string;
dlx: string;
Expand Down Expand Up @@ -36,7 +36,7 @@ export const JS_PACKAGE_MANAGERS: Record<JsPackageManager, JsPackageManagerMeta>
},
bun: {
name: 'bun',
lockfile: 'bun.lockb',
lockfile: ['bun.lock', 'bun.lockb'],
add: 'bun add',
exec: 'bun run',
dlx: 'bunx',
Expand Down Expand Up @@ -67,14 +67,30 @@ export function detectJsPackageManager(opts?: {
let pm: JsPackageManager;
let detectedPm: JsPackageManager | undefined;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let detectedPm: JsPackageManager | undefined;
let detectedPm: JsPackageManager | undefined;
let detectedPmLockFile: string | undefined;

for (pm in JS_PACKAGE_MANAGERS) {
const lockFilePath = path.join(
cwd,
JS_PACKAGE_MANAGERS[pm].lockfile,
);
const lockfiles = Array.isArray(JS_PACKAGE_MANAGERS[pm].lockfile)
? JS_PACKAGE_MANAGERS[pm].lockfile
: [JS_PACKAGE_MANAGERS[pm].lockfile as string];
Comment on lines +70 to +72
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const lockfiles = Array.isArray(JS_PACKAGE_MANAGERS[pm].lockfile)
? JS_PACKAGE_MANAGERS[pm].lockfile
: [JS_PACKAGE_MANAGERS[pm].lockfile as string];
const possibleLockFiles = _.castArray(JS_PACKAGE_MANAGERS[pm].lockfile));


if (pathExistsSync(lockFilePath)) {
let lockFileExists = false;
for (const lockfile of lockfiles) {
const lockFilePath = path.join(cwd, lockfile);
if (pathExistsSync(lockFilePath)) {
lockFileExists = true;
break;
}
}
Comment on lines +74 to +81
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let lockFileExists = false;
for (const lockfile of lockfiles) {
const lockFilePath = path.join(cwd, lockfile);
if (pathExistsSync(lockFilePath)) {
lockFileExists = true;
break;
}
}
const thisPmLockFile = _.find(possibleLockFiles, (lockFileName) => pathExistsSync(path.join(cwd, lockFileName)));


if (lockFileExists) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (lockFileExists) {
if (thisPmlockFile) {

// if we find 2 lockfiles at the same level, we throw an error
if (detectedPm) throw new Error(`Found multiple js package manager lockfiles - ${JS_PACKAGE_MANAGERS[pm].lockfile} and ${JS_PACKAGE_MANAGERS[detectedPm].lockfile}`);
if (detectedPm) {
const currentLockfiles = Array.isArray(JS_PACKAGE_MANAGERS[pm].lockfile)
? (JS_PACKAGE_MANAGERS[pm].lockfile as string[]).join(' or ')
: JS_PACKAGE_MANAGERS[pm].lockfile;
const detectedLockfiles = Array.isArray(JS_PACKAGE_MANAGERS[detectedPm].lockfile)
? (JS_PACKAGE_MANAGERS[detectedPm].lockfile as string[]).join(' or ')
: JS_PACKAGE_MANAGERS[detectedPm].lockfile;
throw new Error(`Found multiple js package manager lockfiles - ${currentLockfiles} and ${detectedLockfiles}`);
}
detectedPm = pm;
Comment on lines +85 to 94
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (detectedPm) {
const currentLockfiles = Array.isArray(JS_PACKAGE_MANAGERS[pm].lockfile)
? (JS_PACKAGE_MANAGERS[pm].lockfile as string[]).join(' or ')
: JS_PACKAGE_MANAGERS[pm].lockfile;
const detectedLockfiles = Array.isArray(JS_PACKAGE_MANAGERS[detectedPm].lockfile)
? (JS_PACKAGE_MANAGERS[detectedPm].lockfile as string[]).join(' or ')
: JS_PACKAGE_MANAGERS[detectedPm].lockfile;
throw new Error(`Found multiple js package manager lockfiles - ${currentLockfiles} and ${detectedLockfiles}`);
}
detectedPm = pm;
if (detectedPmLockFile) {
throw new Error(`Found multiple js package manager lockfiles - ${thisPmLockFile} and ${detectedPmLockFile}`);
}
detectedPmLockFile = thisPmLockFile;

}
}
Expand Down
Loading