From b3dfa3ba8af85fff396f8bc98a8d93a2f990b68e Mon Sep 17 00:00:00 2001 From: await-ovo <13152410380@163.com> Date: Tue, 27 Dec 2022 00:58:25 +0800 Subject: [PATCH] feat(plugin-commands-script-runners): add --shell-mode option for pnpm dlx (#5840) close #5679 --- .changeset/olive-readers-drop.md | 6 ++++++ exec/plugin-commands-script-runners/src/dlx.ts | 14 +++++++++++++- .../test/dlx.e2e.ts | 16 ++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 .changeset/olive-readers-drop.md diff --git a/.changeset/olive-readers-drop.md b/.changeset/olive-readers-drop.md new file mode 100644 index 00000000000..0ab01959af6 --- /dev/null +++ b/.changeset/olive-readers-drop.md @@ -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). diff --git a/exec/plugin-commands-script-runners/src/dlx.ts b/exec/plugin-commands-script-runners/src/dlx.ts index 2ad87ea3ba0..6411652d6d0 100644 --- a/exec/plugin-commands-script-runners/src/dlx.ts +++ b/exec/plugin-commands-script-runners/src/dlx.ts @@ -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 & 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, }) } diff --git a/exec/plugin-commands-script-runners/test/dlx.e2e.ts b/exec/plugin-commands-script-runners/test/dlx.e2e.ts index 2d61744caa2..bcb88ae85d6 100644 --- a/exec/plugin-commands-script-runners/test/dlx.e2e.ts +++ b/exec/plugin-commands-script-runners/test/dlx.e2e.ts @@ -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() +})