Skip to content

Commit

Permalink
chore(node): update getUserPkgManager
Browse files Browse the repository at this point in the history
  • Loading branch information
virtuoushub committed Aug 27, 2022
1 parent cb8505e commit e304fbd
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions cli/src/utils/getUserPkgManager.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
export type PackageManager = "npm" | "pnpm" | "yarn";

/**
* @link https://stackoverflow.com/a/69301988
*
* @returns best guess at user's package manager
*/
export const getUserPkgManager: () => PackageManager = () => {
// This environment variable is set by npm and yarn but pnpm seems less consistent
const userAgent = process.env.npm_config_user_agent;
// This environment variable is set by npm and yarn but pnpm seems less consistent
const agent = process.env.npm_config_user_agent;

if (userAgent?.startsWith("yarn")) return "yarn";
if (userAgent?.startsWith("pnpm")) return "pnpm";
if (!agent) {
// This environment variable is set on Linux but I'm not sure about other OSes.
const parent = process.env._;

return "npm";
if (!parent) {
// No luck, assume npm
return "npm";
}

if (parent.endsWith("pnpx") || parent.endsWith("pnpm")) return "pnpm";
if (parent.endsWith("yarn")) return "yarn";

// Assume npm for anything else
return "npm";
}

const [program] = agent.split("/");

if (program === "yarn") return "yarn";
if (program === "pnpm") return "pnpm";

// Assume npm
return "npm";
};

0 comments on commit e304fbd

Please sign in to comment.