refactor(@angular-devkit/core): use picomatch for PatternMatchingHost glob support

The glob support in the `PatternMatchingHost` class now uses the capabilities of the
`picomatch` package to convert glob strings into regular expressions. This removes
custom string replacement code that previously was used. The `picomatch` package is
already used by `@angular-devkit/build-angular` and is present in the repository but
is a new dependency for the `@angular-devkit/core` package specifically.
This commit is contained in:
Charles Lyding 2023-08-29 16:01:53 -04:00 committed by Alan Agius
parent 683f84dc13
commit f9372acb1a
3 changed files with 9 additions and 22 deletions

View File

@ -34,9 +34,11 @@ ts_library(
module_root = "src/index.d.ts",
deps = [
"@npm//@types/node",
"@npm//@types/picomatch",
"@npm//ajv",
"@npm//ajv-formats",
"@npm//jsonc-parser",
"@npm//picomatch",
"@npm//rxjs",
"@npm//source-map",
# @node_module: typescript:es2015.proxy

View File

@ -28,6 +28,7 @@
"ajv-formats": "2.1.1",
"ajv": "8.12.0",
"jsonc-parser": "3.2.0",
"picomatch": "2.3.1",
"rxjs": "7.8.1",
"source-map": "0.7.4"
},

View File

@ -6,6 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import { parse as parseGlob } from 'picomatch';
import { Path } from '../path';
import { ResolverHost } from './resolver';
@ -17,28 +18,11 @@ export class PatternMatchingHost<StatsT extends object = {}> extends ResolverHos
protected _patterns = new Map<RegExp, ReplacementFunction>();
addPattern(pattern: string | string[], replacementFn: ReplacementFunction) {
// Simple GLOB pattern replacement.
const reString =
'^(' +
(Array.isArray(pattern) ? pattern : [pattern])
.map(
(ex) =>
'(' +
ex
.split(/[/\\]/g)
.map((f) =>
f
.replace(/[-[\]{}()+?.^$|]/g, '\\$&')
.replace(/^\*\*/g, '(.+?)?')
.replace(/\*/g, '[^/\\\\]*'),
)
.join('[/\\\\]') +
')',
)
.join('|') +
')($|/|\\\\)';
this._patterns.set(new RegExp(reString), replacementFn);
const patterns = Array.isArray(pattern) ? pattern : [pattern];
for (const glob of patterns) {
const { output } = parseGlob(glob);
this._patterns.set(new RegExp(`^${output}$`), replacementFn);
}
}
protected _resolve(path: Path) {