Coder Social home page Coder Social logo

Ideas? about execa HOT 10 CLOSED

sindresorhus avatar sindresorhus commented on September 24, 2024
Ideas?

from execa.

Comments (10)

sindresorhus avatar sindresorhus commented on September 24, 2024 3

@revelt Either execa(commandApp, commandAttributes, {stdio: 'inherit'}) or use the --color flag to force colors.

from execa.

callumlocke avatar callumlocke commented on September 24, 2024

I often want stdio: 'inherit' behaviour (which is normally only an option with cp.spawn() or execa.spawn(), I think) but with the simple promise-returning API of execa().

The stdio: 'inherit' option lets you watch logs coming from the child process live and incrementally (and in colour!), which is what I want 50% of the time. But it means I have to use spawn, and therefore have to manually hook up event listeners to handle the child process ending or erroring.

I often find myself writing a wrapper like this:

function run(file, args, options = {}) {
  return new Promise((resolve, reject) => {
    const child = execa.spawn.call(
      null, file, args, Object.assign({ stdio: 'inherit' }, options)
    );

    child.on('error', reject);
    child.on('exit', code => {
      if (code === 0) resolve();
      else reject();
    });
  });
}

It would be great if execa() could introduce an option like this.

Even better would be if there was some nice way to visually distinguish the child logs as being from the child process, like prefixing each line with > or something (but still retaining ANSI colour codes in the child logs).


EDIT

Just thought of another way of approaching this, maybe the solution is simply to add a new promise-returning method to spawned child process objects:

const child = execa.spawn(...);
const {stdio, stderr} = await child.exited();

I guess this would give the most flexibility, as you still get access to a ChildProcess instance (in case you need to send a signal to it, or something) but you also get the convenience of a promise-based way to monitor its completion.

Maybe the .exited() method (not sure about the name) could even take an argument to set the expected exit code, e.g. .exited(0) would mean any non-zero exit code causes a rejection.

from execa.

sindresorhus avatar sindresorhus commented on September 24, 2024

@callumlocke I really like the idea! I've had similar needs. Would you mind moving this into a new issue?

from execa.

zealoushacker avatar zealoushacker commented on September 24, 2024

IPC support, like in child_process.spawn's options.stdio would be awesome.

child-process-promise seems to do a good job of this, because it's a wrapper and supports the same options as the thing it's trying to improve, is that why you had chosen not to go that route?

from execa.

sindresorhus avatar sindresorhus commented on September 24, 2024

@zealoushacker Should already be supported, since we use child_process.spawn() internally, but needs to be tested and documented.

from execa.

tpetry avatar tpetry commented on September 24, 2024

Automatic handling of timeout by execa for executed processes would be nice. You define a max running time of e.g. 15000ms and the executed process has to finish within this time limit or will be killed. This is very useful for any application which has to fulfill QoS criterias.

from execa.

sindresorhus avatar sindresorhus commented on September 24, 2024

@tpetry execa already has a timeout option:

execa(file, [arguments], [options])

Same options as child_process.execFile.

from execa.

tpetry avatar tpetry commented on September 24, 2024

My fault, you're right. Didn't look up the options because i expected the same options as child_process.spawn

from execa.

revelt avatar revelt commented on September 24, 2024

Is there a way to make it so that the output of the commands run via execa is coloured in the terminal?

I'm running it this way:

execa(commandApp, commandAttributes).stdout.pipe(process.stdout)

but everything in the terminal (tested on both hyper.js and iTerm) is black and white.

To give some context, I'm using execa in fol to allow people to input a folder name, then let them pick a subfolder from a list, then place that choice into user's chosen terminal command. I need this, for example, to call gulp with a flag and a name of a subfolder, like gulp -t vip_template_64. That subfolder name varies, it's the different template I pick each time.

I got it wired and working, just the stdout output is uncoloured.

Thanks in advance.

from execa.

revelt avatar revelt commented on September 24, 2024

@sindresorhus it worked! thanks a lot.

from execa.

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.