Skip to content

Commit

Permalink
Remove ignore option for isGitIgnored and isGitIgnoredSync (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jan 21, 2022
1 parent 04fbd5b commit 2e43cc4
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 67 deletions.
68 changes: 30 additions & 38 deletions gitignore.js
Expand Up @@ -6,12 +6,15 @@ import gitIgnore from 'ignore';
import slash from 'slash';
import {toPath} from './utilities.js';

const DEFAULT_IGNORE = [
'**/node_modules/**',
'**/flow-typed/**',
'**/coverage/**',
'**/.git',
];
const gitignoreGlobOptions = {
ignore: [
'**/node_modules',
'**/flow-typed',
'**/coverage',
'**/.git',
],
absolute: true,
};

const mapGitIgnorePatternTo = base => ignore => {
if (ignore.startsWith('!')) {
Expand Down Expand Up @@ -58,51 +61,40 @@ const ensureAbsolutePathForCwd = (cwd, p) => {

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

const getFile = async (file, cwd) => {
const filePath = path.join(cwd, file);
const content = await fs.promises.readFile(filePath, 'utf8');

return {
cwd,
filePath,
content,
};
};
const getFile = async (filePath, cwd) => ({
cwd,
filePath,
content: await fs.promises.readFile(filePath, 'utf8'),
});

const getFileSync = (file, cwd) => {
const filePath = path.join(cwd, file);
const content = fs.readFileSync(filePath, 'utf8');

return {
cwd,
filePath,
content,
};
};
const getFileSync = (filePath, cwd) => ({
cwd,
filePath,
content: fs.readFileSync(filePath, 'utf8'),
});

const normalizeOptions = ({
ignore = [],
cwd = slash(process.cwd()),
} = {}) => ({ignore: [...DEFAULT_IGNORE, ...ignore], cwd: toPath(cwd)});
const normalizeOptions = (options = {}) => ({
cwd: toPath(options.cwd) || slash(process.cwd()),
});

export const isGitIgnored = async options => {
options = normalizeOptions(options);
const {cwd} = normalizeOptions(options);

const paths = await fastGlob('**/.gitignore', options);
const paths = await fastGlob('**/.gitignore', {cwd, ...gitignoreGlobOptions});

const files = await Promise.all(paths.map(file => getFile(file, options.cwd)));
const files = await Promise.all(paths.map(file => getFile(file, cwd)));
const ignores = reduceIgnore(files);

return getIsIgnoredPredicate(ignores, options.cwd);
return getIsIgnoredPredicate(ignores, cwd);
};

export const isGitIgnoredSync = options => {
options = normalizeOptions(options);
const {cwd} = normalizeOptions(options);

const paths = fastGlob.sync('**/.gitignore', options);
const paths = fastGlob.sync('**/.gitignore', {cwd, ...gitignoreGlobOptions});

const files = paths.map(file => getFileSync(file, options.cwd));
const files = paths.map(file => getFileSync(file, cwd));
const ignores = reduceIgnore(files);

return getIsIgnoredPredicate(ignores, options.cwd);
return getIsIgnoredPredicate(ignores, cwd);
};
1 change: 0 additions & 1 deletion index.d.ts
Expand Up @@ -57,7 +57,6 @@ export interface Options extends FastGlobOptionsWithoutCwd {

export interface GitignoreOptions {
readonly cwd?: URL | string;
readonly ignore?: readonly string[];
}

export type GlobbyFilterFunction = (path: URL | string) => boolean;
Expand Down
6 changes: 3 additions & 3 deletions index.test-d.ts
Expand Up @@ -125,7 +125,7 @@ expectType<GlobTask[]>(generateGlobTasksSync('*.tmp', {ignore: ['**/b.tmp']}));
expectType<boolean>(isDynamicPattern('**'));
expectType<boolean>(isDynamicPattern(['**', 'path1', 'path2']));
expectType<boolean>(isDynamicPattern(['**', 'path1', 'path2'], {extglob: false}));
expectType<boolean>(isDynamicPattern(['**'], {cwd: new URL('https://example.com')}));
expectType<boolean>(isDynamicPattern(['**'], {cwd: new URL('file:///path/to/cwd')}));
expectType<boolean>(isDynamicPattern(['**'], {cwd: __dirname}));

// IsGitIgnored
Expand All @@ -137,7 +137,7 @@ expectType<Promise<GlobbyFilterFunction>>(
);
expectType<Promise<GlobbyFilterFunction>>(
isGitIgnored({
ignore: ['**/b.tmp'],
cwd: new URL('file:///path/to/cwd'),
}),
);

Expand All @@ -150,6 +150,6 @@ expectType<GlobbyFilterFunction>(
);
expectType<GlobbyFilterFunction>(
isGitIgnoredSync({
ignore: ['**/b.tmp'],
cwd: new URL('file:///path/to/cwd'),
}),
);
4 changes: 2 additions & 2 deletions readme.md
Expand Up @@ -132,7 +132,7 @@ This function is backed by [`fast-glob`](https://github.com/mrmlnc/fast-glob#isd

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

Takes `cwd?: URL | string` and `ignore?: string[]` as options. `.gitignore` files matched by the ignore config are not used for the resulting filter function.
Takes `cwd?: URL | string` as options.

```js
import {isGitIgnored} from 'globby';
Expand All @@ -146,7 +146,7 @@ console.log(isIgnored('some/file'));

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

Takes the same options as `isGitIgnored`.
Takes `cwd?: URL | string` as options.

## Globbing patterns

Expand Down
23 changes: 0 additions & 23 deletions tests/gitignore.js
Expand Up @@ -44,29 +44,6 @@ test('gitignore - sync', t => {
}
});

test('ignore ignored .gitignore', async t => {
const ignore = ['**/.gitignore'];

for (const cwd of getPathValues(path.join(PROJECT_ROOT, '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));
const expected = ['foo.js', 'bar.js'];
t.deepEqual(actual, expected);
}
});

test('ignore ignored .gitignore - sync', t => {
const ignore = ['**/.gitignore'];

for (const cwd of getPathValues(path.join(PROJECT_ROOT, 'fixtures/gitignore'))) {
const isIgnored = isGitIgnoredSync({cwd, ignore});
const actual = ['foo.js', 'bar.js'].filter(file => !isIgnored(file));
const expected = ['foo.js', 'bar.js'];
t.deepEqual(actual, expected);
}
});

test('negative gitignore', async t => {
for (const cwd of getPathValues(path.join(PROJECT_ROOT, 'fixtures/negative'))) {
// eslint-disable-next-line no-await-in-loop
Expand Down

0 comments on commit 2e43cc4

Please sign in to comment.