Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(exec): support shell interpreter #4328

Merged
merged 6 commits into from
Feb 13, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 21 additions & 7 deletions packages/plugin-commands-script-runners/src/exec.ts
Expand Up @@ -19,17 +19,21 @@ import {

export const shorthands = {
parallel: runShorthands.parallel,
c: '--shell-mode',
}

export const commandNames = ['exec']

export function rcOptionsTypes () {
BlackHole1 marked this conversation as resolved.
Show resolved Hide resolved
return pick([
'bail',
'sort',
'unsafe-perm',
'workspace-concurrency',
], types)
return {
...pick([
'bail',
'sort',
'unsafe-perm',
'workspace-concurrency',
], types),
'shell-mode': Boolean,
}
}

export const cliOptionsTypes = () => ({
Expand All @@ -54,11 +58,19 @@ For options that may be used with `-r`, see "pnpm help recursive"',
name: '--recursive',
shortAlias: '-r',
},
{
description: 'If exist, runs file inside of a shell. \
Uses /bin/sh on UNIX and \\cmd.exe on Windows. \
A different shell can be specified as a string. \
BlackHole1 marked this conversation as resolved.
Show resolved Hide resolved
The shell should understand the -c switch on UNIX or /d /s /c on Windows.',
BlackHole1 marked this conversation as resolved.
Show resolved Hide resolved
name: '--shell-mode',
BlackHole1 marked this conversation as resolved.
Show resolved Hide resolved
shortAlias: '-c',
},
],
},
],
url: docsUrl('exec'),
usages: ['pnpm [-r] exec <command> [args...]'],
usages: ['pnpm [-r] [--shell-mode] exec <command> [args...]'],
})
}

Expand All @@ -70,6 +82,7 @@ export async function handler (
reverse?: boolean
sort?: boolean
workspaceConcurrency?: number
shellMode?: boolean
} & Pick<Config, 'extraBinPaths' | 'lockfileDir' | 'dir' | 'userAgent' | 'recursive' | 'workspaceDir'>,
params: string[]
) {
Expand Down Expand Up @@ -134,6 +147,7 @@ export async function handler (
cwd: prefix,
env,
stdio: 'inherit',
shell: opts.shellMode ?? false,
})
result.passes++
} catch (err: any) { // eslint-disable-line
Expand Down
27 changes: 27 additions & 0 deletions packages/plugin-commands-script-runners/test/exec.e2e.ts
Expand Up @@ -414,6 +414,33 @@ test('pnpm exec outside of projects', async () => {
expect(outputs).toStrictEqual([])
})

test('pnpm exec shell mode', async () => {
prepareEmpty()

await exec.handler({
...DEFAULT_OPTS,
dir: process.cwd(),
recursive: false,
selectedProjectsGraph: {
[process.cwd()]: {
dependencies: [],
package: {
dir: process.cwd(),
writeProjectManifest: async () => {},
manifest: {
name: 'test_shell_mode',
},
},
},
},
shellMode: true,
}, ['echo', '$PNPM_PACKAGE_NAME > name.txt'])

const result = (await fs.readFile(path.resolve('name.txt'), 'utf8')).trim()

expect(result).toBe('test_shell_mode')
})

test('pnpm recursive exec works with PnP', async () => {
preparePackages([
{
Expand Down