Installing dependencies.
This commit is contained in:
+22
@@ -0,0 +1,22 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014 Ben Drucker
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
spawn-error-forwarder [](https://travis-ci.org/bendrucker/spawn-error-forwarder)
|
||||
=====================
|
||||
|
||||
Emit errors on stdout stream for a spawned child process. Useful for capturing errors from a spawned process when you want the output from stdout.
|
||||
|
||||
## Setup
|
||||
```bash
|
||||
$ npm install spawn-error-forwarder
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
#### `fwd(child [, errFactory]` -> `child`
|
||||
|
||||
Buffers `child.stderr` output. If the spawned process exits with a code `> 0`, the buffered output of `child.stderr` is used to generate an error which is emitted on `child.stdout`. By default, the error message is the output of `child.stderr`. If you provide an `errFactory` function, it will be called with `code, stderr` where `code` is the child's exit code and `stderr` is string that contains the output of `child.stderr`. `errFactory` should return an `Error` to be emitted on `child.stdout`.
|
||||
|
||||
## Example
|
||||
|
||||
```js
|
||||
var fwd = require('spawn-error-forwarder');
|
||||
var spawn = require('child_process').spawn;
|
||||
var child = spawn('git', ['log', 'non-existent-path']);
|
||||
|
||||
fwd(child, function (code, stderr) {
|
||||
return new Error('git log exited with ' + code + ':\n\n' + stderr);
|
||||
});
|
||||
|
||||
child.stdout
|
||||
.on('error', console.error.bind(console))
|
||||
.pipe(process.stdout);
|
||||
```
|
||||
|
||||
We want to pipe the output of `git log` to `process.stdout` but since we're providing a path that doesn't exist git will exit with a non-zero code and we'll log its output with `console.error`.
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
{
|
||||
"name": "spawn-error-forwarder",
|
||||
"version": "1.0.0",
|
||||
"description": "Emit errors on stdout stream for a spawned child process",
|
||||
"main": "./src",
|
||||
"directories": {
|
||||
"test": "test"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "mocha"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/bendrucker/spawn-error-forwarder.git"
|
||||
},
|
||||
"keywords": [
|
||||
"child_processs",
|
||||
"spawn",
|
||||
"error",
|
||||
"stdout",
|
||||
"stderr"
|
||||
],
|
||||
"author": "Ben Drucker <bvdrucker@gmail.com> (http://bendrucker.me)",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/bendrucker/spawn-error-forwarder/issues"
|
||||
},
|
||||
"homepage": "https://github.com/bendrucker/spawn-error-forwarder",
|
||||
"devDependencies": {
|
||||
"chai": "~1.10.0"
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
'use strict';
|
||||
|
||||
function createErr (code, stderr) {
|
||||
return new Error(stderr);
|
||||
}
|
||||
|
||||
module.exports = function (child, errFactory) {
|
||||
errFactory = errFactory || createErr;
|
||||
var stderr = [];
|
||||
child.stderr.on('data', function (chunk) {
|
||||
stderr.push(chunk);
|
||||
});
|
||||
child.on('close', function (code) {
|
||||
if (code !== 0) {
|
||||
child.stdout.emit('error', errFactory(
|
||||
code,
|
||||
Buffer.concat(stderr).toString()
|
||||
));
|
||||
}
|
||||
});
|
||||
return child;
|
||||
};
|
||||
Reference in New Issue
Block a user