feat(plugin-commands-script-runners): add --shell-mode option for pnpm dlx (#5840)

close #5679
This commit is contained in:
await-ovo
2022-12-27 00:58:25 +08:00
committed by GitHub
parent 964d5ff278
commit b3dfa3ba8a
3 changed files with 35 additions and 1 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/plugin-commands-script-runners": minor
"pnpm": minor
---
The `pnpm dlx` command supports the `--shell-mode` (or `-c`) option. When used, the script is executed by a shell [#5679](https://github.com/pnpm/pnpm/issues/5679).

View File

@@ -16,11 +16,16 @@ import { makeEnv } from './makeEnv'
export const commandNames = ['dlx']
export const shorthands = {
c: '--shell-mode',
}
export function rcOptionsTypes () {
return {
...pick([
'use-node-version',
], types),
'shell-mode': Boolean,
}
}
@@ -35,12 +40,16 @@ export function help () {
descriptionLists: [
{
title: 'Options',
list: [
{
description: 'The package to install before running the command',
name: '--package',
},
{
description: 'Runs the script inside of a shell. Uses /bin/sh on UNIX and \\cmd.exe on Windows.',
name: '--shell-mode',
shortAlias: '-c',
},
],
},
OUTPUT_OPTIONS,
@@ -52,6 +61,7 @@ export function help () {
export type DlxCommandOptions = {
package?: string[]
shellMode?: boolean
} & Pick<Config, 'reporter' | 'userAgent'> & add.AddCommandOptions
export async function handler (
@@ -87,8 +97,10 @@ export async function handler (
? command
: await getBinName(modulesDir, await getPkgName(prefix))
await execa(binName, args, {
cwd: process.cwd(),
env,
stdio: 'inherit',
shell: opts.shellMode ?? false,
})
}

View File

@@ -90,3 +90,19 @@ test('dlx should fail when the package has no bins', async () => {
}, ['is-positive'])
).rejects.toThrow(/No binaries found in is-positive/)
})
test('dlx should work in shell mode', async () => {
prepareEmpty()
await dlx.handler({
...DEFAULT_OPTS,
dir: path.resolve('project'),
storeDir: path.resolve('store'),
package: [
'is-positive',
],
shellMode: true,
}, ['echo "some text" > foo'])
expect(fs.existsSync('foo')).toBeTruthy()
})