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

Programmatically disable colors in error messages #4118

Open
thorn0 opened this issue Mar 7, 2018 · 2 comments
Open

Programmatically disable colors in error messages #4118

thorn0 opened this issue Mar 7, 2018 · 2 comments
Labels
area:api Issues with Prettier's Application Programming Interface

Comments

@thorn0
Copy link
Member

thorn0 commented Mar 7, 2018

When prettier is used programmatically and throws an exception, this exception contains color codes, On Windows, if it isn't caught, the exception outputs in such a way that the color codes make it to the screen, so it looks like this:

d:\dev\asdf\node_modules\prettier\index.js:23135
      throw error;
      ^

SyntaxError: Invalid character. (1:8)
←[0m←[31m←[1m>←[22m←[39m←[90m 1 | ←[39mlodash ←[33m#←[39m(x as number )←[0m
←[0m ←[90m   | ←[39m       ←[31m←[1m^←[22m←[39m←[0m
    at createError (d:\dev\asdf\node_modules\prettier\parser-typescript.js:1:194)
    at.parser [as parse] (d:\dev\asdf\simple.js:7:19)
    ...

I found that I can pass the --no-color command line argument to my program to turn off those codes, but I'd like to hard code it somehow, and I didn't find a good way to do it.

It even might make sense to disable colors by default and only enable them when prettier is used from CLI.

@suchipi suchipi added the area:api Issues with Prettier's Application Programming Interface label Mar 8, 2018
@lydell
Copy link
Member

lydell commented Mar 8, 2018

You might be able to set the environment variable FORCE_COLOR=0 before require():ing anything. See: https://github.com/chalk/supports-color#info. Pretty ugly solution, but it might get you unblocked for now :)

@thorn0
Copy link
Member Author

thorn0 commented Mar 8, 2018

The worst part of these soluions is that they disable chalk globally for the whole program, not for prettier only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:api Issues with Prettier's Application Programming Interface
Projects
None yet
Development

No branches or pull requests

3 participants