Skip to content

bradparks/execa__sindresorhus_better_child_process

 
 

Repository files navigation

execa Build Status: Linux Build status: Windows Coverage Status

A better child_process

Why

Install

$ npm install --save execa

Usage

const execa = require('execa');

execa('echo', ['unicorns']).then(result => {
	console.log(result.stdout);
	//=> 'unicorns'
});

execa.shell('echo unicorns').then(result => {
	console.log(result.stdout);
	//=> 'unicorns'
});

// example of catching an error
execa.shell('exit 3').catch(error => {
	console.log(error);
	/*
	{
		message: 'Command failed: /bin/sh -c exit 3'
		killed: false,
		code: 3,
		signal: null,
		cmd: '/bin/sh -c exit 3',
		stdout: '',
		stderr: ''
	}
	*/
});

API

execa(file, [arguments], [options])

Execute a file.

Same options as child_process.execFile.

Returns a child_process instance.

The child_process instance is enhanced to also be promise for a result object with stdout and stderr properties.

execa.stdout(file, [arguments], [options])

Same as execa(), but returns only stdout.

execa.stderr(file, [arguments], [options])

Same as execa(), but returns only stderr.

execa.shell(command, [options])

Execute a command through the system shell. Prefer execa() whenever possible, as it's both faster and safer.

Same options as child_process.exec.

Returns a child_process instance.

The child_process instance is enhanced to also be promise for a result object with stdout and stderr properties.

execa.spawn(file, [arguments], [options])

Spawn a file.

Same API as child_process.spawn.

execa.sync(file, [arguments], [options])

Execute a file synchronously.

Same options as child_process.execFileSync, except the default encoding is utf8 instead of buffer.

Returns the same result object as child_process.spawnSync.

execa.shellSync(file, [options])

Execute a command synchronously through the system shell.

Same options as child_process.execSync, except the default encoding is utf8 instead of buffer.

Returns the same result object as child_process.spawnSync.

options

Additional options:

stripEof

Type: boolean
Default: true

Strip EOF (last newline) from the output.

preferLocal

Type: boolean
Default: true

Prefer locally installed binaries when looking for a binary to execute.
If you $ npm install foo, you can then execa('foo').

input

Type: string Buffer ReadableStream

Write some input to the stdin of your binary.
Streams are not allowed when using the synchronous methods.

License

MIT © Sindre Sorhus

About

A better `child_process`

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%