Print the number of files that need formatting. (#12561)

Co-authored-by: fisker Cheung <lionkay@gmail.com>
This commit is contained in:
Harry Hopkinsonandfisker Cheung authored and GitHub committed 2022-04-07 09:47:13 +08:00
1 parent 3fd64b4f30
commit f98c197b66
10 files changed
+65 -11

No files matched your search

+21
View File
@@ -0,0 +1,21 @@
#### Print the number of files need change (#12561 by @Harry-Hopkinson)
```console
# Prettier stable
$ prettier src --check
Checking formatting...
[warn] src/fileA.js
[warn] Code style issues found in the above file(s). Forgot to run Prettier?
# Prettier main
$ prettier src --check
Checking formatting...
[warn] src/fileA.js
[warn] src/fileB.js
[warn] Code style issues found in 2 files. Forgot to run Prettier?
$ prettier src --check
Checking formatting...
[warn] src/fileA.js
[warn] Code style issues found in the above file. Forgot to run Prettier?
```
+1 -1
View File
@@ -69,7 +69,7 @@ Console output if some of the files require re-formatting:
Checking formatting...
[warn] src/fileA.js
[warn] src/fileB.js
[warn] Code style issues found in the above file(s). Forgot to run Prettier?
[warn] Code style issues found in 2 files. Forgot to run Prettier?
```
The command will return exit code `1` in the second case, which is helpful inside the CI pipelines.
+12 -2
View File
@@ -417,11 +417,21 @@ async function formatFiles(context) {
if (context.argv.check) {
if (numberOfUnformattedFilesFound === 0) {
context.logger.log("All matched files use Prettier code style!");
} else if (numberOfUnformattedFilesFound === 1) {
context.logger.warn(
context.argv.write
? "Code style issues fixed in the above file."
: "Code style issues found in the above file. Forgot to run Prettier?"
);
} else {
context.logger.warn(
context.argv.write
? "Code style issues fixed in the above file(s)."
: "Code style issues found in the above file(s). Forgot to run Prettier?"
? "Code style issues found in " +
numberOfUnformattedFilesFound +
" files."
: "Code style issues found in " +
numberOfUnformattedFilesFound +
" files. Forgot to run Prettier?"
);
}
}
@@ -1,14 +1,28 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`--checks should print the number of files that need formatting (stderr) 1`] = `
"[warn] unformatted.js
[warn] unformatted2.js
[warn] Code style issues found in 2 files. Forgot to run Prettier?
"
`;
exports[`--checks should print the number of files that need formatting (stdout) 1`] = `
"Checking formatting...
"
`;
exports[`--checks should print the number of files that need formatting (write) 1`] = `Array []`;
exports[`--checks works in CI just as in a non-TTY mode (stderr) 1`] = `
"[warn] unformatted.js
[warn] Code style issues found in the above file(s). Forgot to run Prettier?
[warn] Code style issues found in the above file. Forgot to run Prettier?
"
`;
exports[`--checks works in CI just as in a non-TTY mode (stderr) 2`] = `
"[warn] unformatted.js
[warn] Code style issues found in the above file(s). Forgot to run Prettier?
[warn] Code style issues found in the above file. Forgot to run Prettier?
"
`;
@@ -31,7 +31,7 @@ exports[`ignore-unknown alias (stdout) 1`] = `
exports[`ignore-unknown check (stderr) 1`] = `
"[warn] javascript.js
[warn] Code style issues found in the above file(s). Forgot to run Prettier?
[warn] Code style issues found in the above file. Forgot to run Prettier?
"
`;
@@ -3,7 +3,7 @@
exports[`--check with unknown path and no parser multiple files (stderr) 1`] = `
"[error] No parser could be inferred for file: FOO
[warn] foo.js
[warn] Code style issues found in the above file(s). Forgot to run Prettier?
[warn] Code style issues found in the above file. Forgot to run Prettier?
"
`;
@@ -36,7 +36,7 @@ exports[`--list-different with unknown path and no parser specific file (stderr)
exports[`--write and --check with unknown path and no parser multiple files (stderr) 1`] = `
"[error] No parser could be inferred for file: FOO
[warn] foo.js
[warn] Code style issues fixed in the above file(s).
[warn] Code style issues fixed in the above file.
"
`;
@@ -34,13 +34,13 @@ exports[`no file diffs with --list-different + formatted file (write) 2`] = `Arr
exports[`output with --check + unformatted differs when piped (stderr) 1`] = `
"[warn] unformatted.js
[warn] Code style issues fixed in the above file(s).
[warn] Code style issues fixed in the above file.
"
`;
exports[`output with --check + unformatted differs when piped (stderr) 2`] = `
"[warn] unformatted.js
[warn] Code style issues fixed in the above file(s).
[warn] Code style issues fixed in the above file.
"
`;
+8
View File
@@ -48,3 +48,11 @@ describe("--checks works in CI just as in a non-TTY mode", () => {
expect(await result0.stdout).toEqual(await result1.stdout);
});
});
describe("--checks should print the number of files that need formatting", () => {
runPrettier("cli/write", ["--check", "unformatted.js", "unformatted2.js"], {
input: "0",
}).test({
status: 1,
});
});
@@ -0,0 +1 @@
var x = 1;
+1 -1
View File
@@ -70,7 +70,7 @@ Console output if some of the files require re-formatting:
Checking formatting...
[warn] src/fileA.js
[warn] src/fileB.js
[warn] Code style issues found in the above file(s). Forgot to run Prettier?
[warn] Code style issues found in 2 files. Forgot to run Prettier?
```
The command will return exit code `1` in the second case, which is helpful inside the CI pipelines.