Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add --bail option which fails on fix (#52) #60

Merged
merged 1 commit into from Jan 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
21 changes: 13 additions & 8 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 prettyQuickResult = 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 All @@ -44,12 +42,19 @@ prettyQuick(
})
);

if (success) {
if (prettyQuickResult.success) {
console.log('✅ Everything is awesome!');
} else {
console.log(
'✗ Partially staged files were fixed up.' +
` ${chalk.bold('Please update stage before committing')}.`
);
if (prettyQuickResult.errors.indexOf('PARTIALLY_STAGED_FILE') !== -1) {
console.log(
'✗ Partially staged files were fixed up.' +
` ${chalk.bold('Please update stage before committing')}.`
);
}
if (prettyQuickResult.errors.indexOf('BAIL_ON_WRITE') !== -1) {
console.log(
'✗ File had to be prettified and prettyQuick was set to bail mode.'
);
}
process.exit(1); // ensure git hooks abort
}
27 changes: 27 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).toEqual({ errors: [], success: 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).toEqual({ errors: ['BAIL_ON_WRITE'], success: false });
});

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

Expand Down Expand Up @@ -255,6 +271,17 @@ 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).toEqual({
errors: ['PARTIALLY_STAGED_FILE'],
success: 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).toEqual({ errors: [], success: 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).toEqual({ errors: ['BAIL_ON_WRITE'], success: false });
});

test('calls onWriteFile with changed files for an array of globstar patterns', () => {
const onWriteFile = jest.fn();
mockHgFs();
Expand Down
12 changes: 12 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,29 @@ 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 {
success: failReasons.size === 0,
errors: Array.from(failReasons),
};
};