Skip to content
Myr

Remove `execa.shell()` and `execa.shellSync()` (#219)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by Sindre Sorhus · parent f6db264 · Browse files · 5 files+39 added−176 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+39 added−176 removed

  • other:+39 added−176 removed

5 files+39 added−176 removed

modified:index.d.ts+5 added-84 removed
64 unmodified lines
6566 /**67 If `true`, runs `command` 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.6869 We recommend against using this option since it is:70 - not cross-platform, encouraging shell-specific syntax.71 - slower, because of the additional shell interpretation.72 - unsafe, potentially allowing command injection.7374 @default false75 */76 readonly shell?: boolean | string;77
315 unmodified lines
393 stderr(file: string, options?: execa.Options): Promise<string>;394 stderr(file: string, options?: execa.Options<null>): Promise<Buffer>;395396 /**392 Execute a command through the system shell.393394 Prefer `execa()` whenever possible, as it's both faster and safer.395396 @param command - The command to execute.397 @returns A [`child_process` instance](https://nodejs.org/api/child_process.html#child_process_class_childprocess).398399 @example400 ```401 import execa from 'execa';402403 (async => {404 // Run a shell command405 const {stdout} = await execa.shell('echo unicorns');406 //=> 'unicorns'407408 // Catching an error409 try {410 await execa.shell('exit 3');411 } catch (error) {412 console.log(error);413 //{414 // message: 'Command failed with exit code 3 (ESRCH): exit 3',415 // code: 3,416 // exitCode: 3,417 // exitCodeName: 'ESRCH',418 // stdout: '',419 // stderr: '',420 // all: '',421 // failed: true,422 // command: 'exit 3',423 // timedOut: false,424 // killed: false425 //}426 }427 })();428 ```429 */430 shell(command: string, options?: execa.Options): execa.ExecaChildProcess;431 shell(432 command: string,433 options?: execa.Options<null>434 ): execa.ExecaChildProcess<Buffer>;435436 /**397 Execute a file synchronously.398399 This method throws an `Error` if the command fails.400
15 unmodified lines
416 sync(417 file: string,418 options?: execa.SyncOptions<null>419 ): execa.ExecaSyncReturnValue<Buffer>;460461 /**462 Execute a command synchronously through the system shell.463464 This method throws an `Error` if the command fails.465466 @param command - The command to execute.467 @returns The same result object as [`child_process.spawnSync`](https://nodejs.org/api/child_process.html#child_process_child_process_spawnsync_command_args_options).468469 @example470 ```471 import execa from 'execa';472473 try {474 execa.shellSync('exit 3');475 } catch (error) {476 console.log(error);477 //{478 // message: 'Command failed with exit code 3 (ESRCH): exit 3',479 // code: 3,480 // exitCode: 3,481 // exitCodeName: 'ESRCH',482 // stdout: '',483 // stderr: '',484 // failed: true,485 // command: 'exit 3',486 // timedOut: false487 //}488 }489 ```490 */491 shellSync(492 command: string,493 options?: execa.Options494 ): execa.ExecaSyncReturnValue;495 shellSync(496 command: string,497 options?: execa.Options<null>498 ): execa.ExecaSyncReturnValue<Buffer>;420};421422export = execa;
modified:index.js-8 removed
125 unmodified lines
126127 return value;128}129130function handleShell(fn, command, options) {131 return fn(command, {...options, shell: true});132}133130function makeAllStream(spawned) {131 if (!spawned.stdout && !spawned.stderr) {132 return;133 }
307 unmodified lines
441 const {stderr} = await execa(...args);442 return stderr;443};444449module.exports.shell = (command, options) => handleShell(execa, command, options);450445module.exports.sync = (command, args, options) => {446 const parsed = handleArgs(command, args, options);447 const joinedCommand = joinCommand(command, args);448
27 unmodified lines
476 command: joinedCommand,477 timedOut: false478 };479};486487module.exports.shellSync = (command, options) => handleShell(execa.sync, command, options);
modified:index.test-d.ts-17 removed
modified:readme.md+22 added-38 removed
50 unmodified lines
51 // Pipe the child process stdout to the current stdout52 execa('echo', ['unicorns']).stdout.pipe(process.stdout);535455 // Run a shell command56 const {stdout} = await execa.shell('echo unicorns');57 //=> 'unicorns'5855 // Catching an error56 try {61 await execa.shell('exit 3');57 await execa('wrong command');58 } catch (error) {59 console.log(error);60 /*61 {66 message: 'Command failed with exit code 3 (ESRCH): exit 3',67 code: 3,68 exitCode: 3,69 exitCodeName: 'ESRCH',62 message: 'spawn wrong command ENOENT',63 errno: 'ENOENT',64 code: 'ENOENT',65 syscall: 'spawn wrong command',66 path: 'wrong command',67 killed: false,68 stdout: '',69 stderr: '',72 all: '',70 failed: true,74 command: 'exit 3',75 timedOut: false,76 killed: false71 signal: null,72 cmd: 'wrong command',73 timedOut: false74 }75 */76 }77
11 unmodified lines
89})();9091// Catching an error with a sync method92try {96 execa.shellSync('exit 3');93 execa.sync('wrong command');94} catch (error) {95 console.log(error);96 /*97 {101 message: 'Command failed with exit code 3 (ESRCH): exit 3',102 code: 3,103 exitCode: 3,104 exitCodeName: 'ESRCH',105 stdout: '',106 stderr: '',107 failed: true,108 command: 'exit 3',109 timedOut: false98 message: 'spawnSync wrong command ENOENT',99 errno: 'ENOENT',100 code: 'ENOENT',101 syscall: 'spawnSync wrong command',102 path: 'wrong command',103 }104 */105}106```
13 unmodified lines
120Arguments should not be escaped nor quoted. Exception: inside `command`, spaces can be escaped with a backslash.121122Think of this as a mix of `child_process.execFile` and `child_process.spawn`.123131As opposed to [`execa.shell()`](#execashellcommand-options), no shell interpreter (Bash, `cmd.exe`, etc.) is used, so shell features such as variables substitution (`echo $PATH`) are not allowed.124Unless the [`shell`](#shell) option is used, no shell interpreter (Bash, `cmd.exe`, etc.) is used, so shell features such as variables substitution (`echo $PATH`) are not allowed.125126Returns a [`child_process` instance](https://nodejs.org/api/child_process.html#child_process_class_childprocess) which is enhanced to be a `Promise`.127128It exposes an additional `.all` stream, with `stdout` and `stderr` interleaved.
11 unmodified lines
140### execa.stderr(command, [options])141142Same as `execa()`, but returns only `stderr`.143151### execa.shell(command, [options])152153Execute a command through the system shell. Prefer `execa()` whenever possible, as it's faster, safer and more cross-platform.154155Returns a [`child_process` instance](https://nodejs.org/api/child_process.html#child_process_class_childprocess).156157The `child_process` instance is enhanced to also be promise for a result object with `stdout` and `stderr` properties.158144### execa.sync(file, [arguments], [options])145### execa.sync(command, [options])146147Execute a file synchronously.
3 unmodified lines
151It does not have the `.all` property that `execa()` has because the [underlying synchronous implementation](https://nodejs.org/api/child_process.html#child_process_child_process_execfilesync_file_args_options) only returns `stdout` and `stderr` at the end of the execution, so they cannot be interleaved.152153This method throws an `Error` if the command fails.154170### execa.shellSync(command, [options])171172Execute a command synchronously through the system shell.173174Returns the same result object as [`child_process.spawnSync`](https://nodejs.org/api/child_process.html#child_process_child_process_spawnsync_command_args_options).175155### options156157Type: `object`158
55 unmodified lines
214Default: `false`215216If `true`, runs `command` 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.217218We recommend against using this option since it is:219- not cross-platform, encouraging shell-specific syntax.220- slower, because of the additional shell interpretation.221- unsafe, potentially allowing command injection.222223#### stripFinalNewline224225Type: `boolean`<br>226Default: `true`
127 unmodified lines
modified:test.js+12 added-29 removed
135 unmodified lines
136 const {stdout} = await execa(' node fixtures/echo foo bar ');137 t.is(stdout, 'foo\nbar');138});139140test('execa.shell()', async t => {141 const {stdout} = await execa.shell('node fixtures/noop foo');142 t.is(stdout, 'foo');143});144140test('execa.sync()', t => {141 const {stdout} = execa.sync('noop', ['foo']);142 t.is(stdout, 'foo');143});
15 unmodified lines
159 t.is(typeof error.stdout, 'string');160 t.is(typeof error.stderr, 'string');161});162168test('execa.shellSync()', t => {169 const {stdout} = execa.shellSync('node fixtures/noop foo');170 t.is(stdout, 'foo');171});172173test('execa.shellSync() includes stdout and stderr in errors for improved debugging', t => {174 t.throws(() => {175 execa.shellSync('node fixtures/error-message.js');176 }, {message: STDERR_STDOUT_REGEXP, code: 1});177});178179test('skip throwing when using reject option in execa.shellSync()', t => {180 const error = execa.shellSync('node fixtures/error-message.js', {reject: false});181 t.is(typeof error.stdout, 'string');182 t.is(typeof error.stderr, 'string');183});184163test('stripEof option (legacy)', async t => {164 const {stdout} = await execa('noop', ['foo'], {stripEof: false});165 t.is(stdout, 'foo\n');166});
322 unmodified lines
489test('cleanup false - SIGTERM', spawnAndKill, 'SIGTERM', 'false', exitIfWindows);490test('cleanup true - SIGKILL', spawnAndKill, 'SIGKILL', 'true', exitIfWindows);491test('cleanup false - SIGKILL', spawnAndKill, 'SIGKILL', 'false', exitIfWindows);492515test('execa.shell() supports the `shell` option', async t => {516 const {stdout} = await execa.shell('node fixtures/noop foo', {517 shell: process.platform === 'win32' ? 'cmd.exe' : '/bin/bash'518 });519 t.is(stdout, 'foo');520});521493if (process.platform !== 'win32') {494 test('write to fast-exit process', async t => {495 // Try-catch here is necessary, because this test is not 100% accurate496 // Sometimes process can manage to accept input before exiting
32 unmodified lines
529 'bar'530 ]);531});532533test('can use `options.shell: true`', async t => {534 const {stdout} = await execa('node fixtures/noop foo', {shell: true});535 t.is(stdout, 'foo');536});537538test('can use `options.shell: string`', async t => {539 const {stdout} = await execa('node fixtures/noop foo', {540 shell: process.platform === 'win32' ? 'cmd.exe' : '/bin/bash'541 });542 t.is(stdout, 'foo');543});544545test('use extend environment with `extendEnv: true` and `shell: true`', async t => {546 process.env.TEST = 'test';547 const command = process.platform === 'win32' ? 'echo %TEST%' : 'echo $TEST';548 const stdout = await execa.stdout(command, {shell: true, env: {}, extendEnv: true});
125 unmodified lines