Make --list-different to work with --stdin (#2393)

* Add test for `prettier --stdin --list-different`

See https://github.com/prettier/prettier/issues/2389

* Pass test for `prettier --stdin --list-different`

This fixes https://github.com/prettier/prettier/issues/2389

* Deduplicate --list-different handling

* Exit-early to reduce nesting in listDifferent()

* Simplify condition in listDifferent()
master
Joseph Frazier 2017-07-04 14:41:46 -04:00 committed by GitHub
parent a0cadad222
commit e82a774568
2 changed files with 35 additions and 13 deletions

View File

@ -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);

View File

@ -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);
});