Skip to content

Allow shell option to be a file URL #635

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

Merged
merged 1 commit into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ export type CommonOptions<EncodingType extends EncodingOption = DefaultEncodingO

@default false
*/
readonly shell?: boolean | string;
readonly shell?: boolean | string | URL;

/**
Specify the character encoding used to decode the `stdout` and `stderr` output. If set to `'buffer'`, then `stdout` and `stderr` will be a `Uint8Array` instead of a string.
Expand Down
11 changes: 6 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ const getEnv = ({env: envOption, extendEnv, preferLocal, localDir, execPath}) =>
return env;
};

const normalizeFileUrl = file => file instanceof URL ? fileURLToPath(file) : file;

const getFilePath = file => {
if (file instanceof URL) {
return fileURLToPath(file);
}
const fileString = normalizeFileUrl(file);

if (typeof file !== 'string') {
if (typeof fileString !== 'string') {
throw new TypeError('First argument must be a string or a file URL.');
}

return file;
return fileString;
};

const handleArguments = (file, args, options = {}) => {
Expand All @@ -63,6 +63,7 @@ const handleArguments = (file, args, options = {}) => {
windowsHide: true,
verbose: verboseDefault,
...options,
shell: normalizeFileUrl(options.shell),
};

options.env = getEnv(options);
Expand Down
1 change: 1 addition & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ execa('unicorns', {uid: 0});
execa('unicorns', {gid: 0});
execa('unicorns', {shell: true});
execa('unicorns', {shell: '/bin/sh'});
execa('unicorns', {shell: fileUrl});
execa('unicorns', {timeout: 1000});
execa('unicorns', {maxBuffer: 1000});
execa('unicorns', {killSignal: 'SIGTERM'});
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"path-key": "^4.0.0",
"tempfile": "^5.0.0",
"tsd": "^0.29.0",
"which": "^4.0.0",
Copy link
Collaborator Author

@ehmicky ehmicky Dec 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not a huge fan of that module (which is also used indirectly through node-cross-spawn), but it is only used for a single test.

"xo": "^0.56.0"
},
"c8": {
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ Sets the group identity of the process.

#### shell

Type: `boolean | string`\
Type: `boolean | string | URL`\
Default: `false`

If `true`, 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. The shell should understand the `-c` switch on UNIX or `/d /s /c` on Windows.
Expand Down
11 changes: 8 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {fileURLToPath, pathToFileURL} from 'node:url';
import test from 'ava';
import isRunning from 'is-running';
import getNode from 'get-node';
import which from 'which';
import {execa, execaSync, execaNode, $} from '../index.js';
import {setFixtureDir, PATH_KEY, FIXTURES_DIR_URL} from './helpers/fixtures-dir.js';

Expand Down Expand Up @@ -245,11 +246,15 @@ test('can use `options.shell: true`', async t => {
t.is(stdout, 'foo');
});

test('can use `options.shell: string`', async t => {
const shell = process.platform === 'win32' ? 'cmd.exe' : '/bin/bash';
const testShellPath = async (t, mapPath) => {
const shellPath = process.platform === 'win32' ? 'cmd.exe' : 'bash';
const shell = mapPath(await which(shellPath));
const {stdout} = await execa('node test/fixtures/noop.js foo', {shell});
t.is(stdout, 'foo');
});
};

test('can use `options.shell: string`', testShellPath, identity);
test('can use `options.shell: file URL`', testShellPath, pathToFileURL);

test('use extend environment with `extendEnv: true` and `shell: true`', async t => {
process.env.TEST = 'test';
Expand Down