diff --git a/README.md b/README.md index ac4f328..b64e1ed 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/package.json b/package.json index e8a247d..1e60813 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/__tests__/scm-git.test.js b/src/__tests__/scm-git.test.js index fafcf00..edad1a9 100644 --- a/src/__tests__/scm-git.test.js +++ b/src/__tests__/scm-git.test.js @@ -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', () => { diff --git a/src/__tests__/scm-hg.test.js b/src/__tests__/scm-hg.test.js index 10a50b9..1c8eb1f 100644 --- a/src/__tests__/scm-hg.test.js +++ b/src/__tests__/scm-hg.test.js @@ -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(); @@ -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(); diff --git a/src/createMatcher.js b/src/createMatcher.js new file mode 100644 index 0000000..810e7c5 --- /dev/null +++ b/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; +}; diff --git a/src/index.js b/src/index.js index 280c831..2ae18d4 100644 --- a/src/index.js +++ b/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 ( @@ -9,6 +10,7 @@ export default ( config, since, staged, + pattern, restage = true, branch, verbose, @@ -38,6 +40,7 @@ export default ( const changedFiles = scm .getChangedFiles(directory, revision, staged) .filter(isSupportedExtension) + .filter(createMatcher(pattern)) .filter(rootIgnorer) .filter(cwdIgnorer); @@ -45,6 +48,7 @@ export default ( ? scm .getUnstagedChangedFiles(directory, revision) .filter(isSupportedExtension) + .filter(createMatcher(pattern)) .filter(rootIgnorer) .filter(cwdIgnorer) : []; diff --git a/yarn.lock b/yarn.lock index 3e3b3c9..cd3224c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"