对于未来的谷歌人,我最终使用了以下代码段。我用它来选择默认选项,但我仍然明确询问用户他们的包管理器偏好,比抱歉更安全。
function getPackageManager() {
// This environment variable is set by npm and yarn but pnpm seems less consistent
const agent = process.env.npm_config_user_agent;
if (!agent) {
// This environment variable is set on Linux but I'm not sure about other OSes.
const parent = process.env._;
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";
}