Skip to content

Commit

Permalink
feat: add support for glob patterns like prettier (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 132584f
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 1 deletion.
2 changes: 2 additions & 0 deletions bin/pretty-quick.js
Expand Up @@ -13,6 +13,8 @@ let success = true;
prettyQuick(
process.cwd(),
Object.assign({}, args, {
matchers: args._,

onFoundSinceRevision: (scm, revision) => {
console.log(
`🔍 Finding changed files since ${chalk.bold(
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
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', { matchers: ['*.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', {
matchers: ['**/*.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', {
matchers: ['*.*(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', { matchers: ['*.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', {
matchers: ['**/*.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', {
matchers: ['*.*(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
11 changes: 11 additions & 0 deletions src/createMatcher.js
@@ -0,0 +1,11 @@
import multimatch from 'multimatch';
import path from 'path';

export default patterns => {
if (patterns.length === 0) {
return () => true;
}
return file => {
return multimatch(path.normalize(file), patterns).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,
matchers = [],
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(matchers))
.filter(rootIgnorer)
.filter(cwdIgnorer);

const unstagedFiles = staged
? scm
.getUnstagedChangedFiles(directory, revision)
.filter(isSupportedExtension)
.filter(createMatcher(matchers))
.filter(rootIgnorer)
.filter(cwdIgnorer)
: [];
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Expand Up @@ -3130,6 +3130,16 @@ 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"
integrity sha512-22foS/gqQfANZ3o+W7ST2x25ueHDVNWl/b9OlGcLpy/iKxjCpvcNCM51YCenUi7Mt/jAjjqv8JwZRs8YP5sRjA==
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 132584f

Please sign in to comment.