Skip to content

Commit 9efd7fd

Browse files
jantimonazz
authored andcommitted
feat: add support for glob patterns like prettier (#57) (#59)
1 parent 108344a commit 9efd7fd

File tree

7 files changed

+113
-1
lines changed

7 files changed

+113
-1
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ Use with the `--staged` flag to skip re-staging files after formatting.
8888

8989
When not in `staged` pre-commit mode, use this flag to compare changes with the specified branch. Defaults to `master` (git) / `default` (hg) branch.
9090

91+
### `--pattern`
92+
93+
Filters the files for the given [minimatch](https://github.com/isaacs/minimatch) pattern.
94+
For example `pretty-quick --pattern "**/*.*(js|jsx)"` or `pretty-quick --pattern "**/*.js" --pattern "**/*.jsx"`
95+
9196
### `--verbose`
9297

9398
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.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"execa": "^0.8.0",
2929
"find-up": "^2.1.0",
3030
"ignore": "^3.3.7",
31-
"mri": "^1.1.0"
31+
"mri": "^1.1.0",
32+
"multimatch": "^3.0.0"
3233
},
3334
"scripts": {
3435
"prepublishOnly": "yarn build",

src/__tests__/scm-git.test.js

+41
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,47 @@ describe('with git', () => {
158158

159159
expect(onWriteFile).toHaveBeenCalledWith('./foo.js');
160160
expect(onWriteFile).toHaveBeenCalledWith('./bar.md');
161+
expect(onWriteFile.mock.calls.length).toBe(2);
162+
});
163+
164+
test('calls onWriteFile with changed files for the given pattern', () => {
165+
const onWriteFile = jest.fn();
166+
mockGitFs();
167+
prettyQuick('root', { pattern: '*.md', since: 'banana', onWriteFile });
168+
expect(onWriteFile.mock.calls).toEqual([['./bar.md']]);
169+
});
170+
171+
test('calls onWriteFile with changed files for the given globstar pattern', () => {
172+
const onWriteFile = jest.fn();
173+
mockGitFs();
174+
prettyQuick('root', {
175+
pattern: '**/*.md',
176+
since: 'banana',
177+
onWriteFile,
178+
});
179+
expect(onWriteFile.mock.calls).toEqual([['./bar.md']]);
180+
});
181+
182+
test('calls onWriteFile with changed files for the given extglob pattern', () => {
183+
const onWriteFile = jest.fn();
184+
mockGitFs();
185+
prettyQuick('root', {
186+
pattern: '*.*(md|foo|bar)',
187+
since: 'banana',
188+
onWriteFile,
189+
});
190+
expect(onWriteFile.mock.calls).toEqual([['./bar.md']]);
191+
});
192+
193+
test('calls onWriteFile with changed files for an array of globstar patterns', () => {
194+
const onWriteFile = jest.fn();
195+
mockGitFs();
196+
prettyQuick('root', {
197+
pattern: ['**/*.foo', '**/*.md', '**/*.bar'],
198+
since: 'banana',
199+
onWriteFile,
200+
});
201+
expect(onWriteFile.mock.calls).toEqual([['./bar.md']]);
161202
});
162203

163204
test('writes formatted files to disk', () => {

src/__tests__/scm-hg.test.js

+40
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,35 @@ describe('with hg', () => {
116116
expect(onWriteFile).toHaveBeenCalledWith('./bar.md');
117117
});
118118

119+
test('calls onWriteFile with changed files for the given pattern', () => {
120+
const onWriteFile = jest.fn();
121+
mockHgFs();
122+
prettyQuick('root', { pattern: '*.md', since: 'banana', onWriteFile });
123+
expect(onWriteFile.mock.calls).toEqual([['./bar.md']]);
124+
});
125+
126+
test('calls onWriteFile with changed files for the given globstar pattern', () => {
127+
const onWriteFile = jest.fn();
128+
mockHgFs();
129+
prettyQuick('root', {
130+
pattern: '**/*.md',
131+
since: 'banana',
132+
onWriteFile,
133+
});
134+
expect(onWriteFile.mock.calls).toEqual([['./bar.md']]);
135+
});
136+
137+
test('calls onWriteFile with changed files for the given extglob pattern', () => {
138+
const onWriteFile = jest.fn();
139+
mockHgFs();
140+
prettyQuick('root', {
141+
pattern: '*.*(md|foo|bar)',
142+
since: 'banana',
143+
onWriteFile,
144+
});
145+
expect(onWriteFile.mock.calls).toEqual([['./bar.md']]);
146+
});
147+
119148
test('writes formatted files to disk', () => {
120149
const onWriteFile = jest.fn();
121150

@@ -127,6 +156,17 @@ describe('with hg', () => {
127156
expect(fs.readFileSync('/bar.md', 'utf8')).toEqual('formatted:# foo');
128157
});
129158

159+
test('calls onWriteFile with changed files for an array of globstar patterns', () => {
160+
const onWriteFile = jest.fn();
161+
mockHgFs();
162+
prettyQuick('root', {
163+
pattern: ['**/*.foo', '**/*.md', '**/*.bar'],
164+
since: 'banana',
165+
onWriteFile,
166+
});
167+
expect(onWriteFile.mock.calls).toEqual([['./bar.md']]);
168+
});
169+
130170
test('without --staged does NOT stage changed files', () => {
131171
mockHgFs();
132172

src/createMatcher.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import multimatch from 'multimatch';
2+
const path = require('path');
3+
4+
export default pattern => {
5+
// Match everything if no pattern was given
6+
if (typeof pattern !== 'string' && !Array.isArray(pattern)) {
7+
return () => true;
8+
}
9+
const patterns = Array.isArray(pattern) ? pattern : [pattern];
10+
return file =>
11+
multimatch(path.normalize(file), patterns, { dot: true }).length > 0;
12+
};

src/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import scms from './scms';
22
import formatFiles from './formatFiles';
33
import createIgnorer from './createIgnorer';
4+
import createMatcher from './createMatcher';
45
import isSupportedExtension from './isSupportedExtension';
56

67
export default (
@@ -9,6 +10,7 @@ export default (
910
config,
1011
since,
1112
staged,
13+
pattern,
1214
restage = true,
1315
branch,
1416
verbose,
@@ -38,13 +40,15 @@ export default (
3840
const changedFiles = scm
3941
.getChangedFiles(directory, revision, staged)
4042
.filter(isSupportedExtension)
43+
.filter(createMatcher(pattern))
4144
.filter(rootIgnorer)
4245
.filter(cwdIgnorer);
4346

4447
const unstagedFiles = staged
4548
? scm
4649
.getUnstagedChangedFiles(directory, revision)
4750
.filter(isSupportedExtension)
51+
.filter(createMatcher(pattern))
4852
.filter(rootIgnorer)
4953
.filter(cwdIgnorer)
5054
: [];

yarn.lock

+9
Original file line numberDiff line numberDiff line change
@@ -3130,6 +3130,15 @@ [email protected]:
31303130
version "2.0.0"
31313131
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
31323132

3133+
multimatch@^3.0.0:
3134+
version "3.0.0"
3135+
resolved "https://registry.yarnpkg.com/multimatch/-/multimatch-3.0.0.tgz#0e2534cc6bc238d9ab67e1b9cd5fcd85a6dbf70b"
3136+
dependencies:
3137+
array-differ "^2.0.3"
3138+
array-union "^1.0.2"
3139+
arrify "^1.0.1"
3140+
minimatch "^3.0.4"
3141+
31333142
31343143
version "0.0.7"
31353144
resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.7.tgz#3075ce93bc21b8fab43e1bc4da7e8115ed1e7bab"

0 commit comments

Comments
 (0)