Skip to content

Commit a9fc794

Browse files
fiskersindresorhus
andauthoredJan 17, 2022
Accept URL in function returned by isGitIgnored (#207)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent 1be9d02 commit a9fc794

File tree

4 files changed

+32
-14
lines changed

4 files changed

+32
-14
lines changed
 

‎gitignore.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const ensureAbsolutePathForCwd = (cwd, p) => {
5656
return path.join(cwd, p);
5757
};
5858

59-
const getIsIgnoredPredicate = (ignores, cwd) => p => ignores.ignores(slash(path.relative(cwd, ensureAbsolutePathForCwd(cwd, p.path || p))));
59+
const getIsIgnoredPredicate = (ignores, cwd) => p => ignores.ignores(slash(path.relative(cwd, ensureAbsolutePathForCwd(cwd, toPath(p.path || p)))));
6060

6161
const getFile = async (file, cwd) => {
6262
const filePath = path.join(cwd, file);

‎gitignore.test.js

+29-11
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import slash from 'slash';
55
import {isGitIgnored, isGitIgnoredSync} from './gitignore.js';
66

77
const __dirname = path.dirname(fileURLToPath(import.meta.url));
8-
const getCwdValues = cwd => [cwd, pathToFileURL(cwd), pathToFileURL(cwd).href];
8+
const getPathValues = cwd => [cwd, pathToFileURL(cwd), pathToFileURL(cwd).href];
99

1010
test('gitignore', async t => {
11-
for (const cwd of getCwdValues(path.join(__dirname, 'fixtures/gitignore'))) {
11+
for (const cwd of getPathValues(path.join(__dirname, 'fixtures/gitignore'))) {
1212
// eslint-disable-next-line no-await-in-loop
1313
const isIgnored = await isGitIgnored({cwd});
1414
const actual = ['foo.js', 'bar.js'].filter(file => !isIgnored(file));
@@ -19,7 +19,7 @@ test('gitignore', async t => {
1919

2020
test('gitignore - mixed path styles', async t => {
2121
const directory = path.join(__dirname, 'fixtures/gitignore');
22-
for (const cwd of getCwdValues(directory)) {
22+
for (const cwd of getPathValues(directory)) {
2323
// eslint-disable-next-line no-await-in-loop
2424
const isIgnored = await isGitIgnored({cwd});
2525
t.true(isIgnored(slash(path.resolve(directory, 'foo.js'))));
@@ -28,15 +28,15 @@ test('gitignore - mixed path styles', async t => {
2828

2929
test('gitignore - os paths', async t => {
3030
const directory = path.join(__dirname, 'fixtures/gitignore');
31-
for (const cwd of getCwdValues(directory)) {
31+
for (const cwd of getPathValues(directory)) {
3232
// eslint-disable-next-line no-await-in-loop
3333
const isIgnored = await isGitIgnored({cwd});
3434
t.true(isIgnored(path.resolve(directory, 'foo.js')));
3535
}
3636
});
3737

3838
test('gitignore - sync', t => {
39-
for (const cwd of getCwdValues(path.join(__dirname, 'fixtures/gitignore'))) {
39+
for (const cwd of getPathValues(path.join(__dirname, 'fixtures/gitignore'))) {
4040
const isIgnored = isGitIgnoredSync({cwd});
4141
const actual = ['foo.js', 'bar.js'].filter(file => !isIgnored(file));
4242
const expected = ['bar.js'];
@@ -47,7 +47,7 @@ test('gitignore - sync', t => {
4747
test('ignore ignored .gitignore', async t => {
4848
const ignore = ['**/.gitignore'];
4949

50-
for (const cwd of getCwdValues(path.join(__dirname, 'fixtures/gitignore'))) {
50+
for (const cwd of getPathValues(path.join(__dirname, 'fixtures/gitignore'))) {
5151
// eslint-disable-next-line no-await-in-loop
5252
const isIgnored = await isGitIgnored({cwd, ignore});
5353
const actual = ['foo.js', 'bar.js'].filter(file => !isIgnored(file));
@@ -59,7 +59,7 @@ test('ignore ignored .gitignore', async t => {
5959
test('ignore ignored .gitignore - sync', t => {
6060
const ignore = ['**/.gitignore'];
6161

62-
for (const cwd of getCwdValues(path.join(__dirname, 'fixtures/gitignore'))) {
62+
for (const cwd of getPathValues(path.join(__dirname, 'fixtures/gitignore'))) {
6363
const isIgnored = isGitIgnoredSync({cwd, ignore});
6464
const actual = ['foo.js', 'bar.js'].filter(file => !isIgnored(file));
6565
const expected = ['foo.js', 'bar.js'];
@@ -68,7 +68,7 @@ test('ignore ignored .gitignore - sync', t => {
6868
});
6969

7070
test('negative gitignore', async t => {
71-
for (const cwd of getCwdValues(path.join(__dirname, 'fixtures/negative'))) {
71+
for (const cwd of getPathValues(path.join(__dirname, 'fixtures/negative'))) {
7272
// eslint-disable-next-line no-await-in-loop
7373
const isIgnored = await isGitIgnored({cwd});
7474
const actual = ['foo.js', 'bar.js'].filter(file => !isIgnored(file));
@@ -78,7 +78,7 @@ test('negative gitignore', async t => {
7878
});
7979

8080
test('negative gitignore - sync', t => {
81-
for (const cwd of getCwdValues(path.join(__dirname, 'fixtures/negative'))) {
81+
for (const cwd of getPathValues(path.join(__dirname, 'fixtures/negative'))) {
8282
const isIgnored = isGitIgnoredSync({cwd});
8383
const actual = ['foo.js', 'bar.js'].filter(file => !isIgnored(file));
8484
const expected = ['foo.js'];
@@ -87,7 +87,7 @@ test('negative gitignore - sync', t => {
8787
});
8888

8989
test('multiple negation', async t => {
90-
for (const cwd of getCwdValues(path.join(__dirname, 'fixtures/multiple-negation'))) {
90+
for (const cwd of getPathValues(path.join(__dirname, 'fixtures/multiple-negation'))) {
9191
// eslint-disable-next-line no-await-in-loop
9292
const isIgnored = await isGitIgnored({cwd});
9393

@@ -104,7 +104,7 @@ test('multiple negation', async t => {
104104
});
105105

106106
test('multiple negation - sync', t => {
107-
for (const cwd of getCwdValues(path.join(__dirname, 'fixtures/multiple-negation'))) {
107+
for (const cwd of getPathValues(path.join(__dirname, 'fixtures/multiple-negation'))) {
108108
const isIgnored = isGitIgnoredSync({cwd});
109109

110110
const actual = [
@@ -118,3 +118,21 @@ test('multiple negation - sync', t => {
118118
t.deepEqual(actual, expected);
119119
}
120120
});
121+
122+
test('check file', async t => {
123+
const directory = path.join(__dirname, 'fixtures/gitignore');
124+
const ignoredFile = path.join(directory, 'foo.js');
125+
const isIgnored = await isGitIgnored({cwd: directory});
126+
const isIgnoredSync = isGitIgnoredSync({cwd: directory});
127+
128+
for (const file of getPathValues(ignoredFile)) {
129+
t.true(isIgnored(file));
130+
t.true(isIgnored({path: file}));
131+
t.true(isIgnoredSync(file));
132+
t.true(isIgnoredSync({path: file}));
133+
}
134+
135+
for (const file of getPathValues(path.join(directory, 'bar.js'))) {
136+
t.false(isIgnored(file));
137+
}
138+
});

‎index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export interface GitignoreOptions {
5959
readonly ignore?: readonly string[];
6060
}
6161

62-
export type GlobbyFilterFunction = (path: string) => boolean;
62+
export type GlobbyFilterFunction = (path: URL | string) => boolean;
6363

6464
/**
6565
Find files and directories using glob patterns.

‎readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ console.log(isIgnored('some/file'));
138138

139139
### isGitIgnoredSync(options?)
140140

141-
Returns a `(path: string) => boolean` indicating whether a given path is ignored via a `.gitignore` file.
141+
Returns a `(path: URL | string) => boolean` indicating whether a given path is ignored via a `.gitignore` file.
142142

143143
Takes the same options as `isGitIgnored`.
144144

0 commit comments

Comments
 (0)
Please sign in to comment.