Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
smably authored and azz committed May 20, 2019
1 parent 0d510f7 commit a6057ce
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 30 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -101,6 +101,10 @@ Outputs the name of each file right before it is proccessed. This can be useful

Prevent `git commit` if any files are fixed.

### `--check`

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.

<!-- Undocumented = Unsupported :D
### `--config`
Expand Down
11 changes: 11 additions & 0 deletions bin/pretty-quick.js
Expand Up @@ -36,6 +36,12 @@ const prettyQuickResult = prettyQuick(
console.log(`✍️ Fixing up ${chalk.bold(file)}.`);
},

onCheckFile: (file, isFormatted) => {
if (!isFormatted) {
console.log(`⛔️ Check failed: ${chalk.bold(file)}`);
}
},

onExamineFile: file => {
console.log(`🔍 Examining ${chalk.bold(file)}.`);
},
Expand All @@ -56,5 +62,10 @@ if (prettyQuickResult.success) {
'✗ File had to be prettified and prettyQuick was set to bail mode.'
);
}
if (prettyQuickResult.errors.indexOf('CHECK_FAILED') !== -1) {
console.log(
'✗ Code style issues found in the above file(s). Forgot to run Prettier?'
);
}
process.exit(1); // ensure git hooks abort
}
27 changes: 0 additions & 27 deletions src/formatFiles.js

This file was deleted.

15 changes: 12 additions & 3 deletions src/index.js
@@ -1,5 +1,5 @@
import scms from './scms';
import formatFiles from './formatFiles';
import processFiles from './processFiles';
import createIgnorer from './createIgnorer';
import createMatcher from './createMatcher';
import isSupportedExtension from './isSupportedExtension';
Expand All @@ -14,12 +14,14 @@ export default (
restage = true,
branch,
bail,
check,
verbose,
onFoundSinceRevision,
onFoundChangedFiles,
onPartiallyStagedFile,
onWriteFile,
onExamineFile,
onCheckFile,
onWriteFile,
} = {}
) => {
const scm = scms(currentDirectory);
Expand Down Expand Up @@ -60,7 +62,8 @@ export default (

const failReasons = new Set();

formatFiles(directory, changedFiles, {
processFiles(directory, changedFiles, {
check,
config,
onWriteFile: file => {
onWriteFile && onWriteFile(file);
Expand All @@ -76,6 +79,12 @@ export default (
}
}
},
onCheckFile: (file, isFormatted) => {
onCheckFile && onCheckFile(file, isFormatted);
if (!isFormatted) {
failReasons.add('CHECK_FAILED');
}
},
onExamineFile: verbose && onExamineFile,
});

Expand Down
36 changes: 36 additions & 0 deletions src/processFiles.js
@@ -0,0 +1,36 @@
import { readFileSync, writeFileSync } from 'fs';
import * as prettier from 'prettier';
import { join } from 'path';

export default (
directory,
files,
{ check, config, onExamineFile, onCheckFile, onWriteFile } = {}
) => {
for (const relative of files) {
onExamineFile && onExamineFile(relative);
const file = join(directory, relative);
const options = Object.assign(
{},
prettier.resolveConfig.sync(file, {
config,
editorconfig: true,
}),
{ filepath: file }
);
const input = readFileSync(file, 'utf8');

if (check) {
const isFormatted = prettier.check(input, options);
onCheckFile && onCheckFile(relative, isFormatted);
continue;
}

const output = prettier.format(input, options);

if (output !== input) {
writeFileSync(file, output);
onWriteFile && onWriteFile(relative);
}
}
};

0 comments on commit a6057ce

Please sign in to comment.