Skip to content

Commit 6b385c9

Browse files
committed
refactor: Support glob patterns (via minimatch) rather than regular expressions
1 parent b7dcbb5 commit 6b385c9

File tree

9 files changed

+127
-115
lines changed

9 files changed

+127
-115
lines changed

apps/api-extractor/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"@rushstack/ts-command-line": "workspace:*",
4646
"colors": "~1.2.1",
4747
"lodash": "~4.17.15",
48+
"minimatch": "~3.0.3",
4849
"resolve": "~1.22.1",
4950
"semver": "~7.5.4",
5051
"source-map": "~0.6.1",
@@ -56,6 +57,7 @@
5657
"@rushstack/heft-node-rig": "2.4.0",
5758
"@types/heft-jest": "1.0.1",
5859
"@types/lodash": "4.14.116",
60+
"@types/minimatch": "3.0.5",
5961
"@types/node": "18.17.15",
6062
"@types/resolve": "1.20.2",
6163
"@types/semver": "7.5.0"

apps/api-extractor/src/api/ExtractorConfig.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,8 @@ export class ExtractorConfig {
837837
}
838838

839839
const bundledPackages: string[] = configObject.bundledPackages || [];
840-
// Note: we cannot fully validate package name pattern, as the string may be a RegExp pattern.
840+
841+
// Note: we cannot fully validate package name patterns, as the strings may contain wildcards.
841842
// We won't know if the entries are valid until we can compare them against the package.json "dependencies" contents.
842843

843844
const tsconfigFilePath: string = ExtractorConfig._resolvePathWithTokens(

apps/api-extractor/src/api/IConfigFile.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,8 @@ export interface IConfigFile {
376376

377377
/**
378378
* A list of NPM package names whose exports should be treated as part of this package.
379-
* Supports regular expressions.
379+
*
380+
* @remarks Also supports glob patterns.
380381
*
381382
* @example
382383
*

apps/api-extractor/src/collector/Collector.ts

+14-12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
PackageName
1212
} from '@rushstack/node-core-library';
1313
import { ReleaseTag } from '@microsoft/api-extractor-model';
14+
import minimatch from 'minimatch';
1415

1516
import { ExtractorMessageId } from '../api/ExtractorMessageId';
1617

@@ -157,56 +158,57 @@ export class Collector {
157158
}
158159

159160
/**
160-
* Searches the provided package.json for dependencies that match the provided package names and/or RegExp patterns
161+
* Searches the provided package.json for dependencies that match the provided package names and/or glob patterns
161162
* in `bundledPackages`.
162-
* @param bundledPackages - The list of package names and/or RegExp patterns to search for in the package.json.
163+
* @param bundledPackages - The list of package names and/or glob patterns to search for in the package.json.
163164
* @param packageJson - The package.json of the package being processed.
164165
* @returns The set of matching package names.
165166
*/
166167
private static _resolveBundledPackagePatterns(
167168
bundledPackages: string[],
168169
packageJson: INodePackageJson | undefined
169170
): ReadonlySet<string> {
170-
// The set to be built up and returned
171-
const packageNames: Set<string> = new Set<string>();
172-
173171
if (bundledPackages.length === 0) {
174172
// If no `bundledPackages` were specified, then there is nothing to resolve.
175173
// Return an empty set.
176-
return packageNames;
174+
return new Set<string>();
177175
}
178176

179177
if (packageJson === undefined) {
180178
// If no package.json is present, then there are no possible package matches.
181179
// Return an empty set.
182-
return packageNames;
180+
return new Set<string>();
183181
}
184182

185183
const dependencyNames: string[] = Object.keys(packageJson.dependencies ?? {});
186184

187185
if (dependencyNames.length === 0) {
188186
// If there are no dependencies, then there are no possible package matches.
189187
// Return an empty set.
190-
return packageNames;
188+
return new Set<string>();
191189
}
192190

191+
// The set of resolved package names to be populated and returned
192+
const packageNames: Set<string> = new Set<string>();
193+
193194
for (const packageNameOrPattern of bundledPackages) {
194195
// If the string is an exact package name, search for exact match
195196
if (PackageName.isValidName(packageNameOrPattern)) {
196197
if (dependencyNames.includes(packageNameOrPattern)) {
197198
packageNames.add(packageNameOrPattern);
198199
}
199200
} else {
200-
// If the entry isn't an exact package name, assume RegExp and search for matches
201-
const regexp: RegExp = new RegExp(packageNameOrPattern);
202-
const matches: string[] = dependencyNames.filter((dependencyName) => regexp.test(dependencyName));
201+
// If the entry isn't an exact package name, assume glob pattern and search for matches
202+
const matches: string[] = dependencyNames.filter((dependencyName) =>
203+
minimatch(dependencyName, packageNameOrPattern)
204+
);
203205
matches.forEach((match) => packageNames.add(match));
204206
}
205207
}
206208
return packageNames;
207209
}
208210

209-
/**
211+
/**a
210212
* Returns a list of names (e.g. "example-library") that should appear in a reference like this:
211213
*
212214
* ```

apps/api-extractor/src/schemas/api-extractor.schema.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
},
2525

2626
"bundledPackages": {
27-
"description": "A list of NPM package names whose exports should be treated as part of this package. Also supports regular expressions.",
27+
"description": "A list of NPM package names whose exports should be treated as part of this package. Also supports glob patterns.",
2828
"type": "array",
2929
"items": {
3030
"type": "string"
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"bundledPackages": ["api-extractor-lib1-test", "api-extractor-lib2.*"]
2+
"bundledPackages": ["api-extractor-lib1-test", "api-extractor-lib2*"]
33
}

0 commit comments

Comments
 (0)