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

Don't rely on the npm_execpath env variable to determine which package manager to use #272

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/friendly-doors-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@webstone/cli": minor
---

Determine the user's package manager with <pkg-manager> --version rather than relying on the `npm_execpath` env variable.
26 changes: 14 additions & 12 deletions packages/cli/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import { execSync } from "child_process";

type PackageManagers = "npm" | "pnpm" | "yarn";

/**
* If you modify this function, also change it in
* webstone/packages/create-webstone-app/src/helpers.ts
*/
export const determinePackageManager = (): PackageManagers => {
if (process.env.npm_execpath?.endsWith("npm-cli.js")) {
return "npm";
} else if (process.env.npm_execpath?.endsWith("pnpm.cjs")) {
return "pnpm";
} else if (process.env.npm_execpath?.endsWith("yarn.js")) {
return "yarn";
} else {
console.warn(
`Could not determine package manager based on "process.env.npm_execpath". Value for env variable: ${process.env.npm_execpath}. Using npm as a fallback. Please report this as a bug, we'd love to make it more resilient.`
);
return "npm";
}
const packageManagers: PackageManagers[] = ["pnpm", "yarn"];
const installedPackageManager: PackageManagers =
packageManagers.find((pkgManager) => {
try {
execSync(`${pkgManager} --version`);
return pkgManager;
} catch (_) {
// Current `pkgManager` is not installed, move on to the next.
}
}) || "npm";

return installedPackageManager;
};
27 changes: 14 additions & 13 deletions packages/create-webstone-app/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import chalk from "chalk";
import { execSync } from "child_process";

import { ListrTaskWrapper, ListrRenderer } from "listr2/dist/index";

Expand All @@ -13,21 +14,21 @@ export type WebstoneTask = ListrTaskWrapper<Ctx, typeof ListrRenderer>;

/**
* If you modify this function, also change it in
* webstone/packages/cli/src/determine-package-manager.ts
* webstone/packages/cli/src/helpers.ts
*/
export const determinePackageManager = (): PackageManagers => {
if (process.env.npm_execpath?.endsWith("npm-cli.js")) {
return "npm";
} else if (process.env.npm_execpath?.endsWith("pnpm.cjs")) {
return "pnpm";
} else if (process.env.npm_execpath?.endsWith("yarn.js")) {
return "yarn";
} else {
console.warn(
`Could not determine package manager based on "process.env.npm_execpath". Value for env variable: ${process.env.npm_execpath}. Using npm as a fallback. Please report this as a bug, we'd love to make it more resilient.`
);
return "npm";
}
const packageManagers: PackageManagers[] = ["pnpm", "yarn"];
const installedPackageManager: PackageManagers =
packageManagers.find((pkgManager) => {
try {
execSync(`${pkgManager} --version`);
return pkgManager;
} catch (_) {
// Current `pkgManager` is not installed, move on to the next.
}
}) || "npm";

return installedPackageManager;
};

export const getAppName = (appDir: string) => {
Expand Down