Navigation Menu

Skip to content

Commit

Permalink
feat: add support for glob patterns like prettier (#57) (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
jantimon authored and azz committed Jan 17, 2019
1 parent 108344a commit 9efd7fd
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 1 deletion.
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)"` or `pretty-quick --pattern "**/*.js" --pattern "**/*.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",
"multimatch": "^3.0.0"
},
"scripts": {
"prepublishOnly": "yarn build",
Expand Down
41 changes: 41 additions & 0 deletions src/__tests__/scm-git.test.js
Expand Up @@ -158,6 +158,47 @@ 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('calls onWriteFile with changed files for an array of globstar patterns', () => {
const onWriteFile = jest.fn();
mockGitFs();
prettyQuick('root', {
pattern: ['**/*.foo', '**/*.md', '**/*.bar'],
since: 'banana',
onWriteFile,
});
expect(onWriteFile.mock.calls).toEqual([['./bar.md']]);
});

test('writes formatted files to disk', () => {
Expand Down
40 changes: 40 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 All @@ -127,6 +156,17 @@ describe('with hg', () => {
expect(fs.readFileSync('/bar.md', 'utf8')).toEqual('formatted:# foo');
});

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

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

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

export default pattern => {
// Match everything if no pattern was given
if (typeof pattern !== 'string' && !Array.isArray(pattern)) {
return () => true;
}
const patterns = Array.isArray(pattern) ? pattern : [pattern];
return file =>
multimatch(path.normalize(file), patterns, { dot: true }).length > 0;
};
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
9 changes: 9 additions & 0 deletions yarn.lock
Expand Up @@ -3130,6 +3130,15 @@ ms@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"

multimatch@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/multimatch/-/multimatch-3.0.0.tgz#0e2534cc6bc238d9ab67e1b9cd5fcd85a6dbf70b"
dependencies:
array-differ "^2.0.3"
array-union "^1.0.2"
arrify "^1.0.1"
minimatch "^3.0.4"

mute-stream@0.0.7:
version "0.0.7"
resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.7.tgz#3075ce93bc21b8fab43e1bc4da7e8115ed1e7bab"
Expand Down

0 comments on commit 9efd7fd

Please sign in to comment.