From bad86bb82943ca13470186f9fb0d89763d714283 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 21 Jan 2022 12:53:16 +0800 Subject: [PATCH 1/2] Remove `ignore` option for `isGitIgnored` --- gitignore.js | 60 ++++++++++++++++------------------------------ index.d.ts | 1 - index.test-d.ts | 6 ++--- readme.md | 4 ++-- tests/gitignore.js | 23 ------------------ 5 files changed, 26 insertions(+), 68 deletions(-) diff --git a/gitignore.js b/gitignore.js index d5f8631..b3c24f2 100644 --- a/gitignore.js +++ b/gitignore.js @@ -6,13 +6,6 @@ import gitIgnore from 'ignore'; import slash from 'slash'; import {toPath} from './utilities.js'; -const DEFAULT_IGNORE = [ - '**/node_modules/**', - '**/flow-typed/**', - '**/coverage/**', - '**/.git', -]; - const mapGitIgnorePatternTo = base => ignore => { if (ignore.startsWith('!')) { return '!' + path.posix.join(base, ignore.slice(1)); @@ -58,51 +51,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, absolute: true}); - 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, absolute: true}); - 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); }; diff --git a/index.d.ts b/index.d.ts index 6a84d28..ae46f2a 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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; diff --git a/index.test-d.ts b/index.test-d.ts index e6bc5f5..25955e9 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -125,7 +125,7 @@ expectType(generateGlobTasksSync('*.tmp', {ignore: ['**/b.tmp']})); expectType(isDynamicPattern('**')); expectType(isDynamicPattern(['**', 'path1', 'path2'])); expectType(isDynamicPattern(['**', 'path1', 'path2'], {extglob: false})); -expectType(isDynamicPattern(['**'], {cwd: new URL('https://example.com')})); +expectType(isDynamicPattern(['**'], {cwd: new URL('file:///path/to/cwd')})); expectType(isDynamicPattern(['**'], {cwd: __dirname})); // IsGitIgnored @@ -137,7 +137,7 @@ expectType>( ); expectType>( isGitIgnored({ - ignore: ['**/b.tmp'], + cwd: new URL('file:///path/to/cwd'), }), ); @@ -150,6 +150,6 @@ expectType( ); expectType( isGitIgnoredSync({ - ignore: ['**/b.tmp'], + cwd: new URL('file:///path/to/cwd'), }), ); diff --git a/readme.md b/readme.md index 32d76ed..8ddca37 100644 --- a/readme.md +++ b/readme.md @@ -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'; @@ -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 diff --git a/tests/gitignore.js b/tests/gitignore.js index 5fc8c00..7dd04b6 100644 --- a/tests/gitignore.js +++ b/tests/gitignore.js @@ -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 From 40f19f9d55c67dc02be7882d7a0d5f99308148f1 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 21 Jan 2022 19:44:11 +0800 Subject: [PATCH 2/2] Ignore dirs when glob `.gitignore` file --- gitignore.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/gitignore.js b/gitignore.js index b3c24f2..0d27c58 100644 --- a/gitignore.js +++ b/gitignore.js @@ -6,6 +6,16 @@ import gitIgnore from 'ignore'; import slash from 'slash'; import {toPath} from './utilities.js'; +const gitignoreGlobOptions = { + ignore: [ + '**/node_modules', + '**/flow-typed', + '**/coverage', + '**/.git', + ], + absolute: true, +}; + const mapGitIgnorePatternTo = base => ignore => { if (ignore.startsWith('!')) { return '!' + path.posix.join(base, ignore.slice(1)); @@ -70,7 +80,7 @@ const normalizeOptions = (options = {}) => ({ export const isGitIgnored = async options => { const {cwd} = normalizeOptions(options); - const paths = await fastGlob('**/.gitignore', {cwd, absolute: true}); + const paths = await fastGlob('**/.gitignore', {cwd, ...gitignoreGlobOptions}); const files = await Promise.all(paths.map(file => getFile(file, cwd))); const ignores = reduceIgnore(files); @@ -81,7 +91,7 @@ export const isGitIgnored = async options => { export const isGitIgnoredSync = options => { const {cwd} = normalizeOptions(options); - const paths = fastGlob.sync('**/.gitignore', {cwd, absolute: true}); + const paths = fastGlob.sync('**/.gitignore', {cwd, ...gitignoreGlobOptions}); const files = paths.map(file => getFileSync(file, cwd)); const ignores = reduceIgnore(files);