Repository: prettier/prettier (JavaScript) — Bug Fix

Issue:
CLI: --list-different + --write = Exit status 1 if something was modified
It doesn't play very well with npm scripts:

```
d:\dev\foo>npm run format

> @ format d:\dev\foo
> prettier --write --list-different *.js

foo.js
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! @ format: `prettier --write --list-different *.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the @ format script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
```

Without `--list-different` though it doesn't behave like this.


Discussion:
related issues from searching `write "list-different"`:

* https://github.com/prettier/prettier/pull/1633
* https://github.com/prettier/prettier/issues/4129
I use yarn/npm to run prettier and I am getting the error "Exit status 1" or "Exit code 1" for pretty much any command, except for "--help" and "-v".

If I install and run prettier globally then there are no errors.
I started getting this error in a CI build on Github after I pushed a Typescript file in the repo that I had modified in VS Code running on Windows 8.1 x64. Could this be related to Windows and/or VS Code?
I also have issues with my CI when using `--list-different`:

![image](https://user-images.githubusercontent.com/469989/38780220-72c7fdc4-40d3-11e8-81dd-1f776be89f3d.png)

Without `--list-different` it works.

**EDIT:** I found out that it crashed because it didn't like the formatting of `package.json`. The output in the CLI was not very self explanatory. 🙈 
Guys, this issue is about the `--write` + `--list-different` combination. Please stop writing unrelated things here.
@thorn0 But it seems like the problem already shows up when only using `--list-different`, so errors coming from using `--list-different` AND `--write` might be just based on that.
Getting same error when running `prettier --list-different`.
npm ERR! code ELIFECYCLE

Prettier: 1.11.1
Node: v8.9.4
npm: 5.7.1

Edit: Appropriate behaviour according to [--list-different documentation](https://prettier.io/docs/en/cli.html#list-different)
@bennyn `--list-different` without `--write` is a check. [It checks that the files are formatted properly](https://prettier.io/docs/en/cli.html#list-different) and lists the ones that aren't. It's totally expected that it fails with the exit code 1 when it finds something (`package.json` in your case).

With `--write` however, Prettier immediately reformats the files rectifying all the formatting problems, so this exit code 1 makes very little sense in that case. Meanwhile, `--write` is too verbose without `--list-different`. That's why I'd like to use them together. And that's what this issue is about.
Sorry @thorn0 for this misunderstanding - everything works as expected in the documentation 😄

I'd like an exit code `1` when something is changed so I can use it in git hooks.
@aMoniker From my reading of this issue, the current behavior of exiting with `1` if `--list-different` is passed, but `--write` isn’t will remain.
@j-f1 I'm using `--write` and `--list-different` to write & get an exit code of `1` when something is written, which ensures my git hook is aborted. Can I rely on this, or will there be a different method to ensure an exit `1` when using `--write`?
Why do you want to abort the commit if the necessary changes to fix the formatting have been made automatically?
It's running on a `prepush` hook rather than on a `precommit` hook, so I'd like the changes to be written, and give the user the chance to look them over, make a separate commit for them, then `git push` again.

Doing it on `git commit` is nice too, but it breaks the line-by-line commit functionality of tools like GitHub Desktop.

Resolving patch:
diff --git a/src/cli/util.js b/src/cli/util.js
index cb69ac23384b..bfbcc7efec51 100644
--- a/src/cli/util.js
+++ b/src/cli/util.js
@@ -146,8 +146,8 @@ function listDifferent(context, input, options, filename) {
     if (!prettier.check(input, options)) {
       if (!context.argv["write"]) {
         context.logger.log(filename);
+        process.exitCode = 1;
       }
-      process.exitCode = 1;
     }
   } catch (error) {
     context.logger.error(error.message);
@@ -498,7 +498,9 @@ function formatFiles(context) {
 
     if (context.argv["list-different"] && isDifferent) {
       context.logger.log(filename);
-      process.exitCode = 1;
+      if (!context.argv["write"]) {
+        process.exitCode = 1;
+      }
     }
 
     if (context.argv["write"]) {


Test patch:
diff --git a/tests_integration/__tests__/infer-parser.js b/tests_integration/__tests__/infer-parser.js
index da2d673fb1e0..de4ded99fae0 100644
--- a/tests_integration/__tests__/infer-parser.js
+++ b/tests_integration/__tests__/infer-parser.js
@@ -113,7 +113,7 @@ describe("--write and --list-different with unknown path and no parser", () => {
 
   describe("multiple files", () => {
     runPrettier("cli/infer-parser/", ["--list-different", "--write", "*"]).test(
-      { status: 1 }
+      { status: 0 }
     );
   });
 });
diff --git a/tests_integration/__tests__/piped-output.js b/tests_integration/__tests__/piped-output.js
index 92dabaf32c6c..b77262eff4a9 100644
--- a/tests_integration/__tests__/piped-output.js
+++ b/tests_integration/__tests__/piped-output.js
@@ -8,7 +8,7 @@ describe("output with --list-different + unformatted differs when piped", () =>
     ["--write", "--list-different", "--no-color", "unformatted.js"],
     { stdoutIsTTY: true }
   ).test({
-    status: 1
+    status: 0
   });
 
   const result1 = runPrettier(
@@ -16,7 +16,7 @@ describe("output with --list-different + unformatted differs when piped", () =>
     ["--write", "--list-different", "--no-color", "unformatted.js"],
     { stdoutIsTTY: false }
   ).test({
-    status: 1
+    status: 0
   });
 
   expect(result0.stdout.length).toBeGreaterThan(result1.stdout.length);