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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const findUpSyncMock = vi.mocked(findUpSync);
describe('CLASS: JsPackageManagerFactory', () => {
beforeEach(() => {
findUpSyncMock.mockReturnValue(undefined);
spawnSyncMock.mockReturnValue({ status: 1 } as any);
delete process.env.npm_config_user_agent;
});

Expand Down
23 changes: 14 additions & 9 deletions code/lib/cli/src/js-package-manager/JsPackageManagerFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,15 @@ export class JsPackageManagerFactory {
return new this.PROXY_MAP[force]({ cwd });
}

// Option 2: If the user is running a command via npx/pnpx/yarn create/etc, we infer the package manager from the command
const inferredPackageManager = this.inferPackageManagerFromUserAgent();
if (inferredPackageManager && inferredPackageManager in this.PROXY_MAP) {
return new this.PROXY_MAP[inferredPackageManager]({ cwd });
}

// Option 3: We try to infer the package manager from the closest lockfile
const yarnVersion = getYarnVersion(cwd);

// Option 2: We try to infer the package manager from the closest lockfile
const closestLockfilePath = findUpSync([YARN_LOCKFILE, PNPM_LOCKFILE, NPM_LOCKFILE], {
cwd,
});
const closestLockfile = closestLockfilePath && path.basename(closestLockfilePath);

const hasNPMCommand = hasNPM(cwd);
const hasPNPMCommand = hasPNPM(cwd);
const yarnVersion = getYarnVersion(cwd);

if (yarnVersion && (closestLockfile === YARN_LOCKFILE || (!hasNPMCommand && !hasPNPMCommand))) {
return yarnVersion === 1 ? new Yarn1Proxy({ cwd }) : new Yarn2Proxy({ cwd });
Expand All @@ -55,6 +48,18 @@ export class JsPackageManagerFactory {
return new PNPMProxy({ cwd });
}

if (hasNPMCommand && closestLockfile === NPM_LOCKFILE) {
return new NPMProxy({ cwd });
}

// Option 3: If the user is running a command via npx/pnpx/yarn create/etc, we infer the package manager from the command
const inferredPackageManager = this.inferPackageManagerFromUserAgent();
if (inferredPackageManager && inferredPackageManager in this.PROXY_MAP) {
return new this.PROXY_MAP[inferredPackageManager]({ cwd });
}

// Default fallback, whenever users try to use something different than NPM, PNPM, Yarn,
// but still have NPM installed
if (hasNPMCommand) {
return new NPMProxy({ cwd });
}
Expand Down
4 changes: 2 additions & 2 deletions code/lib/cli/src/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export const doUpgrade = async ({
shell: true,
});

if (check.stderr && !check.stderr.toString().includes('npm notice')) {
if (check.stderr && check.stderr.toString().includes('npm ERR')) {
throw new UpgradeStorybookPackagesError({
command,
args: checkArgs,
Expand All @@ -208,7 +208,7 @@ export const doUpgrade = async ({
logger.info(checkSb.stdout.toString());
logger.info(checkSb.stderr.toString());

if (checkSb.stderr && !checkSb.stderr.toString().includes('npm notice')) {
if (checkSb.stderr && checkSb.stderr.toString().includes('npm ERR')) {
throw new UpgradeStorybookPackagesError({
command,
args: checkSbArgs,
Expand Down