Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI: Fix using wrong package managers in existing projects #25474

Merged
merged 4 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
20 changes: 13 additions & 7 deletions code/lib/cli/src/js-package-manager/JsPackageManagerFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,7 @@ 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
// Option 2: We try to infer the package manager from the closest lockfile
const yarnVersion = getYarnVersion(cwd);

const closestLockfilePath = findUpSync([YARN_LOCKFILE, PNPM_LOCKFILE, NPM_LOCKFILE], {
Expand All @@ -55,6 +49,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
Loading