Skip to content

Commit cba8941

Browse files
jiawei397sindresorhus
andauthoredFeb 7, 2025··
Export isIgnoredByIgnoreFiles and isIgnoredByIgnoreFilesSync functions (#269)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent c000568 commit cba8941

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed
 

‎index.d.ts

+48
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,51 @@ export function isGitIgnored(options?: GitignoreOptions): Promise<GlobbyFilterFu
205205
export function isGitIgnoredSync(options?: GitignoreOptions): GlobbyFilterFunction;
206206

207207
export function convertPathToPattern(source: string): FastGlob.Pattern;
208+
209+
/**
210+
Check if a path is ignored by the ignore files.
211+
212+
@param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns).
213+
@param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-3) in addition to the ones in this package.
214+
@returns A filter function indicating whether a given path is ignored via the ignore files.
215+
216+
This is a more generic form of the `isGitIgnored` function, allowing you to find ignore files with a [compatible syntax](http://git-scm.com/docs/gitignore). For instance, this works with Babel's `.babelignore`, Prettier's `.prettierignore`, or ESLint's `.eslintignore` files.
217+
218+
@example
219+
```
220+
import {isIgnoredByIgnoreFiles} from 'globby';
221+
222+
const isIgnored = await isIgnoredByIgnoreFiles('**\/.gitignore');
223+
224+
console.log(isIgnored('some/file'));
225+
```
226+
*/
227+
export function isIgnoredByIgnoreFiles(
228+
patterns: string | readonly string[],
229+
options?: Options
230+
): Promise<GlobbyFilterFunction>;
231+
232+
/**
233+
Check if a path is ignored by the ignore files.
234+
235+
@param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns).
236+
@param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-3) in addition to the ones in this package.
237+
@returns A filter function indicating whether a given path is ignored via the ignore files.
238+
239+
This is a more generic form of the `isGitIgnored` function, allowing you to find ignore files with a [compatible syntax](http://git-scm.com/docs/gitignore). For instance, this works with Babel's `.babelignore`, Prettier's `.prettierignore`, or ESLint's `.eslintignore` files.
240+
241+
@see {@link isIgnoredByIgnoreFiles}
242+
243+
@example
244+
```
245+
import {isIgnoredByIgnoreFilesSync} from 'globby';
246+
247+
const isIgnored = isIgnoredByIgnoreFilesSync('**\/.gitignore');
248+
249+
console.log(isIgnored('some/file'));
250+
```
251+
*/
252+
export function isIgnoredByIgnoreFilesSync(
253+
patterns: string | readonly string[],
254+
options?: Options
255+
): GlobbyFilterFunction;

‎index.js

+2
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ export const generateGlobTasksSync = normalizeArgumentsSync(generateTasksSync);
259259
export {
260260
isGitIgnored,
261261
isGitIgnoredSync,
262+
isIgnoredByIgnoreFiles,
263+
isIgnoredByIgnoreFilesSync,
262264
} from './ignore.js';
263265

264266
export const {convertPathToPattern} = fastGlob;

‎readme.md

+33
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,39 @@ Returns a `(path: URL | string) => boolean` indicating whether a given path is i
157157

158158
Takes `cwd?: URL | string` as options.
159159

160+
161+
### isIgnoredByIgnoreFiles(patterns, options?)
162+
163+
Returns a `Promise<(path: URL | string) => boolean>` indicating whether a given path is ignored via the ignore files.
164+
165+
This is a more generic form of the `isGitIgnored` function, allowing you to find ignore files with a [compatible syntax](http://git-scm.com/docs/gitignore). For instance, this works with Babel's `.babelignore`, Prettier's `.prettierignore`, or ESLint's `.eslintignore` files.
166+
167+
Takes `cwd?: URL | string` as options.
168+
169+
```js
170+
import {isIgnoredByIgnoreFiles} from 'globby';
171+
172+
const isIgnored = await isIgnoredByIgnoreFiles("**/.gitignore");
173+
174+
console.log(isIgnored('some/file'));
175+
```
176+
177+
### isIgnoredByIgnoreFilesSync(patterns, options?)
178+
179+
Returns a `(path: URL | string) => boolean` indicating whether a given path is ignored via the ignore files.
180+
181+
This is a more generic form of the `isGitIgnoredSync` function, allowing you to find ignore files with a [compatible syntax](http://git-scm.com/docs/gitignore). For instance, this works with Babel's `.babelignore`, Prettier's `.prettierignore`, or ESLint's `.eslintignore` files.
182+
183+
Takes `cwd?: URL | string` as options.
184+
185+
```js
186+
import {isIgnoredByIgnoreFilesSync} from 'globby';
187+
188+
const isIgnored = isIgnoredByIgnoreFilesSync("**/.gitignore");
189+
190+
console.log(isIgnored('some/file'));
191+
```
192+
160193
## Globbing patterns
161194

162195
Just a quick overview.

0 commit comments

Comments
 (0)