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
Provenance
- Commit
06f21c6- Tree
d2a3cf6- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
f6db264
5 files changed+39 added−176 removed
modified:index.d.ts+5 added-84 removed
64 unmodified lines65 66 /**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.68 69+ 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.73+74 @default false75 */76 readonly shell?: boolean | string;77 315 unmodified lines393 stderr(file: string, options?: execa.Options): Promise<string>;394 stderr(file: string, options?: execa.Options<null>): Promise<Buffer>;395 396 /**392- Execute a command through the system shell.393-394- Prefer `execa()` whenever possible, as it's both faster and safer.395-396- @param command - The command to execute.397- @returns A [`child_process` instance](https://nodejs.org/api/child_process.html#child_process_class_childprocess).398-399- @example400- ```401- import execa from 'execa';402-403- (async => {404- // Run a shell command405- const {stdout} = await execa.shell('echo unicorns');406- //=> 'unicorns'407-408- // 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>;435-436- /**397 Execute a file synchronously.398 399 This method throws an `Error` if the command fails.400 15 unmodified lines416 sync(417 file: string,418 options?: execa.SyncOptions<null>419 ): execa.ExecaSyncReturnValue<Buffer>;460-461- /**462- Execute a command synchronously through the system shell.463-464- This method throws an `Error` if the command fails.465-466- @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).468-469- @example470- ```471- import execa from 'execa';472-473- 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 };421 422 export = execa;modified:index.js-8 removed
125 unmodified lines126 127 return value;128 }129 130-function handleShell(fn, command, options) {131- return fn(command, {...options, shell: true});132-}133-130 function makeAllStream(spawned) {131 if (!spawned.stdout && !spawned.stderr) {132 return;133 }307 unmodified lines441 const {stderr} = await execa(...args);442 return stderr;443 };444 449-module.exports.shell = (command, options) => handleShell(execa, command, options);450-445 module.exports.sync = (command, args, options) => {446 const parsed = handleArgs(command, args, options);447 const joinedCommand = joinCommand(command, args);448 27 unmodified lines476 command: joinedCommand,477 timedOut: false478 };479 };486-487-module.exports.shellSync = (command, options) => handleShell(execa.sync, command, options);modified:index.test-d.ts-17 removed
126 unmodified linesExpand all1271281291301311321331341351361371381391311321331345 unmodified lines140141142143153154155156157158159160126 unmodified linesExpand allexpectType<Buffer>(await execa.stderr('unicorns', {encoding: null}));expectType<string>(await execa.stderr('unicorns', ['foo'], {encoding: 'utf8'}));expectType<Buffer>(await execa.stderr('unicorns', ['foo'], {encoding: null}));
expectType<ExecaChildProcess<string>>(execa.shell('unicorns'));expectType<ExecaReturnValue<string>>(await execa.shell('unicorns'));expectType<ExecaReturnValue<string>>( await execa.shell('unicorns', {encoding: 'utf8'}));expectType<ExecaReturnValue<Buffer>>( await execa.shell('unicorns', {encoding: null}));
expectType<ExecaSyncReturnValue<string>>(execa.sync('unicorns'));expectType<ExecaSyncReturnValue<string>>( execa.sync('unicorns', {encoding: 'utf8'}));5 unmodified lines);expectType<ExecaSyncReturnValue<Buffer>>( execa.sync('unicorns', ['foo'], {encoding: null}));
expectType<ExecaSyncReturnValue<string>>(execa.shellSync('unicorns'));expectType<ExecaSyncReturnValue<string>>( execa.shellSync('unicorns', {encoding: 'utf8'}));expectType<ExecaSyncReturnValue<Buffer>>( execa.shellSync('unicorns', {encoding: null}));modified:readme.md+22 added-38 removed
50 unmodified lines51 // Pipe the child process stdout to the current stdout52 execa('echo', ['unicorns']).stdout.pipe(process.stdout);53 54 55- // Run a shell command56- const {stdout} = await execa.shell('echo unicorns');57- //=> 'unicorns'58-55 // 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 lines89 })();90 91 // Catching an error with a sync method92 try {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 lines120 Arguments should not be escaped nor quoted. Exception: inside `command`, spaces can be escaped with a backslash.121 122 Think of this as a mix of `child_process.execFile` and `child_process.spawn`.123 131-As 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.124+Unless 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.125 126 Returns a [`child_process` instance](https://nodejs.org/api/child_process.html#child_process_class_childprocess) which is enhanced to be a `Promise`.127 128 It exposes an additional `.all` stream, with `stdout` and `stderr` interleaved.11 unmodified lines140 ### execa.stderr(command, [options])141 142 Same as `execa()`, but returns only `stderr`.143 151-### execa.shell(command, [options])152-153-Execute a command through the system shell. Prefer `execa()` whenever possible, as it's faster, safer and more cross-platform.154-155-Returns a [`child_process` instance](https://nodejs.org/api/child_process.html#child_process_class_childprocess).156-157-The `child_process` instance is enhanced to also be promise for a result object with `stdout` and `stderr` properties.158-144 ### execa.sync(file, [arguments], [options])145 ### execa.sync(command, [options])146 147 Execute a file synchronously.3 unmodified lines151 It 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.152 153 This method throws an `Error` if the command fails.154 170-### execa.shellSync(command, [options])171-172-Execute a command synchronously through the system shell.173-174-Returns the same result object as [`child_process.spawnSync`](https://nodejs.org/api/child_process.html#child_process_child_process_spawnsync_command_args_options).175-155 ### options156 157 Type: `object`158 55 unmodified lines214 Default: `false`215 216 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.217 218+We 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.222+223 #### stripFinalNewline224 225 Type: `boolean`<br>226 Default: `true`127 unmodified linesmodified:test.js+12 added-29 removed
135 unmodified lines136 const {stdout} = await execa(' node fixtures/echo foo bar ');137 t.is(stdout, 'foo\nbar');138 });139 140-test('execa.shell()', async t => {141- const {stdout} = await execa.shell('node fixtures/noop foo');142- t.is(stdout, 'foo');143-});144-140 test('execa.sync()', t => {141 const {stdout} = execa.sync('noop', ['foo']);142 t.is(stdout, 'foo');143 });15 unmodified lines159 t.is(typeof error.stdout, 'string');160 t.is(typeof error.stderr, 'string');161 });162 168-test('execa.shellSync()', t => {169- const {stdout} = execa.shellSync('node fixtures/noop foo');170- t.is(stdout, 'foo');171-});172-173-test('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-});178-179-test('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-});184-163 test('stripEof option (legacy)', async t => {164 const {stdout} = await execa('noop', ['foo'], {stripEof: false});165 t.is(stdout, 'foo\n');166 });322 unmodified lines489 test('cleanup false - SIGTERM', spawnAndKill, 'SIGTERM', 'false', exitIfWindows);490 test('cleanup true - SIGKILL', spawnAndKill, 'SIGKILL', 'true', exitIfWindows);491 test('cleanup false - SIGKILL', spawnAndKill, 'SIGKILL', 'false', exitIfWindows);492 515-test('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-});521-493 if (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 exiting32 unmodified lines529 'bar'530 ]);531 });532 533+test('can use `options.shell: true`', async t => {534+ const {stdout} = await execa('node fixtures/noop foo', {shell: true});535+ t.is(stdout, 'foo');536+});537+538+test('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+});544+545 test('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