forked from t3-oss/create-t3-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(node): update
getUserPkgManager
- Loading branch information
1 parent
cb8505e
commit e304fbd
Showing
1 changed file
with
29 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
}; |