Skip to content

Don't use ANSI codes if stdout isn't a TTY#2903

Merged
azz merged 2 commits intoprettier:masterfrom
Narigo:add-silent-option
Oct 5, 2017
Merged

Don't use ANSI codes if stdout isn't a TTY#2903
azz merged 2 commits intoprettier:masterfrom
Narigo:add-silent-option

Conversation

@Narigo
Copy link
Copy Markdown
Contributor

@Narigo Narigo commented Sep 27, 2017

I've tried to pipe the output of --list-differences into xargs git add, but due to the progress information, the stdout is not really usable.

This commit could fix that by suppressing output information. I think it might make sense to change process.stdout/console.log to process.stderr/console.error in case of errors, but I did not want to put multiple things into one PR.

Doing yarn did not work for me as it currently cannot find the Git commit of postcss-values-parser referenced in the package.json. I've installed the latest version of it to get it to run, but this is probably the cause why the css_numbers/jsfmt.spec.js test fails for me locally.

Edit: Forgot yarn lint initially, which is now fixed :)

@lydell
Copy link
Copy Markdown
Member

lydell commented Sep 27, 2017

About the yarn install error – sorry, that was by bad. It should work now again. See #2627 (comment)

@lydell
Copy link
Copy Markdown
Member

lydell commented Sep 28, 2017

What if we printed the progress information to stderr instead?

@suchipi
Copy link
Copy Markdown
Member

suchipi commented Sep 28, 2017

I'm concerned that printing to stderr might flag the run as an error in some CI servers...

Also, I personally would prefer the name --no-progress over --silent

@lydell
Copy link
Copy Markdown
Member

lydell commented Sep 28, 2017

Warnings are usually printed to stderr, and I have not seen CI failures because of warnings. As far as I know, only the exit code matters.

@Narigo
Copy link
Copy Markdown
Contributor Author

Narigo commented Sep 28, 2017

--no-progress is better, yes. Could also be --quiet. Just tell me what you prefer, if you think adding this feature makes sense at all and I will change it.

@azz
Copy link
Copy Markdown
Member

azz commented Sep 29, 2017

Can we use process.stdout.isTTY instead of a new option?

@Narigo
Copy link
Copy Markdown
Contributor Author

Narigo commented Sep 29, 2017

Will change it to process.stdout.isTTY and remove the option.

@Narigo
Copy link
Copy Markdown
Contributor Author

Narigo commented Sep 29, 2017

Should be updated now and use process.stdout.isTTY.

Copy link
Copy Markdown
Member

@lydell lydell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I made a few small comments, though.


const runPrettier = require("../runPrettier");

test("output with --list-different + unformated differs when piped", () => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: "formated" → "formatted"

There are more instances of this below, including some file names.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've copied the texts from the write.js tests. Shall I rename the files in there as well or do you want to just have the string typos fixed and the file changes in another PR?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Let's do it in another PR (if you feel like it).

exports[`no file diffs with --list-different + formated file 2`] = `Array []`;

exports[`output with --list-different + unformated differs when piped 1`] = `
"unformated.jsunformated.js
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add --no-color to these tests? We've had CI failures because of this before. (Also, the tests become more readable.)

Copy link
Copy Markdown
Member

@lydell lydell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

exports[`no file diffs with --list-different + formatted file 2`] = `Array []`;

exports[`output with --list-different + unformatted differs when piped 1`] = `
"unformated.jsunformated.js
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought this was a color code. Oh well, --no-color doesn't hurt anyway. I guess it's from moving the cursor.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and that's exactly the problem I had when piping the output: The next command in the pipe complained about that... :)

@Narigo
Copy link
Copy Markdown
Contributor Author

Narigo commented Sep 29, 2017

I'd rename formated.js -> formatted.js and unformated.js -> unformatted.js, but I'd like to wait until this is merged. Otherwise I'd have to change the other things first, rebase here and ... I admit I just finished the renaming on this branch and am now too lazy to do it the other way around... ;)


process.chdir(normalizeDir(dir));
process.stdin.isTTY = !!options.isTTY;
process.stdout.isTTY = !!options.stdoutIsTTY;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can just use the existing isTTY here; I don't know of any real-world situations where stdin is a tty and stdout isn't (or vice-versa)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://nodejs.org/api/process.html

$ node -p "Boolean(process.stdin.isTTY)"
true
$ echo "foo" | node -p "Boolean(process.stdin.isTTY)"
false
$ node -p "Boolean(process.stdout.isTTY)"
true
$ node -p "Boolean(process.stdout.isTTY)" | cat
false

I think it is correct to use process.stdout.isTTY here.

@mitermayer
Copy link
Copy Markdown
Member

Would this issue also solve something like this prettier/vim-prettier#59?

Having issues now with prettier 1.7.1 since we are printting the message

--parser with value postcss is deprecated. Prettier now treats it as: --parser=css.

Sad :(

@azz
Copy link
Copy Markdown
Member

azz commented Sep 30, 2017

Would this issue also solve something like this prettier/vim-prettier#59?

I don't think so. That warning should be printed with console.warn which should go to stderr. @ikatyang any ideas?

@ikatyang
Copy link
Copy Markdown
Member

@mitermayer

If vim-prettier somehow can't understand the difference between stdout/stderr, I'd recommend to write a wrapper script to execute prettier so that you can do whatever you want, e.g. hide warnings.

const child_process = require("child_process");
const pathToPrettierCli = "node_modules/.bin/prettier";

const result = child_process.spawnSync(pathToPrettierCli, ["--parser", "postcss"], {
  encoding: "utf8",
  input: ".test { display: none; }"
});

console.log('stdout', result.stdout);
console.log('stderr', result.stderr);
console.log('status', result.status);

@azz

Should we add some kind of logger system so that they can easily get what they want and we can also log some debug information so as to trace what the root cause is? e.g. --loglevel.

@azz
Copy link
Copy Markdown
Member

azz commented Sep 30, 2017

Sounds good to me.

@ikatyang ikatyang mentioned this pull request Sep 30, 2017
@azz azz changed the title Add --silent option Don't use ANSI codes if stdout isn't a TTY Oct 5, 2017
@azz azz merged commit 2011053 into prettier:master Oct 5, 2017
@azz
Copy link
Copy Markdown
Member

azz commented Oct 5, 2017

Renamed the PR and merged. Thanks!

@Narigo Narigo deleted the add-silent-option branch October 5, 2017 11:03
@azz
Copy link
Copy Markdown
Member

azz commented Oct 5, 2017

This broke this build.

https://travis-ci.org/prettier/prettier/jobs/283636656

Summary of all failing tests
FAIL tests_integration/__tests__/piped-output.js
  ● output with --list-different + unformatted differs when piped
    expect(value).toMatchSnapshot()
    
    Received value does not match stored snapshot 1.
    
    - "unformated.jsated.js
    "
    + undefined
      
      at Object.<anonymous>.test (tests_integration/__tests__/piped-output.js:18:26)
          at Promise (<anonymous>)
          at <anonymous>
  ● no file diffs with --list-different + formatted file
    expect(value).toMatchSnapshot()
    
    Received value does not match stored snapshot 1.
    
    - Array []
    + undefined
      
      at Object.<anonymous>.test (tests_integration/__tests__/piped-output.js:41:25)
          at Promise (<anonymous>)
          at <anonymous>
FAIL tests_integration/__tests__/write.js
  ● write file with --write + unformated file › (stdout)
    expect(value).toMatchSnapshot()
    
    Received value does not match stored snapshot 1.
    
    - Snapshot
    + Received
    
    - "unformated.js%s %dms
    + "%s %dms
      "
      
      at Object.test (tests_integration/runPrettier.js:106:25)
          at Promise (<anonymous>)
          at <anonymous>
  ● do not write file with --write + formated file › (stdout)
    expect(value).toMatchSnapshot()
    
    Received value does not match stored snapshot 1.
    
    - Snapshot
    + Received
    
    - "formated.js%s %dms
    + "%s %dms
      "
      
      at Object.test (tests_integration/runPrettier.js:106:25)
          at Promise (<anonymous>)
          at <anonymous>
  ● do not write file with --write + invalid file › (stdout)
    expect(value).toMatchSnapshot()
    
    Received value does not match stored snapshot 1.
    
    - Snapshot
    + Received
    
    - "invalid.js
      "
    + "
      
      at Object.test (tests_integration/runPrettier.js:106:25)
          at Promise (<anonymous>)
          at <anonymous>

@Narigo
Copy link
Copy Markdown
Contributor Author

Narigo commented Oct 5, 2017

I have no idea. After fetching again, I see that my branch diverged from yours. Probably some git issue?
Did the behaviour on --write change in the meantime? I've copied most of the tests from the write integration tests.

@azz
Copy link
Copy Markdown
Member

azz commented Oct 5, 2017

On mobile at the moment but maybe #2957 or #2951

@lock lock Bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Jan 19, 2019
@lock lock Bot locked as resolved and limited conversation to collaborators Jan 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants