Skip to content

Commit

Permalink
feat: Add --bail option which fails on fix (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
josejulio authored and jantimon committed Jan 17, 2019
1 parent 9efd7fd commit 0799764
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -97,6 +97,10 @@ For example `pretty-quick --pattern "**/*.*(js|jsx)"` or `pretty-quick --pattern

Outputs the name of each file right before it is proccessed. This can be useful if Prettier throws an error and you can't identify which file is causing the problem.

## `--bail`

Prevent `git commit` if any files are fixed.

<!-- Undocumented = Unsupported :D
### `--config`
Expand Down
4 changes: 1 addition & 3 deletions bin/pretty-quick.js
Expand Up @@ -9,8 +9,7 @@ const prettyQuick = require('..').default;

const args = mri(process.argv.slice(2));

let success = true;
prettyQuick(
const success = prettyQuick(
process.cwd(),
Object.assign({}, args, {
onFoundSinceRevision: (scm, revision) => {
Expand All @@ -31,7 +30,6 @@ prettyQuick(

onPartiallyStagedFile: file => {
console.log(`✗ Found ${chalk.bold('partially')} staged file ${file}.`);
success = false;
},

onWriteFile: file => {
Expand Down
24 changes: 24 additions & 0 deletions src/__tests__/scm-git.test.js
Expand Up @@ -212,6 +212,22 @@ describe('with git', () => {
expect(fs.readFileSync('/bar.md', 'utf8')).toEqual('formatted:# foo');
});

test('succeeds if a file was changed and bail is not set', () => {
mockGitFs();

const result = prettyQuick('root', { since: 'banana' });

expect(result).toBe(true);
});

test('fails if a file was changed and bail is set to true', () => {
mockGitFs();

const result = prettyQuick('root', { since: 'banana', bail: true });

expect(result).toBe(false);
});

test('with --staged stages fully-staged files', () => {
mockGitFs();

Expand Down Expand Up @@ -255,6 +271,14 @@ describe('with git', () => {
});
});

test('with --staged returns false', () => {
const additionalUnstaged = './raz.js\n'; // raz.js is partly staged and partly not staged
mockGitFs(additionalUnstaged);

const result = prettyQuick('root', { since: 'banana', staged: true });
expect(result).toBe(false);
});

test('without --staged does NOT stage changed files', () => {
mockGitFs();

Expand Down
16 changes: 16 additions & 0 deletions src/__tests__/scm-hg.test.js
Expand Up @@ -156,6 +156,22 @@ describe('with hg', () => {
expect(fs.readFileSync('/bar.md', 'utf8')).toEqual('formatted:# foo');
});

test('succeeds if a file was changed and bail is not set', () => {
mockHgFs();

const result = prettyQuick('root', { since: 'banana' });

expect(result).toBe(true);
});

test('fails if a file was changed and bail is set to true', () => {
mockHgFs();

const result = prettyQuick('root', { since: 'banana', bail: true });

expect(result).toBe(false);
});

test('calls onWriteFile with changed files for an array of globstar patterns', () => {
const onWriteFile = jest.fn();
mockHgFs();
Expand Down
9 changes: 9 additions & 0 deletions src/index.js
Expand Up @@ -13,6 +13,7 @@ export default (
pattern,
restage = true,
branch,
bail,
verbose,
onFoundSinceRevision,
onFoundChangedFiles,
Expand Down Expand Up @@ -57,18 +58,26 @@ export default (

onFoundChangedFiles && onFoundChangedFiles(changedFiles);

const failReasons = new Set();

formatFiles(directory, changedFiles, {
config,
onWriteFile: file => {
onWriteFile && onWriteFile(file);
if (bail) {
failReasons.add('BAIL_ON_WRITE');
}
if (staged && restage) {
if (wasFullyStaged(file)) {
scm.stageFile(directory, file);
} else {
onPartiallyStagedFile && onPartiallyStagedFile(file);
failReasons.add('PARTIALLY_STAGED_FILE');
}
}
},
onExamineFile: verbose && onExamineFile,
});

return failReasons.size === 0;
};

0 comments on commit 0799764

Please sign in to comment.