execa
Advanced tools
Comparing version 0.3.0 to 0.4.0
97
index.js
@@ -7,2 +7,3 @@ 'use strict'; | ||
var npmRunPath = require('npm-run-path'); | ||
var isStream = require('is-stream'); | ||
var pathKey = require('path-key')(); | ||
@@ -49,2 +50,16 @@ var TEN_MEBIBYTE = 1024 * 1024 * 10; | ||
function handleInput(spawned, opts) { | ||
var input = opts.input; | ||
if (input === null || input === undefined) { | ||
return; | ||
} | ||
if (isStream(input)) { | ||
input.pipe(spawned.stdin); | ||
} else { | ||
spawned.stdin.end(input); | ||
} | ||
} | ||
function handleOutput(opts, val) { | ||
@@ -58,2 +73,25 @@ if (opts.stripEof) { | ||
function handleShell(fn, cmd, opts) { | ||
var file; | ||
var args; | ||
opts = objectAssign({}, opts); | ||
if (process.platform === 'win32') { | ||
opts.__winShell = true; | ||
file = process.env.comspec || 'cmd.exe'; | ||
args = ['/s', '/c', '"' + cmd + '"']; | ||
opts.windowsVerbatimArguments = true; | ||
} else { | ||
file = '/bin/sh'; | ||
args = ['-c', cmd]; | ||
} | ||
if (opts.shell) { | ||
file = opts.shell; | ||
} | ||
return fn(file, args, opts); | ||
} | ||
module.exports = function (cmd, args, opts) { | ||
@@ -81,31 +119,28 @@ var spawned; | ||
crossSpawnAsync._enoent.hookChildProcess(spawned, parsed); | ||
handleInput(spawned, parsed.opts); | ||
}); | ||
promise.kill = spawned.kill.bind(spawned); | ||
promise.pid = spawned.pid; | ||
spawned.then = promise.then.bind(promise); | ||
spawned.catch = promise.catch.bind(promise); | ||
return promise; | ||
return spawned; | ||
}; | ||
module.exports.shell = function (cmd, opts) { | ||
var file; | ||
var args; | ||
module.exports.stdout = function () { | ||
// TODO: set `stderr: 'ignore'` when that option is implemented | ||
return module.exports.apply(null, arguments).then(function (x) { | ||
return x.stdout; | ||
}); | ||
}; | ||
opts = objectAssign({}, opts); | ||
module.exports.stderr = function () { | ||
// TODO: set `stdout: 'ignore'` when that option is implemented | ||
return module.exports.apply(null, arguments).then(function (x) { | ||
return x.stderr; | ||
}); | ||
}; | ||
if (process.platform === 'win32') { | ||
opts.__winShell = true; | ||
file = process.env.comspec || 'cmd.exe'; | ||
args = ['/s', '/c', '"' + cmd + '"']; | ||
opts.windowsVerbatimArguments = true; | ||
} else { | ||
file = '/bin/sh'; | ||
args = ['-c', cmd]; | ||
} | ||
if (opts.shell) { | ||
file = opts.shell; | ||
} | ||
return module.exports(file, args, opts); | ||
module.exports.shell = function (cmd, opts) { | ||
return handleShell(module.exports, cmd, opts); | ||
}; | ||
@@ -124,5 +159,19 @@ | ||
var parsed = handleArgs(cmd, args, opts); | ||
var out = childProcess.execFileSync(parsed.cmd, parsed.args, parsed.opts); | ||
return handleOutput(parsed.opts, out); | ||
if (isStream(parsed.opts.input)) { | ||
throw new TypeError('The `input` option cannot be a stream in sync mode'); | ||
} | ||
var result = childProcess.spawnSync(parsed.cmd, parsed.args, parsed.opts); | ||
if (parsed.opts.stripEof) { | ||
result.stdout = stripEof(result.stdout); | ||
result.stderr = stripEof(result.stderr); | ||
} | ||
return result; | ||
}; | ||
module.exports.shellSync = function (cmd, opts) { | ||
return handleShell(module.exports.sync, cmd, opts); | ||
}; |
{ | ||
"name": "execa", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "A better `child_process`", | ||
@@ -12,2 +12,9 @@ "license": "MIT", | ||
}, | ||
"maintainers": [ | ||
{ | ||
"name": "James Talmage", | ||
"email": "james@talmage.io", | ||
"url": "github.com/jamestalmage" | ||
} | ||
], | ||
"engines": { | ||
@@ -42,2 +49,3 @@ "node": ">=0.12" | ||
"cross-spawn-async": "^2.1.1", | ||
"is-stream": "^1.1.0", | ||
"npm-run-path": "^1.0.0", | ||
@@ -44,0 +52,0 @@ "object-assign": "^4.0.1", |
@@ -64,6 +64,14 @@ # execa [![Build Status: Linux](https://travis-ci.org/sindresorhus/execa.svg?branch=master)](https://travis-ci.org/sindresorhus/execa) [![Build status: Windows](https://ci.appveyor.com/api/projects/status/x5ajamxtjtt93cqv/branch/master?svg=true)](https://ci.appveyor.com/project/sindresorhus/execa/branch/master) [![Coverage Status](https://coveralls.io/repos/github/sindresorhus/execa/badge.svg?branch=master)](https://coveralls.io/github/sindresorhus/execa?branch=master) | ||
Returns a promise for a result object with `stdout` and `stderr` properties. | ||
Returns a [`child_process` instance](https://nodejs.org/api/child_process.html#child_process_class_childprocess). | ||
The promise instance has a `pid` property (ID of the child process) and a `kill` method (for sending signals to the child process). | ||
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]) | ||
@@ -75,5 +83,5 @@ | ||
Returns a promise for a result object with `stdout` and `stderr` properties. | ||
Returns a [`child_process` instance](https://nodejs.org/api/child_process.html#child_process_class_childprocess). | ||
The promise instance has a `pid` property (ID of the child process) and a `kill` method (for sending signals to the child process). | ||
The `child_process` instance is enhanced to also be promise for a result object with `stdout` and `stderr` properties. | ||
@@ -90,6 +98,14 @@ ### execa.spawn(file, [arguments], [options]) | ||
Same options as [`child_process.execFileSync`](https://nodejs.org/api/child_process.html#child_process_child_process_execfile_file_args_options_callback), except that the default encoding is `utf8` instead of `buffer`. | ||
Same options as [`child_process.execFileSync`](https://nodejs.org/api/child_process.html#child_process_child_process_execfilesync_file_args_options), except the default encoding is `utf8` instead of `buffer`. | ||
Returns `stdout`. | ||
Returns the same result object as [`child_process.spawnSync`](https://nodejs.org/api/child_process.html#child_process_child_process_spawnsync_command_args_options). | ||
### execa.shellSync(file, [options]) | ||
Execute a command synchronously through the system shell. | ||
Same options as [`child_process.execSync`](https://nodejs.org/api/child_process.html#child_process_child_process_execsync_command_options), except the default encoding is `utf8` instead of `buffer`. | ||
Returns the same result object as [`child_process.spawnSync`](https://nodejs.org/api/child_process.html#child_process_child_process_spawnsync_command_args_options). | ||
### options | ||
@@ -114,5 +130,12 @@ | ||
#### input | ||
Type: `string` `Buffer` `ReadableStream` | ||
Write some input to the `stdin` of your binary.<br> | ||
Streams are not allowed when using the synchronous methods. | ||
## License | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) |
10449
137
138
6
+ Addedis-stream@^1.1.0
+ Addedis-stream@1.1.0(transitive)