diff --git a/bin/prettier.js b/bin/prettier.js index a51af4c81..53ec0ad54 100755 --- a/bin/prettier.js +++ b/bin/prettier.js @@ -252,6 +252,10 @@ if (argv["help"] || (!filepatterns.length && !stdin)) { if (stdin) { getStream(process.stdin).then(input => { + if (listDifferent(input, options, "(stdin)")) { + return; + } + try { writeOutput(format(input, options)); } catch (e) { @@ -279,19 +283,7 @@ if (stdin) { return; } - if (argv["list-different"]) { - if ( - !prettier.check( - input, - Object.assign({}, options, { filepath: filename }) - ) - ) { - if (!write) { - console.log(filename); - } - process.exitCode = 1; - } - } + listDifferent(input, options, filename); const start = Date.now(); @@ -350,6 +342,23 @@ if (stdin) { }); } +function listDifferent(input, options, filename) { + if (!argv["list-different"]) { + return; + } + + options = Object.assign({}, options, { filepath: filename }); + + if (!prettier.check(input, options)) { + if (!write) { + console.log(filename); + } + process.exitCode = 1; + } + + return true; +} + function writeOutput(result) { // Don't use `console.log` here since it adds an extra newline at the end. process.stdout.write(result.formatted); diff --git a/tests_integration/__tests__/list-different.js b/tests_integration/__tests__/list-different.js new file mode 100644 index 000000000..7ed2ee80e --- /dev/null +++ b/tests_integration/__tests__/list-different.js @@ -0,0 +1,13 @@ +"use strict"; + +const runPrettier = require("../runPrettier"); + +test("checks stdin with --list-different", () => { + const result = runPrettier("cli/with-shebang", ["--list-different"], { + input: "0" + }); + + expect(result.stdout).toEqual("(stdin)\n"); + expect(result.stderr).toEqual(""); + expect(result.status).not.toEqual(0); +});