Skip to content

Commit

Permalink
fix: also use .prettierignore from current working directory (#14) (#58)
Browse files Browse the repository at this point in the history
This pull request tries to fix #14 with a zero config approach.

If the `process.cwd()` is not the same as the git root it will also try to load the .prettierignore file from there.
  • Loading branch information
jantimon authored and azz committed Jan 16, 2019
1 parent 157d4b1 commit 108344a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -108,4 +108,4 @@ For example `pretty-quick --since HEAD` will format only staged files.

## Configuration and Ignore Files

`pretty-quick` will respect your [`.prettierrc`](https://prettier.io/docs/en/configuration), [`.prettierignore`](https://prettier.io/docs/en/ignore#ignoring-files), and [`.editorconfig`](http://editorconfig.org/) files, so there's no additional setup required. Configuration files will be found by searching up the file system. `.prettierignore` files are only found from the working directory that the command was executed from.
`pretty-quick` will respect your [`.prettierrc`](https://prettier.io/docs/en/configuration), [`.prettierignore`](https://prettier.io/docs/en/ignore#ignoring-files), and [`.editorconfig`](http://editorconfig.org/) files, so there's no additional setup required. Configuration files will be found by searching up the file system. `.prettierignore` files are only found from the repository root and the working directory that the command was executed from.
37 changes: 30 additions & 7 deletions src/__tests__/scm-git.test.js
Expand Up @@ -11,13 +11,18 @@ afterEach(() => {
jest.clearAllMocks();
});

const mockGitFs = (additionalUnstaged = '') => {
mock({
'/.git': {},
'/raz.js': 'raz()',
'/foo.js': 'foo()',
'/bar.md': '# foo',
});
const mockGitFs = (additionalUnstaged = '', additionalFiles = {}) => {
mock(
Object.assign(
{
'/.git': {},
'/raz.js': 'raz()',
'/foo.js': 'foo()',
'/bar.md': '# foo',
},
additionalFiles
)
);
execa.sync.mockImplementation((command, args) => {
if (command !== 'git') {
throw new Error(`unexpected command: ${command}`);
Expand Down Expand Up @@ -241,4 +246,22 @@ describe('with git', () => {
expect(onExamineFile).not.toHaveBeenCalledWith('./foo.js');
expect(onExamineFile).not.toHaveBeenCalledWith('./bar.md');
});

test('ignore files matching patterns from the repositories root .prettierignore', () => {
const onWriteFile = jest.fn();
mockGitFs('', {
'/.prettierignore': '*.md',
});
prettyQuick('/sub-directory/', { since: 'banana', onWriteFile });
expect(onWriteFile.mock.calls).toEqual([['./foo.js']]);
});

test('ignore files matching patterns from the working directories .prettierignore', () => {
const onWriteFile = jest.fn();
mockGitFs('', {
'/sub-directory/.prettierignore': '*.md',
});
prettyQuick('/sub-directory/', { since: 'banana', onWriteFile });
expect(onWriteFile.mock.calls).toEqual([['./foo.js']]);
});
});
35 changes: 29 additions & 6 deletions src/__tests__/scm-hg.test.js
Expand Up @@ -11,12 +11,17 @@ afterEach(() => {
jest.clearAllMocks();
});

const mockHgFs = () => {
mock({
'/.hg': {},
'/foo.js': 'foo()',
'/bar.md': '# foo',
});
const mockHgFs = (additionalFiles = {}) => {
mock(
Object.assign(
{
'/.hg': {},
'/foo.js': 'foo()',
'/bar.md': '# foo',
},
additionalFiles
)
);
execa.sync.mockImplementation((command, args) => {
if (command !== 'hg') {
throw new Error(`unexpected command: ${command}`);
Expand Down Expand Up @@ -152,4 +157,22 @@ describe('with hg', () => {
expect(onExamineFile).not.toHaveBeenCalledWith('./foo.js');
expect(onExamineFile).not.toHaveBeenCalledWith('./bar.md');
});

test('ignore files matching patterns from the repositories root .prettierignore', () => {
const onWriteFile = jest.fn();
mockHgFs({
'/.prettierignore': '*.md',
});
prettyQuick('/sub-directory/', { since: 'banana', onWriteFile });
expect(onWriteFile.mock.calls).toEqual([['./foo.js']]);
});

test('ignore files matching patterns from the working directories .prettierignore', () => {
const onWriteFile = jest.fn();
mockHgFs({
'/sub-directory/.prettierignore': '*.md',
});
prettyQuick('/sub-directory/', { since: 'banana', onWriteFile });
expect(onWriteFile.mock.calls).toEqual([['./foo.js']]);
});
});
12 changes: 10 additions & 2 deletions src/index.js
Expand Up @@ -29,16 +29,24 @@ export default (

onFoundSinceRevision && onFoundSinceRevision(scm.name, revision);

const rootIgnorer = createIgnorer(directory);
const cwdIgnorer =
currentDirectory !== directory
? createIgnorer(currentDirectory)
: () => true;

const changedFiles = scm
.getChangedFiles(directory, revision, staged)
.filter(isSupportedExtension)
.filter(createIgnorer(directory));
.filter(rootIgnorer)
.filter(cwdIgnorer);

const unstagedFiles = staged
? scm
.getUnstagedChangedFiles(directory, revision)
.filter(isSupportedExtension)
.filter(createIgnorer(directory))
.filter(rootIgnorer)
.filter(cwdIgnorer)
: [];

const wasFullyStaged = f => unstagedFiles.indexOf(f) < 0;
Expand Down

0 comments on commit 108344a

Please sign in to comment.