fix(dlx): should work with scoped packages (#3943)

close #3916
This commit is contained in:
Zoltan Kochan
2021-11-04 00:39:59 +02:00
committed by GitHub
parent 7da65bd7a9
commit 7d7f6417f8
4 changed files with 43 additions and 18 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/plugin-commands-script-runners": patch
---
`dlx` should be able to run scoped packages.

View File

@@ -68,7 +68,7 @@ export async function handler (
await execa('pnpm', pnpmArgs, {
stdio: 'inherit',
})
await execa(params[0], params.slice(1), {
await execa(scopeless(params[0]), params.slice(1), {
env: {
...process.env,
[PATH]: [
@@ -79,3 +79,10 @@ export async function handler (
stdio: 'inherit',
})
}
function scopeless (pkgName: string) {
if (pkgName.startsWith('@')) {
return pkgName.split('/')[1]
}
return pkgName
}

View File

@@ -0,0 +1,24 @@
import fs from 'fs'
import { dlx } from '@pnpm/plugin-commands-script-runners'
import { prepareEmpty } from '@pnpm/prepare'
test('dlx', async () => {
prepareEmpty()
await dlx.handler({}, ['shx', 'touch', 'foo'])
expect(fs.existsSync('foo')).toBeTruthy()
})
test('dlx --package <pkg1> [--package <pkg2>]', async () => {
prepareEmpty()
await dlx.handler({
package: [
'zkochan/for-testing-pnpm-dlx',
'is-positive',
],
}, ['foo'])
expect(fs.existsSync('foo')).toBeTruthy()
})

View File

@@ -1,24 +1,13 @@
import fs from 'fs'
import execa from 'execa'
import { dlx } from '@pnpm/plugin-commands-script-runners'
import { prepareEmpty } from '@pnpm/prepare'
test('dlx', async () => {
jest.mock('execa')
test('dlx should work with scoped packages', async () => {
prepareEmpty()
await dlx.handler({}, ['shx', 'touch', 'foo'])
await dlx.handler({}, ['@foo/bar'])
expect(fs.existsSync('foo')).toBeTruthy()
})
test('dlx --package <pkg1> [--package <pkg2>]', async () => {
prepareEmpty()
await dlx.handler({
package: [
'zkochan/for-testing-pnpm-dlx',
'is-positive',
],
}, ['foo'])
expect(fs.existsSync('foo')).toBeTruthy()
expect(execa).toBeCalledWith('bar', [], expect.anything())
})