Skip to content

Commit 0918f88

Browse files
feat: detect package manager from env (#15)
1 parent f86f7c4 commit 0918f88

File tree

4 files changed

+50
-8
lines changed

4 files changed

+50
-8
lines changed

src/pkg_manager.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,26 @@ class Pnpm implements PackageManager {
9696

9797
export type PkgManagerName = "npm" | "yarn" | "pnpm";
9898

99+
function getPkgManagerFromEnv(value: string): PkgManagerName | null {
100+
if (value.includes("pnpm/")) return "pnpm";
101+
else if (value.includes("yarn/")) return "yarn";
102+
else if (value.includes("npm/")) return "npm";
103+
else return null;
104+
}
105+
99106
export async function getPkgManager(
100107
cwd: string,
101108
pkgManagerName: PkgManagerName | null
102109
) {
103-
const { projectDir, pkgManagerName: foundPkgManager } = await findProjectDir(
110+
const envPkgManager = process.env.npm_config_user_agent;
111+
const fromEnv =
112+
envPkgManager !== undefined ? getPkgManagerFromEnv(envPkgManager) : null;
113+
114+
const { projectDir, pkgManagerName: fromLockfile } = await findProjectDir(
104115
cwd
105116
);
106117

107-
const result = pkgManagerName || foundPkgManager || "npm";
118+
const result = pkgManagerName || fromEnv || fromLockfile || "npm";
108119

109120
if (result === "yarn") {
110121
return new Yarn(projectDir);

src/utils.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,13 @@ export function prettyTime(diff: number) {
141141
return diff + "ms";
142142
}
143143

144-
export async function exec(cmd: string, args: string[], cwd: string) {
145-
const cp = spawn(cmd, args, { stdio: "inherit", cwd, shell: true });
144+
export async function exec(
145+
cmd: string,
146+
args: string[],
147+
cwd: string,
148+
env?: Record<string, string>
149+
) {
150+
const cp = spawn(cmd, args, { stdio: "inherit", cwd, shell: true, env });
146151

147152
return new Promise<void>((resolve) => {
148153
cp.on("exit", (code) => {

test/commands.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,24 @@ describe("install", () => {
135135
}
136136
);
137137
});
138+
139+
it("detect pnpm from npm_config_user_agent", async () => {
140+
await withTempEnv(
141+
["i", "@std/[email protected]"],
142+
async (_, dir) => {
143+
assert.ok(
144+
await isFile(path.join(dir, "pnpm-lock.yaml")),
145+
"pnpm lockfile not created"
146+
);
147+
},
148+
{
149+
env: {
150+
...process.env,
151+
npm_config_user_agent: `pnpm/8.14.3 ${process.env.npm_config_user_agent}`,
152+
},
153+
}
154+
);
155+
});
138156
});
139157

140158
describe("remove", () => {

test/test_utils.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,23 @@ export interface PkgJson {
1313
optionalDependencies?: Record<string, string>;
1414
}
1515

16-
export async function runJsr(args: string[], cwd: string) {
16+
export async function runJsr(
17+
args: string[],
18+
cwd: string,
19+
env: Record<string, string> = {
20+
...process.env,
21+
npm_config_user_agent: "npm/",
22+
}
23+
) {
1724
const bin = path.join(__dirname, "..", "src", "bin.ts");
1825
const tsNode = path.join(__dirname, "..", "node_modules", ".bin", "ts-node");
19-
return await exec(tsNode, [bin, ...args], cwd);
26+
return await exec(tsNode, [bin, ...args], cwd, env);
2027
}
2128

2229
export async function withTempEnv(
2330
args: string[],
24-
fn: (getPkgJson: () => Promise<PkgJson>, dir: string) => Promise<void>
31+
fn: (getPkgJson: () => Promise<PkgJson>, dir: string) => Promise<void>,
32+
options: { env?: Record<string, string> } = {}
2533
): Promise<void> {
2634
const dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "jsr-cli"));
2735

@@ -40,7 +48,7 @@ export async function withTempEnv(
4048
);
4149

4250
try {
43-
await runJsr(args, dir);
51+
await runJsr(args, dir, options.env);
4452
const pkgJson = async () =>
4553
JSON.parse(
4654
await fs.promises.readFile(path.join(dir, "package.json"), "utf-8")

0 commit comments

Comments
 (0)