Skip to content

Commit 054e239

Browse files
jantimonazz
authored andcommitted
feat: add --bail option which fails on fix (#52) (#60)
1 parent 9efd7fd commit 054e239

File tree

5 files changed

+72
-8
lines changed

5 files changed

+72
-8
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ For example `pretty-quick --pattern "**/*.*(js|jsx)"` or `pretty-quick --pattern
9797

9898
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.
9999

100+
## `--bail`
101+
102+
Prevent `git commit` if any files are fixed.
103+
100104
<!-- Undocumented = Unsupported :D
101105
102106
### `--config`

bin/pretty-quick.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ const prettyQuick = require('..').default;
99

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

12-
let success = true;
13-
prettyQuick(
12+
const prettyQuickResult = prettyQuick(
1413
process.cwd(),
1514
Object.assign({}, args, {
1615
onFoundSinceRevision: (scm, revision) => {
@@ -31,7 +30,6 @@ prettyQuick(
3130

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

3735
onWriteFile: file => {
@@ -44,12 +42,19 @@ prettyQuick(
4442
})
4543
);
4644

47-
if (success) {
45+
if (prettyQuickResult.success) {
4846
console.log('✅ Everything is awesome!');
4947
} else {
50-
console.log(
51-
'✗ Partially staged files were fixed up.' +
52-
` ${chalk.bold('Please update stage before committing')}.`
53-
);
48+
if (prettyQuickResult.errors.indexOf('PARTIALLY_STAGED_FILE') !== -1) {
49+
console.log(
50+
'✗ Partially staged files were fixed up.' +
51+
` ${chalk.bold('Please update stage before committing')}.`
52+
);
53+
}
54+
if (prettyQuickResult.errors.indexOf('BAIL_ON_WRITE') !== -1) {
55+
console.log(
56+
'✗ File had to be prettified and prettyQuick was set to bail mode.'
57+
);
58+
}
5459
process.exit(1); // ensure git hooks abort
5560
}

src/__tests__/scm-git.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,22 @@ describe('with git', () => {
212212
expect(fs.readFileSync('/bar.md', 'utf8')).toEqual('formatted:# foo');
213213
});
214214

215+
test('succeeds if a file was changed and bail is not set', () => {
216+
mockGitFs();
217+
218+
const result = prettyQuick('root', { since: 'banana' });
219+
220+
expect(result).toEqual({ errors: [], success: true });
221+
});
222+
223+
test('fails if a file was changed and bail is set to true', () => {
224+
mockGitFs();
225+
226+
const result = prettyQuick('root', { since: 'banana', bail: true });
227+
228+
expect(result).toEqual({ errors: ['BAIL_ON_WRITE'], success: false });
229+
});
230+
215231
test('with --staged stages fully-staged files', () => {
216232
mockGitFs();
217233

@@ -255,6 +271,17 @@ describe('with git', () => {
255271
});
256272
});
257273

274+
test('with --staged returns false', () => {
275+
const additionalUnstaged = './raz.js\n'; // raz.js is partly staged and partly not staged
276+
mockGitFs(additionalUnstaged);
277+
278+
const result = prettyQuick('root', { since: 'banana', staged: true });
279+
expect(result).toEqual({
280+
errors: ['PARTIALLY_STAGED_FILE'],
281+
success: false,
282+
});
283+
});
284+
258285
test('without --staged does NOT stage changed files', () => {
259286
mockGitFs();
260287

src/__tests__/scm-hg.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,22 @@ describe('with hg', () => {
156156
expect(fs.readFileSync('/bar.md', 'utf8')).toEqual('formatted:# foo');
157157
});
158158

159+
test('succeeds if a file was changed and bail is not set', () => {
160+
mockHgFs();
161+
162+
const result = prettyQuick('root', { since: 'banana' });
163+
164+
expect(result).toEqual({ errors: [], success: true });
165+
});
166+
167+
test('fails if a file was changed and bail is set to true', () => {
168+
mockHgFs();
169+
170+
const result = prettyQuick('root', { since: 'banana', bail: true });
171+
172+
expect(result).toEqual({ errors: ['BAIL_ON_WRITE'], success: false });
173+
});
174+
159175
test('calls onWriteFile with changed files for an array of globstar patterns', () => {
160176
const onWriteFile = jest.fn();
161177
mockHgFs();

src/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export default (
1313
pattern,
1414
restage = true,
1515
branch,
16+
bail,
1617
verbose,
1718
onFoundSinceRevision,
1819
onFoundChangedFiles,
@@ -57,18 +58,29 @@ export default (
5758

5859
onFoundChangedFiles && onFoundChangedFiles(changedFiles);
5960

61+
const failReasons = new Set();
62+
6063
formatFiles(directory, changedFiles, {
6164
config,
6265
onWriteFile: file => {
6366
onWriteFile && onWriteFile(file);
67+
if (bail) {
68+
failReasons.add('BAIL_ON_WRITE');
69+
}
6470
if (staged && restage) {
6571
if (wasFullyStaged(file)) {
6672
scm.stageFile(directory, file);
6773
} else {
6874
onPartiallyStagedFile && onPartiallyStagedFile(file);
75+
failReasons.add('PARTIALLY_STAGED_FILE');
6976
}
7077
}
7178
},
7279
onExamineFile: verbose && onExamineFile,
7380
});
81+
82+
return {
83+
success: failReasons.size === 0,
84+
errors: Array.from(failReasons),
85+
};
7486
};

0 commit comments

Comments
 (0)