Skip to content

Commit 2e43cc4

Browse files
authoredJan 21, 2022
Remove ignore option for isGitIgnored and isGitIgnoredSync (#225)
1 parent 04fbd5b commit 2e43cc4

File tree

5 files changed

+35
-67
lines changed

5 files changed

+35
-67
lines changed
 

‎gitignore.js

+30-38
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ import gitIgnore from 'ignore';
66
import slash from 'slash';
77
import {toPath} from './utilities.js';
88

9-
const DEFAULT_IGNORE = [
10-
'**/node_modules/**',
11-
'**/flow-typed/**',
12-
'**/coverage/**',
13-
'**/.git',
14-
];
9+
const gitignoreGlobOptions = {
10+
ignore: [
11+
'**/node_modules',
12+
'**/flow-typed',
13+
'**/coverage',
14+
'**/.git',
15+
],
16+
absolute: true,
17+
};
1518

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

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

61-
const getFile = async (file, cwd) => {
62-
const filePath = path.join(cwd, file);
63-
const content = await fs.promises.readFile(filePath, 'utf8');
64-
65-
return {
66-
cwd,
67-
filePath,
68-
content,
69-
};
70-
};
64+
const getFile = async (filePath, cwd) => ({
65+
cwd,
66+
filePath,
67+
content: await fs.promises.readFile(filePath, 'utf8'),
68+
});
7169

72-
const getFileSync = (file, cwd) => {
73-
const filePath = path.join(cwd, file);
74-
const content = fs.readFileSync(filePath, 'utf8');
75-
76-
return {
77-
cwd,
78-
filePath,
79-
content,
80-
};
81-
};
70+
const getFileSync = (filePath, cwd) => ({
71+
cwd,
72+
filePath,
73+
content: fs.readFileSync(filePath, 'utf8'),
74+
});
8275

83-
const normalizeOptions = ({
84-
ignore = [],
85-
cwd = slash(process.cwd()),
86-
} = {}) => ({ignore: [...DEFAULT_IGNORE, ...ignore], cwd: toPath(cwd)});
76+
const normalizeOptions = (options = {}) => ({
77+
cwd: toPath(options.cwd) || slash(process.cwd()),
78+
});
8779

8880
export const isGitIgnored = async options => {
89-
options = normalizeOptions(options);
81+
const {cwd} = normalizeOptions(options);
9082

91-
const paths = await fastGlob('**/.gitignore', options);
83+
const paths = await fastGlob('**/.gitignore', {cwd, ...gitignoreGlobOptions});
9284

93-
const files = await Promise.all(paths.map(file => getFile(file, options.cwd)));
85+
const files = await Promise.all(paths.map(file => getFile(file, cwd)));
9486
const ignores = reduceIgnore(files);
9587

96-
return getIsIgnoredPredicate(ignores, options.cwd);
88+
return getIsIgnoredPredicate(ignores, cwd);
9789
};
9890

9991
export const isGitIgnoredSync = options => {
100-
options = normalizeOptions(options);
92+
const {cwd} = normalizeOptions(options);
10193

102-
const paths = fastGlob.sync('**/.gitignore', options);
94+
const paths = fastGlob.sync('**/.gitignore', {cwd, ...gitignoreGlobOptions});
10395

104-
const files = paths.map(file => getFileSync(file, options.cwd));
96+
const files = paths.map(file => getFileSync(file, cwd));
10597
const ignores = reduceIgnore(files);
10698

107-
return getIsIgnoredPredicate(ignores, options.cwd);
99+
return getIsIgnoredPredicate(ignores, cwd);
108100
};

‎index.d.ts

-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ export interface Options extends FastGlobOptionsWithoutCwd {
5757

5858
export interface GitignoreOptions {
5959
readonly cwd?: URL | string;
60-
readonly ignore?: readonly string[];
6160
}
6261

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

‎index.test-d.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ expectType<GlobTask[]>(generateGlobTasksSync('*.tmp', {ignore: ['**/b.tmp']}));
125125
expectType<boolean>(isDynamicPattern('**'));
126126
expectType<boolean>(isDynamicPattern(['**', 'path1', 'path2']));
127127
expectType<boolean>(isDynamicPattern(['**', 'path1', 'path2'], {extglob: false}));
128-
expectType<boolean>(isDynamicPattern(['**'], {cwd: new URL('https://example.com')}));
128+
expectType<boolean>(isDynamicPattern(['**'], {cwd: new URL('file:///path/to/cwd')}));
129129
expectType<boolean>(isDynamicPattern(['**'], {cwd: __dirname}));
130130

131131
// IsGitIgnored
@@ -137,7 +137,7 @@ expectType<Promise<GlobbyFilterFunction>>(
137137
);
138138
expectType<Promise<GlobbyFilterFunction>>(
139139
isGitIgnored({
140-
ignore: ['**/b.tmp'],
140+
cwd: new URL('file:///path/to/cwd'),
141141
}),
142142
);
143143

@@ -150,6 +150,6 @@ expectType<GlobbyFilterFunction>(
150150
);
151151
expectType<GlobbyFilterFunction>(
152152
isGitIgnoredSync({
153-
ignore: ['**/b.tmp'],
153+
cwd: new URL('file:///path/to/cwd'),
154154
}),
155155
);

‎readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ This function is backed by [`fast-glob`](https://github.com/mrmlnc/fast-glob#isd
132132

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

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

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

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

149-
Takes the same options as `isGitIgnored`.
149+
Takes `cwd?: URL | string` as options.
150150

151151
## Globbing patterns
152152

‎tests/gitignore.js

-23
Original file line numberDiff line numberDiff line change
@@ -44,29 +44,6 @@ test('gitignore - sync', t => {
4444
}
4545
});
4646

47-
test('ignore ignored .gitignore', async t => {
48-
const ignore = ['**/.gitignore'];
49-
50-
for (const cwd of getPathValues(path.join(PROJECT_ROOT, 'fixtures/gitignore'))) {
51-
// eslint-disable-next-line no-await-in-loop
52-
const isIgnored = await isGitIgnored({cwd, ignore});
53-
const actual = ['foo.js', 'bar.js'].filter(file => !isIgnored(file));
54-
const expected = ['foo.js', 'bar.js'];
55-
t.deepEqual(actual, expected);
56-
}
57-
});
58-
59-
test('ignore ignored .gitignore - sync', t => {
60-
const ignore = ['**/.gitignore'];
61-
62-
for (const cwd of getPathValues(path.join(PROJECT_ROOT, 'fixtures/gitignore'))) {
63-
const isIgnored = isGitIgnoredSync({cwd, ignore});
64-
const actual = ['foo.js', 'bar.js'].filter(file => !isIgnored(file));
65-
const expected = ['foo.js', 'bar.js'];
66-
t.deepEqual(actual, expected);
67-
}
68-
});
69-
7047
test('negative gitignore', async t => {
7148
for (const cwd of getPathValues(path.join(PROJECT_ROOT, 'fixtures/negative'))) {
7249
// eslint-disable-next-line no-await-in-loop

0 commit comments

Comments
 (0)
Please sign in to comment.