Skip to content

Commit

Permalink
feat: add support for glob patterns like prettier (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
jantimon committed Jan 16, 2019
1 parent 108344a commit 6866f17
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -88,6 +88,11 @@ Use with the `--staged` flag to skip re-staging files after formatting.

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

### `--pattern`

Filters the files for the given [minimatch](https://github.com/isaacs/minimatch) pattern.
For example `pretty-quick --pattern "**/*.*(js|jsx)"`

### `--verbose`

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.
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -28,7 +28,8 @@
"execa": "^0.8.0",
"find-up": "^2.1.0",
"ignore": "^3.3.7",
"mri": "^1.1.0"
"mri": "^1.1.0",
"minimatch": "3.0.4"
},
"scripts": {
"prepublishOnly": "yarn build",
Expand Down
30 changes: 30 additions & 0 deletions src/__tests__/scm-git.test.js
Expand Up @@ -158,6 +158,36 @@ describe('with git', () => {

expect(onWriteFile).toHaveBeenCalledWith('./foo.js');
expect(onWriteFile).toHaveBeenCalledWith('./bar.md');
expect(onWriteFile.mock.calls.length).toBe(2);
});

test('calls onWriteFile with changed files for the given pattern', () => {
const onWriteFile = jest.fn();
mockGitFs();
prettyQuick('root', { pattern: '*.md', since: 'banana', onWriteFile });
expect(onWriteFile.mock.calls).toEqual([['./bar.md']]);
});

test('calls onWriteFile with changed files for the given globstar pattern', () => {
const onWriteFile = jest.fn();
mockGitFs();
prettyQuick('root', {
pattern: '**/*.md',
since: 'banana',
onWriteFile,
});
expect(onWriteFile.mock.calls).toEqual([['./bar.md']]);
});

test('calls onWriteFile with changed files for the given extglob pattern', () => {
const onWriteFile = jest.fn();
mockGitFs();
prettyQuick('root', {
pattern: '*.*(md|foo|bar)',
since: 'banana',
onWriteFile,
});
expect(onWriteFile.mock.calls).toEqual([['./bar.md']]);
});

test('writes formatted files to disk', () => {
Expand Down
29 changes: 29 additions & 0 deletions src/__tests__/scm-hg.test.js
Expand Up @@ -116,6 +116,35 @@ describe('with hg', () => {
expect(onWriteFile).toHaveBeenCalledWith('./bar.md');
});

test('calls onWriteFile with changed files for the given pattern', () => {
const onWriteFile = jest.fn();
mockHgFs();
prettyQuick('root', { pattern: '*.md', since: 'banana', onWriteFile });
expect(onWriteFile.mock.calls).toEqual([['./bar.md']]);
});

test('calls onWriteFile with changed files for the given globstar pattern', () => {
const onWriteFile = jest.fn();
mockHgFs();
prettyQuick('root', {
pattern: '**/*.md',
since: 'banana',
onWriteFile,
});
expect(onWriteFile.mock.calls).toEqual([['./bar.md']]);
});

test('calls onWriteFile with changed files for the given extglob pattern', () => {
const onWriteFile = jest.fn();
mockHgFs();
prettyQuick('root', {
pattern: '*.*(md|foo|bar)',
since: 'banana',
onWriteFile,
});
expect(onWriteFile.mock.calls).toEqual([['./bar.md']]);
});

test('writes formatted files to disk', () => {
const onWriteFile = jest.fn();

Expand Down
9 changes: 9 additions & 0 deletions src/createMatcher.js
@@ -0,0 +1,9 @@
import minimatch from 'minimatch';
const path = require('path');

export default pattern => {
if (typeof pattern !== 'string') {
return () => true;
}
return file => minimatch(path.normalize(file), pattern, { dot: true });
};
4 changes: 4 additions & 0 deletions src/index.js
@@ -1,6 +1,7 @@
import scms from './scms';
import formatFiles from './formatFiles';
import createIgnorer from './createIgnorer';
import createMatcher from './createMatcher';
import isSupportedExtension from './isSupportedExtension';

export default (
Expand All @@ -9,6 +10,7 @@ export default (
config,
since,
staged,
pattern,
restage = true,
branch,
verbose,
Expand Down Expand Up @@ -38,13 +40,15 @@ export default (
const changedFiles = scm
.getChangedFiles(directory, revision, staged)
.filter(isSupportedExtension)
.filter(createMatcher(pattern))
.filter(rootIgnorer)
.filter(cwdIgnorer);

const unstagedFiles = staged
? scm
.getUnstagedChangedFiles(directory, revision)
.filter(isSupportedExtension)
.filter(createMatcher(pattern))
.filter(rootIgnorer)
.filter(cwdIgnorer)
: [];
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Expand Up @@ -3090,7 +3090,7 @@ mimic-response@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/mimic-response/-/mimic-response-1.0.0.tgz#df3d3652a73fded6b9b0b24146e6fd052353458e"

minimatch@^3.0.0, minimatch@^3.0.2, minimatch@^3.0.3, minimatch@^3.0.4:
minimatch@3.0.4, minimatch@^3.0.0, minimatch@^3.0.2, minimatch@^3.0.3, minimatch@^3.0.4:
version "3.0.4"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083"
dependencies:
Expand Down

0 comments on commit 6866f17

Please sign in to comment.