Skip to content
Myr

Support single string input without using the `shell` option (#182)

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 9e5b976 · Browse files · 4 files+91 added−3 removed
This commit

State

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

Diff stats

4 files+91 added−3 removed

  • other:+91 added−3 removed

4 files+91 added−3 removed

added:fixtures/echo+3 added
1#!/usr/bin/env node2'use strict';3console.log(process.argv.slice(2).join('\n'))
modified:index.js+39 added-1 removed
13 unmodified lines
14const stdio = require('./lib/stdio');1516const TEN_MEGABYTES = 1000 * 1000 * 10;1718const SPACES_REGEXP = / +/g;1920// Allow spaces to be escaped by a backslash if not meant as a delimiter21function handleEscaping(tokens, token, index) {22 if (index === 0) {23 return [token];24 }2526 const previousToken = tokens[index - 1];2728 if (!previousToken.endsWith('\\')) {29 return [...tokens, token];30 }3132 return [...tokens.slice(0, index - 1), `${previousToken.slice(0, -1)} ${token}`];33}3435function parseCommand(command, args = []) {36 if (args.length !== 0) {37 throw new Error('Arguments cannot be inside `command` when also specified as an array of strings');38 }3940 const [file, ...extraArgs] = command41 .trim()42 .split(SPACES_REGEXP)43 .reduce(handleEscaping, []);44 return [file, extraArgs];45}4618function handleArgs(command, args, options) {47function handleArgs(command, args, options = {}) {48 if (args && !Array.isArray(args)) {49 options = args;50 args = [];51 }5253 if (!options.shell && command.includes(' ')) {54 [command, args] = parseCommand(command, args);55 }5657 const parsed = crossSpawn._parse(command, args, options);58 command = parsed.command;59 args = parsed.args;60 options = parsed.options;
438 unmodified lines
modified:readme.md+15 added-2 removed
14 unmodified lines
15- Higher max buffer. 10 MB instead of 200 KB.16- [Executes locally installed binaries by name.](#preferlocal)17- [Cleans up spawned processes when the parent process dies.](#cleanup)18- [Adds an `.all` property](#execafile-arguments-options) with interleaved output from `stdout` and `stderr`, similar to what the terminal sees. [*(Async only)*](#execasyncfile-arguments-options)19- [Can specify command and arguments as a single string without a shell](#execafile-arguments-options)202122## Install23
91 unmodified lines
115116## API117118### execa(file, [arguments], [options])119### execa(command, [options])120121Execute a file.122123Arguments can be specified in either:124 - `arguments`: `execa('echo', ['unicorns'])`.125 - `command`: `execa('echo unicorns')`.126127Arguments should not be escaped nor quoted. Exception: inside `command`, spaces can be escaped with a backslash.128129Think of this as a mix of `child_process.execFile` and `child_process.spawn`.130131As 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.132133Returns a [`child_process` instance](https://nodejs.org/api/child_process.html#child_process_class_childprocess) which is enhanced to be a `Promise`.134135It exposes an additional `.all` stream, with `stdout` and `stderr` interleaved.136
1 unmodified line
138139The promise result is an `Object` with `stdout`, `stderr` and `all` properties.140141### execa.stdout(file, [arguments], [options])142### execa.stdout(command, [options])143144Same as `execa()`, but returns only `stdout`.145146### execa.stderr(file, [arguments], [options])147### execa.stderr(command, [options])148149Same as `execa()`, but returns only `stderr`.150151### execa.shell(command, [options])152141Execute a command through the system shell. Prefer `execa()` whenever possible, as it's both faster and safer.153Execute 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.158159### execa.sync(file, [arguments], [options])160### execa.sync(command, [options])161162Execute a file synchronously.163164Returns the same result object as [`child_process.spawnSync`](https://nodejs.org/api/child_process.html#child_process_child_process_spawnsync_command_args_options).
1 unmodified line
166It 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.167168This method throws an `Error` if the command fails.169157### execa.shellSync(file, [options])170### 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).
195 unmodified lines
modified:test.js+34 added
99 unmodified lines
100 await execa('fixtures/noop-err', ['foo bar'], {stderr: fs.openSync(file, 'w')});101 t.is(fs.readFileSync(file, 'utf8'), 'foo bar\n');102});103104test('allow string arguments', async t => {105 const {stdout} = await execa('node fixtures/echo foo bar');106 t.is(stdout, 'foo\nbar');107});108109test('allow string arguments in synchronous mode', t => {110 const {stdout} = execa.sync('node fixtures/echo foo bar');111 t.is(stdout, 'foo\nbar');112});113114test('forbid string arguments together with array arguments', t => {115 t.throws(() => execa('node fixtures/echo foo bar', ['foo', 'bar']), /Arguments cannot be inside/);116});117118test('ignore consecutive spaces in string arguments', async t => {119 const {stdout} = await execa('node fixtures/echo foo bar');120 t.is(stdout, 'foo\nbar');121});122123test('escape other whitespaces in string arguments', async t => {124 const {stdout} = await execa('node fixtures/echo foo\tbar');125 t.is(stdout, 'foo\tbar');126});127128test('allow escaping spaces in string arguments', async t => {129 const {stdout} = await execa('node fixtures/echo foo\\ bar');130 t.is(stdout, 'foo bar');131});132133test('trim string arguments', async t => {134 const {stdout} = await execa(' node fixtures/echo foo bar ');135 t.is(stdout, 'foo\nbar');136});137138test('execa.shell()', async t => {139 const {stdout} = await execa.shell('node fixtures/noop foo');140 t.is(stdout, 'foo');141});
553 unmodified lines