Skip to content

Commit a6057ce

Browse files
smablyazz
authored andcommitted
feat: add --check CLI option (#64)
* chore: rename "write"/"format" to "process" * feat: add --check CLI option * docs: add --check to README * fix: squash bugs and clean up code * fix: rename onProcessFile back to onExamineFile
1 parent 0d510f7 commit a6057ce

File tree

5 files changed

+63
-30
lines changed

5 files changed

+63
-30
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ Outputs the name of each file right before it is proccessed. This can be useful
101101

102102
Prevent `git commit` if any files are fixed.
103103

104+
### `--check`
105+
106+
Check that files are correctly formatted, but don't format them. This is useful on CI to verify that all changed files in the current branch were correctly formatted.
107+
104108
<!-- Undocumented = Unsupported :D
105109
106110
### `--config`

bin/pretty-quick.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ const prettyQuickResult = prettyQuick(
3636
console.log(`✍️ Fixing up ${chalk.bold(file)}.`);
3737
},
3838

39+
onCheckFile: (file, isFormatted) => {
40+
if (!isFormatted) {
41+
console.log(`⛔️ Check failed: ${chalk.bold(file)}`);
42+
}
43+
},
44+
3945
onExamineFile: file => {
4046
console.log(`🔍 Examining ${chalk.bold(file)}.`);
4147
},
@@ -56,5 +62,10 @@ if (prettyQuickResult.success) {
5662
'✗ File had to be prettified and prettyQuick was set to bail mode.'
5763
);
5864
}
65+
if (prettyQuickResult.errors.indexOf('CHECK_FAILED') !== -1) {
66+
console.log(
67+
'✗ Code style issues found in the above file(s). Forgot to run Prettier?'
68+
);
69+
}
5970
process.exit(1); // ensure git hooks abort
6071
}

src/formatFiles.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/index.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import scms from './scms';
2-
import formatFiles from './formatFiles';
2+
import processFiles from './processFiles';
33
import createIgnorer from './createIgnorer';
44
import createMatcher from './createMatcher';
55
import isSupportedExtension from './isSupportedExtension';
@@ -14,12 +14,14 @@ export default (
1414
restage = true,
1515
branch,
1616
bail,
17+
check,
1718
verbose,
1819
onFoundSinceRevision,
1920
onFoundChangedFiles,
2021
onPartiallyStagedFile,
21-
onWriteFile,
2222
onExamineFile,
23+
onCheckFile,
24+
onWriteFile,
2325
} = {}
2426
) => {
2527
const scm = scms(currentDirectory);
@@ -60,7 +62,8 @@ export default (
6062

6163
const failReasons = new Set();
6264

63-
formatFiles(directory, changedFiles, {
65+
processFiles(directory, changedFiles, {
66+
check,
6467
config,
6568
onWriteFile: file => {
6669
onWriteFile && onWriteFile(file);
@@ -76,6 +79,12 @@ export default (
7679
}
7780
}
7881
},
82+
onCheckFile: (file, isFormatted) => {
83+
onCheckFile && onCheckFile(file, isFormatted);
84+
if (!isFormatted) {
85+
failReasons.add('CHECK_FAILED');
86+
}
87+
},
7988
onExamineFile: verbose && onExamineFile,
8089
});
8190

src/processFiles.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { readFileSync, writeFileSync } from 'fs';
2+
import * as prettier from 'prettier';
3+
import { join } from 'path';
4+
5+
export default (
6+
directory,
7+
files,
8+
{ check, config, onExamineFile, onCheckFile, onWriteFile } = {}
9+
) => {
10+
for (const relative of files) {
11+
onExamineFile && onExamineFile(relative);
12+
const file = join(directory, relative);
13+
const options = Object.assign(
14+
{},
15+
prettier.resolveConfig.sync(file, {
16+
config,
17+
editorconfig: true,
18+
}),
19+
{ filepath: file }
20+
);
21+
const input = readFileSync(file, 'utf8');
22+
23+
if (check) {
24+
const isFormatted = prettier.check(input, options);
25+
onCheckFile && onCheckFile(relative, isFormatted);
26+
continue;
27+
}
28+
29+
const output = prettier.format(input, options);
30+
31+
if (output !== input) {
32+
writeFileSync(file, output);
33+
onWriteFile && onWriteFile(relative);
34+
}
35+
}
36+
};

0 commit comments

Comments
 (0)