Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Colors from child scripts not being used in logged output #86

Closed
kentcdodds opened this issue Feb 6, 2017 · 10 comments
Closed

Colors from child scripts not being used in logged output #86

kentcdodds opened this issue Feb 6, 2017 · 10 comments

Comments

@kentcdodds
Copy link
Contributor

Thanks for this tool! I think it's great! I'm not getting colorized output when running concurrently on a script that is outputing stuff with chalk. Here's a repo that reproduces the issue: https://github.com/kentcdodds/chalk-concurrently-bug

output

It's pretty simple. color-red.js is:

#!/usr/bin/env node
const chalk = require('chalk')

console.log(chalk.red(process.argv.slice(2).join(' ')))

Any ideas? This is sort of a blocker for p-s dropping the --parallel option and recommending people just use concurrently. (more info).

@gustavohenke
Copy link
Member

Hi @kentcdodds! Thanks for the report.
After some research, I think that this is a standard behavior of Node itself.

See:

You can, however, use the --raw flag. Under the hoods, the behavior of --raw is to simply use stdio: "inherit", when spawning the child process.
I was able to always retain the colors when using it, even though the output is less cooler :)

@kentcdodds
Copy link
Contributor Author

Ah! I found this which explains what's going on quite nicely.

I think that a solution could be to simply add FORCE_COLOR = 1 to the env that you spawn the script with. Right around here we could probably change that to:

var spawnOpts = config.raw
  ? {stdio: 'inherit'}
  : {
      env: Object.assign({FORCE_COLOR: 1}, process.env)
    };

I'd be happy to make a PR to do this!

@gustavohenke
Copy link
Member

gustavohenke commented Feb 7, 2017

Hmm, yes, this makes more sense 😁
btw, I just checked the other issues, and... #49 is a duplicate, and this exact solution has been proposed there.

I think it's better if we don't force colors everytime.
Instead, we should test whether colors are supported by the terminal running concurrently in order to set FORCE_COLOR env.

This way we can avoid strange ASCII chars in some cases.

@okonet
Copy link

okonet commented Feb 7, 2017

You can check if it's TTY and only force colors in this case. This solved CI and similar cases:

https://github.com/okonet/lint-staged/blob/master/src/index.js#L18

@kentcdodds
Copy link
Contributor Author

We could just use https://www.npmjs.com/package/supports-color :) That's the same thing that chalk uses.

var spawnOpts = config.raw
  ? {stdio: 'inherit'}
  : {
      env: Object.assign({FORCE_COLOR: Boolean(supportsColor)}, process.env)
    };

@gustavohenke
Copy link
Member

Exactly what I thought 👍

If you have the time to send such PR, please go ahead.
Otherwise I'll do it later and push a new version.

@kentcdodds
Copy link
Contributor Author

Writing tests for this is pretty difficult and would require a bit of a rewrite of how tests work. So I'm not going to add tests to my changes.

@gustavohenke
Copy link
Member

Published v3.2.0!

@sinedied
Copy link

sinedied commented Apr 4, 2024

Using concurrently 8.8.2 I don't have any colors from my child scripts, unless I use --raw. Is this a regression?

@gustavohenke
Copy link
Member

Hi @sinedied, unlikely to be a regression, maybe it's something in your environment. But please open a new issue so that I can help diagnose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants