diff --git a/lib/modules/manager/nuget/package-tree.ts b/lib/modules/manager/nuget/package-tree.ts index 0720f69b6e2fef..7dc29959c54f51 100644 --- a/lib/modules/manager/nuget/package-tree.ts +++ b/lib/modules/manager/nuget/package-tree.ts @@ -1,8 +1,8 @@ import is from '@sindresorhus/is'; import { Graph } from 'graph-data-structure'; -import { minimatch } from 'minimatch'; import upath from 'upath'; import { logger } from '../../../logger'; +import { minimatchFilter } from '../../../util/minimatch'; import { scm } from '../../platform/scm'; import type { ProjectFile } from './types'; import { readFileAsXmlDocument } from './util'; @@ -131,7 +131,7 @@ function reframeRelativePathToRootOfRepo( async function getAllPackageFiles(): Promise { const allFiles = await scm.getFileList(); const filteredPackageFiles = allFiles.filter( - minimatch.filter('*.{cs,vb,fs}proj', { matchBase: true, nocase: true }), + minimatchFilter('*.{cs,vb,fs}proj', { matchBase: true, nocase: true }), ); logger.trace({ filteredPackageFiles }, 'Found package files'); diff --git a/lib/util/minimatch.spec.ts b/lib/util/minimatch.spec.ts index bce5b88810f4d1..8c453d81d45d6e 100644 --- a/lib/util/minimatch.spec.ts +++ b/lib/util/minimatch.spec.ts @@ -1,18 +1,33 @@ -import { minimatch } from './minimatch'; +import { minimatch, minimatchFilter } from './minimatch'; describe('util/minimatch', () => { - it('caches minimatch', () => { - expect(minimatch('foo')).toBe(minimatch('foo')); - expect(minimatch('foo', { dot: true })).toBe( - minimatch('foo', { dot: true }), - ); + describe('minimatch', () => { + it('caches minimatch', () => { + expect(minimatch('foo')).toBe(minimatch('foo')); + expect(minimatch('foo', { dot: true })).toBe( + minimatch('foo', { dot: true }), + ); + }); + + it('does not cache minimatch', () => { + expect(minimatch('foo', undefined, false)).not.toBe( + minimatch('foo', undefined, false), + ); + expect(minimatch('foo')).not.toBe(minimatch('foo', undefined, false)); + expect(minimatch('foo', { dot: true })).not.toBe(minimatch('foo')); + }); }); - it('does not cache minimatch', () => { - expect(minimatch('foo', undefined, false)).not.toBe( - minimatch('foo', undefined, false), - ); - expect(minimatch('foo')).not.toBe(minimatch('foo', undefined, false)); - expect(minimatch('foo', { dot: true })).not.toBe(minimatch('foo')); + describe('minimatchFilter', () => { + it('should return a function', () => { + expect(minimatchFilter('*.js')).toBeFunction(); + expect(minimatchFilter('*.js', undefined, false)).toBeFunction(); + }); + + it('should correctly match filenames', () => { + const filterFunc = minimatchFilter('*.js'); + expect(filterFunc('test.js')).toBe(true); + expect(filterFunc('test.txt')).toBe(false); + }); }); }); diff --git a/lib/util/minimatch.ts b/lib/util/minimatch.ts index 1121186b4cd9d0..892e2ae91f2302 100644 --- a/lib/util/minimatch.ts +++ b/lib/util/minimatch.ts @@ -22,3 +22,24 @@ export function minimatch( } return instance; } + +export function minimatchFilter( + pattern: string, + options?: MinimatchOptions, + useCache = true, +): (fileName: string) => boolean { + const key = options ? `${pattern}:${JSON.stringify(options)}` : pattern; + + if (useCache) { + const cachedResult = cache.get(key); + if (cachedResult) { + return (fileName) => cachedResult.match(fileName); + } + } + + const instance = new Minimatch(pattern, options); + if (useCache) { + cache.set(key, instance); + } + return (fileName) => instance.match(fileName); +} diff --git a/lib/workers/global/autodiscover.ts b/lib/workers/global/autodiscover.ts index 4eaab2e746d27b..963ae4dbac6437 100644 --- a/lib/workers/global/autodiscover.ts +++ b/lib/workers/global/autodiscover.ts @@ -1,8 +1,8 @@ import is from '@sindresorhus/is'; -import { minimatch } from 'minimatch'; import type { AllConfig } from '../../config/types'; import { logger } from '../../logger'; import { platform } from '../../modules/platform'; +import { minimatchFilter } from '../../util/minimatch'; import { configRegexPredicate, isConfigRegex } from '../../util/regex'; // istanbul ignore next @@ -107,7 +107,7 @@ export function applyFilters(repos: string[], filters: string[]): string[] { } res = repos.filter(autodiscoveryPred); } else { - res = repos.filter(minimatch.filter(filter, { dot: true, nocase: true })); + res = repos.filter(minimatchFilter(filter, { dot: true, nocase: true })); } for (const repository of res) { matched.add(repository);