Skip to content

Commit

Permalink
fix: the dlx and create commands should set npm_config_user_agent
Browse files Browse the repository at this point in the history
close #3985
  • Loading branch information
zkochan committed Feb 10, 2022
1 parent 7686cc7 commit e57f4c1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .changeset/thirty-owls-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@pnpm/plugin-commands-script-runners": patch
"pnpm": patch
---

The `pnpm dlx` and `pnpm create` commands should set the `npm_config_user_agent` env variable.
26 changes: 18 additions & 8 deletions packages/plugin-commands-script-runners/src/dlx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function help () {
export async function handler (
opts: {
package?: string[]
} & Pick<Config, 'reporter'>,
} & Pick<Config, 'reporter' | 'userAgent'>,
params: string[]
) {
const prefix = path.join(fs.realpathSync(os.tmpdir()), `dlx-${process.pid.toString()}`)
Expand All @@ -69,17 +69,27 @@ export async function handler (
stdio: 'inherit',
})
await execa(versionless(scopeless(params[0])), params.slice(1), {
env: {
...process.env,
[PATH]: [
bins,
process.env[PATH],
].join(path.delimiter),
},
env: makeEnv({ userAgent: opts.userAgent, prependPath: bins }),
stdio: 'inherit',
})
}

function makeEnv (
opts: {
userAgent?: string
prependPath: string
}
) {
return {
...process.env,
npm_config_user_agent: opts.userAgent ?? 'pnpm',
[PATH]: [
opts.prependPath,
process.env[PATH],
].join(path.delimiter),
}
}

function scopeless (pkgName: string) {
if (pkgName.startsWith('@')) {
return pkgName.split('/')[1]
Expand Down
9 changes: 7 additions & 2 deletions packages/plugin-commands-script-runners/test/dlx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ beforeEach((execa as jest.Mock).mockClear)

test('dlx should work with scoped packages', async () => {
prepareEmpty()
const userAgent = 'pnpm/0.0.0'

await dlx.handler({}, ['@foo/bar'])
await dlx.handler({ userAgent }, ['@foo/bar'])

expect(execa).toBeCalledWith('bar', [], expect.anything())
expect(execa).toBeCalledWith('bar', [], expect.objectContaining({
env: expect.objectContaining({
npm_config_user_agent: userAgent,
}),
}))
})

test('dlx should work with versioned packages', async () => {
Expand Down

0 comments on commit e57f4c1

Please sign in to comment.