GithubHelp home page GithubHelp logo

Comments (4)

patrick-steele-idem avatar patrick-steele-idem commented on June 14, 2024

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.

fasiha avatar fasiha commented on June 14, 2024

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.

patrick-steele-idem avatar patrick-steele-idem commented on June 14, 2024

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.

fasiha avatar fasiha commented on June 14, 2024

@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)

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.