diff --git a/README.md b/README.md index ac4f328..bc8d815 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)"` + ### `--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..0acb07a 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", + "minimatch": "3.0.4" }, "scripts": { "prepublishOnly": "yarn build", diff --git a/src/__tests__/scm-git.test.js b/src/__tests__/scm-git.test.js index fafcf00..376b11c 100644 --- a/src/__tests__/scm-git.test.js +++ b/src/__tests__/scm-git.test.js @@ -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', () => { diff --git a/src/__tests__/scm-hg.test.js b/src/__tests__/scm-hg.test.js index 10a50b9..ff90f78 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(); diff --git a/src/createMatcher.js b/src/createMatcher.js new file mode 100644 index 0000000..37ead0c --- /dev/null +++ b/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 }); +}; 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..78e2e00 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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: