Giter Site home page Giter Site logo

execa's Introduction

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'
});

// pipe the child process stdout to the current stdout
execa('echo', ['unicorns']).stdout.pipe(process.stdout);

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.

Think of this as a mix of child_process.execFile and child_process.spawn.

Returns a child_process instance, which is enhanced to also be a 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.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.

reject

Type: boolean
Default: true

Setting this to false resolves the promise with the error instead of rejecting it.

cleanup

Type: boolean
Default: true

Keep track of the spawned process and kill it when the parent process exits.

License

MIT © Sindre Sorhus

execa's People

Contributors

sindresorhus avatar jamestalmage avatar kevva avatar strikeentco avatar dignifiedquire avatar lukehorvat avatar knisterpeter avatar migg24 avatar ntwb avatar

Watchers

Dan Levy avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.