Skip to content

Commit

Permalink
Accept URL in function returned by isGitIgnored (#207)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
fisker and sindresorhus committed Jan 17, 2022
1 parent 1be9d02 commit a9fc794
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
2 changes: 1 addition & 1 deletion gitignore.js
Expand Up @@ -56,7 +56,7 @@ const ensureAbsolutePathForCwd = (cwd, p) => {
return path.join(cwd, p);
};

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

const getFile = async (file, cwd) => {
const filePath = path.join(cwd, file);
Expand Down
40 changes: 29 additions & 11 deletions gitignore.test.js
Expand Up @@ -5,10 +5,10 @@ import slash from 'slash';
import {isGitIgnored, isGitIgnoredSync} from './gitignore.js';

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

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

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

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

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

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

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

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

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

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

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

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

const actual = [
Expand All @@ -118,3 +118,21 @@ test('multiple negation - sync', t => {
t.deepEqual(actual, expected);
}
});

test('check file', async t => {
const directory = path.join(__dirname, 'fixtures/gitignore');
const ignoredFile = path.join(directory, 'foo.js');
const isIgnored = await isGitIgnored({cwd: directory});
const isIgnoredSync = isGitIgnoredSync({cwd: directory});

for (const file of getPathValues(ignoredFile)) {
t.true(isIgnored(file));
t.true(isIgnored({path: file}));
t.true(isIgnoredSync(file));
t.true(isIgnoredSync({path: file}));
}

for (const file of getPathValues(path.join(directory, 'bar.js'))) {
t.false(isIgnored(file));
}
});
2 changes: 1 addition & 1 deletion index.d.ts
Expand Up @@ -59,7 +59,7 @@ export interface GitignoreOptions {
readonly ignore?: readonly string[];
}

export type GlobbyFilterFunction = (path: string) => boolean;
export type GlobbyFilterFunction = (path: URL | string) => boolean;

/**
Find files and directories using glob patterns.
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -138,7 +138,7 @@ console.log(isIgnored('some/file'));

### isGitIgnoredSync(options?)

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

Takes the same options as `isGitIgnored`.

Expand Down

0 comments on commit a9fc794

Please sign in to comment.