Comments (4)
This module is just a small wrapper around the native child_process module. As such, it just passes along the provided arguments directly to the native exec
, execFile
or spawn
methods. You can use the progress
event to access the native child process instance and from there you have access to child.stdin, child.stdout and child.stderr.
Since you have access to the input and output streams of the child process you should be able to do everything that you need. Please take a look at the docs for the child_process module and let me know if you are still stuck or if you think something can be improved in this module.
from child-process-promise.
Thanks for the pointer Patrick! Please forgive this asinine followup, I'm leaving this here just as a tiny example: I wanted to replicate this node-native functionality:
var spawn = require('child_process').spawn;
var s = "hi there!";
var wc = spawn('wc', []);
wc.stdin.write(new Buffer(s));
wc.stdin.end();
wc.stdout.on('data', function(data) {
console.log("wc said: " + data);
});
With child-process-promise, this is what that looks like:
var spawn = require('child-process-promise').spawn;
var s = "hi there!";
var p = spawn('wc', [], { capture: ["stdout"] })
.progress(function(childProcess) {
childProcess.stdin.write(new Buffer(s));
childProcess.stdin.end();
})
.then(function(results) { return results })
.fail(function(err) { console.error('[spawn] ERROR: ', err); });
// ...
p.then(function(results) { console.log("wc said: " + results.stdout); })
where the first chunk of code can be in, say, a module, and the last line in a consumer of that module.
🙇!
from child-process-promise.
Hey @jmls and @fasiha. Just throwing this out there, but what if we monkey-patched the returned Promise
object to include a childProcess
property as shown below:
var spawn = require('child-process-promise').spawn;
var s = "hi there!";
var p = spawn('wc', [], { capture: ["stdout"] })
p.childProcess.stdin.write(new Buffer(s));
p.childProcess.stdin.end();
// ...
p.then(function(results) { console.log("wc said: " + results.stdout); })
The progress
event is not part of the native Promise
specification (only supported by Q as far as I know). I think monkey-patching the promise object is the lesser of two evils. Any other ideas to simplify the code a bit?
from child-process-promise.
@patrick-steele-idem that would be much clearer!
(Embarrassed to admit it but I didn't know that progress
was a Q thing, I just used it blindly. But I had no problem converting the resulting promise to a bluebird promise using resolve
.)
from child-process-promise.
Related Issues (20)
- Kills processes that print to stderr HOT 2
- thanks
- node < 4 support PR? HOT 7
- Please attach a license file HOT 1
- Does not catch 'exit' event from spawn()
- missing stream output HOT 8
- EMFILE error - can be avoided?
- Warning: a promise was rejected with a non-error object HOT 3
- Export ChildProcessError
- getting a 'failed with code 1' HOT 1
- Improve docs for idiots like me (multiple args format to spawn).
- How to kill an exec process before it finishes? HOT 2
- How to use capture with async/await? HOT 1
- Allow maxBuffer option in exec
- Recommend using promisify-child-process instead? HOT 2
- How to do multiple commands? HOT 3
- Typescript definitions HOT 5
- `fs` and `child_process` not found HOT 2
- Problem with "dither:diffusion-amount=20%" in spawn
- Kill signal is not checked or logged hiding the actual cause of failure
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from child-process-promise.