@rushstack/package-extractor 0.12.0 → 0.12.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. package/CHANGELOG.json +17 -0
  2. package/CHANGELOG.md +8 -1
  3. package/dist/tsdoc-metadata.json +1 -1
  4. package/lib-commonjs/ArchiveManager.js +64 -0
  5. package/lib-commonjs/ArchiveManager.js.map +1 -0
  6. package/lib-commonjs/AssetHandler.js +171 -0
  7. package/lib-commonjs/AssetHandler.js.map +1 -0
  8. package/lib-commonjs/PackageExtractor.js +568 -0
  9. package/lib-commonjs/PackageExtractor.js.map +1 -0
  10. package/lib-commonjs/PathConstants.js +11 -0
  11. package/lib-commonjs/PathConstants.js.map +1 -0
  12. package/lib-commonjs/SymlinkAnalyzer.js +159 -0
  13. package/lib-commonjs/SymlinkAnalyzer.js.map +1 -0
  14. package/lib-commonjs/Utils.js +85 -0
  15. package/lib-commonjs/Utils.js.map +1 -0
  16. package/lib-commonjs/index.js +8 -0
  17. package/lib-commonjs/index.js.map +1 -0
  18. package/lib-commonjs/scripts/createLinks/cli/CreateLinksCommandLineParser.js +35 -0
  19. package/lib-commonjs/scripts/createLinks/cli/CreateLinksCommandLineParser.js.map +1 -0
  20. package/lib-commonjs/scripts/createLinks/cli/actions/CreateLinksAction.js +91 -0
  21. package/lib-commonjs/scripts/createLinks/cli/actions/CreateLinksAction.js.map +1 -0
  22. package/lib-commonjs/scripts/createLinks/cli/actions/RemoveLinksAction.js +38 -0
  23. package/lib-commonjs/scripts/createLinks/cli/actions/RemoveLinksAction.js.map +1 -0
  24. package/lib-commonjs/scripts/createLinks/start.js +10 -0
  25. package/lib-commonjs/scripts/createLinks/start.js.map +1 -0
  26. package/lib-commonjs/scripts/createLinks/utilities/CreateLinksUtilities.js +14 -0
  27. package/lib-commonjs/scripts/createLinks/utilities/CreateLinksUtilities.js.map +1 -0
  28. package/lib-commonjs/scripts/createLinks/utilities/constants.js +44 -0
  29. package/lib-commonjs/scripts/createLinks/utilities/constants.js.map +1 -0
  30. package/lib-dts/ArchiveManager.d.ts +13 -0
  31. package/lib-dts/ArchiveManager.d.ts.map +1 -0
  32. package/lib-dts/AssetHandler.d.ts +52 -0
  33. package/lib-dts/AssetHandler.d.ts.map +1 -0
  34. package/lib-dts/PackageExtractor.d.ts +280 -0
  35. package/lib-dts/PackageExtractor.d.ts.map +1 -0
  36. package/lib-dts/PathConstants.d.ts +4 -0
  37. package/lib-dts/PathConstants.d.ts.map +1 -0
  38. package/lib-dts/SymlinkAnalyzer.d.ts +73 -0
  39. package/lib-dts/SymlinkAnalyzer.d.ts.map +1 -0
  40. package/lib-dts/Utils.d.ts +30 -0
  41. package/lib-dts/Utils.d.ts.map +1 -0
  42. package/lib-dts/index.d.ts +3 -0
  43. package/lib-dts/index.d.ts.map +1 -0
  44. package/lib-dts/scripts/createLinks/cli/CreateLinksCommandLineParser.d.ts +8 -0
  45. package/lib-dts/scripts/createLinks/cli/CreateLinksCommandLineParser.d.ts.map +1 -0
  46. package/lib-dts/scripts/createLinks/cli/actions/CreateLinksAction.d.ts +10 -0
  47. package/lib-dts/scripts/createLinks/cli/actions/CreateLinksAction.d.ts.map +1 -0
  48. package/lib-dts/scripts/createLinks/cli/actions/RemoveLinksAction.d.ts +10 -0
  49. package/lib-dts/scripts/createLinks/cli/actions/RemoveLinksAction.d.ts.map +1 -0
  50. package/lib-dts/scripts/createLinks/start.d.ts +2 -0
  51. package/lib-dts/scripts/createLinks/start.d.ts.map +1 -0
  52. package/lib-dts/scripts/createLinks/utilities/CreateLinksUtilities.d.ts +3 -0
  53. package/lib-dts/scripts/createLinks/utilities/CreateLinksUtilities.d.ts.map +1 -0
  54. package/lib-dts/scripts/createLinks/utilities/constants.d.ts +34 -0
  55. package/lib-dts/scripts/createLinks/utilities/constants.d.ts.map +1 -0
  56. package/lib-esm/ArchiveManager.js +57 -0
  57. package/lib-esm/ArchiveManager.js.map +1 -0
  58. package/lib-esm/AssetHandler.js +164 -0
  59. package/lib-esm/AssetHandler.js.map +1 -0
  60. package/lib-esm/PackageExtractor.js +528 -0
  61. package/lib-esm/PackageExtractor.js.map +1 -0
  62. package/lib-esm/PathConstants.js +8 -0
  63. package/lib-esm/PathConstants.js.map +1 -0
  64. package/lib-esm/SymlinkAnalyzer.js +122 -0
  65. package/lib-esm/SymlinkAnalyzer.js.map +1 -0
  66. package/lib-esm/Utils.js +76 -0
  67. package/lib-esm/Utils.js.map +1 -0
  68. package/lib-esm/index.js +4 -0
  69. package/lib-esm/index.js.map +1 -0
  70. package/lib-esm/scripts/createLinks/cli/CreateLinksCommandLineParser.js +31 -0
  71. package/lib-esm/scripts/createLinks/cli/CreateLinksCommandLineParser.js.map +1 -0
  72. package/lib-esm/scripts/createLinks/cli/actions/CreateLinksAction.js +84 -0
  73. package/lib-esm/scripts/createLinks/cli/actions/CreateLinksAction.js.map +1 -0
  74. package/lib-esm/scripts/createLinks/cli/actions/RemoveLinksAction.js +30 -0
  75. package/lib-esm/scripts/createLinks/cli/actions/RemoveLinksAction.js.map +1 -0
  76. package/lib-esm/scripts/createLinks/start.js +8 -0
  77. package/lib-esm/scripts/createLinks/start.js.map +1 -0
  78. package/lib-esm/scripts/createLinks/utilities/CreateLinksUtilities.js +11 -0
  79. package/lib-esm/scripts/createLinks/utilities/CreateLinksUtilities.js.map +1 -0
  80. package/lib-esm/scripts/createLinks/utilities/constants.js +38 -0
  81. package/lib-esm/scripts/createLinks/utilities/constants.js.map +1 -0
  82. package/package.json +5 -5
@@ -0,0 +1,528 @@
1
+ // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2
+ // See LICENSE in the project root for license information.
3
+ import * as path from 'node:path';
4
+ import { Minimatch } from 'minimatch';
5
+ import semver from 'semver';
6
+ import npmPacklist from 'npm-packlist';
7
+ import ignore from 'ignore';
8
+ import { Async, AsyncQueue, Path, FileSystem, Import, JsonFile } from '@rushstack/node-core-library';
9
+ import { Colorize } from '@rushstack/terminal';
10
+ import { SymlinkAnalyzer } from './SymlinkAnalyzer';
11
+ import { AssetHandler } from './AssetHandler';
12
+ import { matchesWithStar, remapSourcePathForTargetFolder, remapPathForExtractorMetadata, makeBinLinksAsync } from './Utils';
13
+ import { CREATE_LINKS_SCRIPT_FILENAME, EXTRACTOR_METADATA_FILENAME, SCRIPTS_FOLDER_PATH } from './PathConstants';
14
+ import { MAX_CONCURRENCY } from './scripts/createLinks/utilities/constants';
15
+ export const TARGET_ROOT_SCRIPT_RELATIVE_PATH_TEMPLATE_STRING = '{TARGET_ROOT_SCRIPT_RELATIVE_PATH}';
16
+ /**
17
+ * Manages the business logic for the "rush deploy" command.
18
+ *
19
+ * @public
20
+ */
21
+ export class PackageExtractor {
22
+ /**
23
+ * Get a list of files that would be included in a package created from the provided package root path.
24
+ *
25
+ * @beta
26
+ */
27
+ static async getPackageIncludedFilesAsync(packageRootPath) {
28
+ // Use npm-packlist to filter the files. Using the Walker class (instead of the default API) ensures
29
+ // that "bundledDependencies" are not included.
30
+ const walkerPromise = new Promise((resolve, reject) => {
31
+ const walker = new npmPacklist.Walker({
32
+ path: packageRootPath
33
+ });
34
+ walker.on('done', resolve).on('error', reject).start();
35
+ });
36
+ const npmPackFiles = await walkerPromise;
37
+ return npmPackFiles;
38
+ }
39
+ /**
40
+ * Extract a package using the provided options
41
+ */
42
+ async extractAsync(options) {
43
+ options = PackageExtractor._normalizeOptions(options);
44
+ const { terminal, projectConfigurations, sourceRootFolder, targetRootFolder, mainProjectName, overwriteExisting, dependencyConfigurations, linkCreation } = options;
45
+ terminal.writeLine(Colorize.cyan(`Extracting to target folder: ${targetRootFolder}`));
46
+ terminal.writeLine(Colorize.cyan(`Main project for extraction: ${mainProjectName}`));
47
+ await FileSystem.ensureFolderAsync(targetRootFolder);
48
+ const existingExtraction = (await FileSystem.readFolderItemNamesAsync(targetRootFolder)).length > 0;
49
+ if (existingExtraction) {
50
+ if (!overwriteExisting) {
51
+ throw new Error('The extraction target folder is not empty. Overwrite must be explicitly requested');
52
+ }
53
+ terminal.writeLine('Deleting target folder contents...');
54
+ terminal.writeLine('');
55
+ await FileSystem.ensureEmptyFolderAsync(targetRootFolder);
56
+ }
57
+ // Create a new state for each run
58
+ const symlinkAnalyzer = new SymlinkAnalyzer({
59
+ requiredSourceParentPath: sourceRootFolder
60
+ });
61
+ const state = {
62
+ symlinkAnalyzer,
63
+ assetHandler: new AssetHandler({ ...options, symlinkAnalyzer }),
64
+ foldersToCopy: new Set(),
65
+ packageJsonByPath: new Map(),
66
+ projectConfigurationsByName: new Map(projectConfigurations.map((p) => [p.projectName, p])),
67
+ projectConfigurationsByPath: new Map(projectConfigurations.map((p) => [p.projectFolder, p])),
68
+ dependencyConfigurationsByName: new Map()
69
+ };
70
+ // set state dependencyConfigurationsByName
71
+ for (const dependencyConfiguration of dependencyConfigurations || []) {
72
+ const { dependencyName } = dependencyConfiguration;
73
+ let existingDependencyConfigurations = state.dependencyConfigurationsByName.get(dependencyName);
74
+ if (!existingDependencyConfigurations) {
75
+ existingDependencyConfigurations = [];
76
+ state.dependencyConfigurationsByName.set(dependencyName, existingDependencyConfigurations);
77
+ }
78
+ existingDependencyConfigurations.push(dependencyConfiguration);
79
+ }
80
+ await this._performExtractionAsync(options, state);
81
+ await state.assetHandler.finalizeAsync({
82
+ onAfterExtractSymlinksAsync: async () => {
83
+ // We need the symlinks to be created before attempting to create the bin links, since it requires
84
+ // the node_modules folder to be realized. While we're here, we may as well perform some specific
85
+ // link creation tasks and write the extractor-metadata.json file before the asset handler finalizes.
86
+ if (linkCreation === 'default') {
87
+ await this._makeBinLinksAsync(options, state);
88
+ }
89
+ else if (linkCreation === 'script') {
90
+ await this._writeCreateLinksScriptAsync(options, state);
91
+ }
92
+ terminal.writeLine('Creating extractor-metadata.json');
93
+ await this._writeExtractorMetadataAsync(options, state);
94
+ }
95
+ });
96
+ }
97
+ static _normalizeOptions(options) {
98
+ if (options.subspaces) {
99
+ if (options.pnpmInstallFolder !== undefined) {
100
+ throw new Error('IExtractorOptions.pnpmInstallFolder cannot be combined with IExtractorOptions.subspaces');
101
+ }
102
+ if (options.transformPackageJson !== undefined) {
103
+ throw new Error('IExtractorOptions.transformPackageJson cannot be combined with IExtractorOptions.subspaces');
104
+ }
105
+ return options;
106
+ }
107
+ const normalizedOptions = { ...options };
108
+ delete normalizedOptions.pnpmInstallFolder;
109
+ delete normalizedOptions.transformPackageJson;
110
+ normalizedOptions.subspaces = [
111
+ {
112
+ subspaceName: 'default',
113
+ pnpmInstallFolder: options.pnpmInstallFolder,
114
+ transformPackageJson: options.transformPackageJson
115
+ }
116
+ ];
117
+ return normalizedOptions;
118
+ }
119
+ async _performExtractionAsync(options, state) {
120
+ const { terminal, mainProjectName, sourceRootFolder, targetRootFolder, folderToCopy: additionalFolderToCopy, createArchiveOnly } = options;
121
+ const { projectConfigurationsByName, foldersToCopy } = state;
122
+ const mainProjectConfiguration = projectConfigurationsByName.get(mainProjectName);
123
+ if (!mainProjectConfiguration) {
124
+ throw new Error(`Main project "${mainProjectName}" was not found in the list of projects`);
125
+ }
126
+ // Calculate the set with additionalProjectsToInclude
127
+ const includedProjectsSet = new Set([mainProjectConfiguration]);
128
+ for (const { additionalProjectsToInclude } of includedProjectsSet) {
129
+ if (additionalProjectsToInclude) {
130
+ for (const additionalProjectNameToInclude of additionalProjectsToInclude) {
131
+ const additionalProjectToInclude = projectConfigurationsByName.get(additionalProjectNameToInclude);
132
+ if (!additionalProjectToInclude) {
133
+ throw new Error(`Project "${additionalProjectNameToInclude}" was not found in the list of projects.`);
134
+ }
135
+ includedProjectsSet.add(additionalProjectToInclude);
136
+ }
137
+ }
138
+ }
139
+ for (const { projectName, projectFolder } of includedProjectsSet) {
140
+ terminal.writeLine(Colorize.cyan(`Analyzing project: ${projectName}`));
141
+ await this._collectFoldersAsync(projectFolder, options, state);
142
+ }
143
+ if (!createArchiveOnly) {
144
+ terminal.writeLine(`Copying folders to target folder "${targetRootFolder}"`);
145
+ }
146
+ await Async.forEachAsync(foldersToCopy, async (folderToCopy) => {
147
+ await this._extractFolderAsync(folderToCopy, options, state);
148
+ }, {
149
+ concurrency: MAX_CONCURRENCY
150
+ });
151
+ if (additionalFolderToCopy) {
152
+ // Copy the additional folder directly into the root of the target folder by setting the sourceRootFolder
153
+ // to the root of the folderToCopy
154
+ const additionalFolderPath = path.resolve(sourceRootFolder, additionalFolderToCopy);
155
+ const additionalFolderExtractorOptions = {
156
+ ...options,
157
+ sourceRootFolder: additionalFolderPath,
158
+ targetRootFolder
159
+ };
160
+ await this._extractFolderAsync(additionalFolderPath, additionalFolderExtractorOptions, state);
161
+ }
162
+ }
163
+ /**
164
+ * Recursively crawl the node_modules dependencies and collect the result in IExtractorState.foldersToCopy.
165
+ */
166
+ async _collectFoldersAsync(packageJsonFolder, options, state) {
167
+ const { terminal, subspaces } = options;
168
+ const { projectConfigurationsByPath } = state;
169
+ const packageJsonFolderPathQueue = new AsyncQueue([packageJsonFolder]);
170
+ await Async.forEachAsync(packageJsonFolderPathQueue, async ([packageJsonFolderPath, callback]) => {
171
+ var _a, _b;
172
+ const packageJsonRealFolderPath = await FileSystem.getRealPathAsync(packageJsonFolderPath);
173
+ if (state.foldersToCopy.has(packageJsonRealFolderPath)) {
174
+ // we've already seen this folder
175
+ callback();
176
+ return;
177
+ }
178
+ state.foldersToCopy.add(packageJsonRealFolderPath);
179
+ const originalPackageJson = await JsonFile.loadAsync(path.join(packageJsonRealFolderPath, 'package.json'));
180
+ const targetSubspace = subspaces === null || subspaces === void 0 ? void 0 : subspaces.find((subspace) => subspace.pnpmInstallFolder && Path.isUnder(packageJsonFolderPath, subspace.pnpmInstallFolder));
181
+ // Transform packageJson using the provided transformer, if requested
182
+ const packageJson = (_b = (_a = targetSubspace === null || targetSubspace === void 0 ? void 0 : targetSubspace.transformPackageJson) === null || _a === void 0 ? void 0 : _a.call(targetSubspace, originalPackageJson)) !== null && _b !== void 0 ? _b : originalPackageJson;
183
+ state.packageJsonByPath.set(packageJsonRealFolderPath, packageJson);
184
+ // Union of keys from regular dependencies, peerDependencies, optionalDependencies
185
+ // (and possibly devDependencies if includeDevDependencies=true)
186
+ const dependencyNamesToProcess = new Set();
187
+ // Just the keys from optionalDependencies and peerDependencies
188
+ const optionalDependencyNames = new Set();
189
+ for (const name of Object.keys(packageJson.dependencies || {})) {
190
+ dependencyNamesToProcess.add(name);
191
+ }
192
+ for (const name of Object.keys(packageJson.peerDependencies || {})) {
193
+ dependencyNamesToProcess.add(name);
194
+ optionalDependencyNames.add(name); // consider peers optional, since they are so frequently broken
195
+ }
196
+ for (const name of Object.keys(packageJson.optionalDependencies || {})) {
197
+ dependencyNamesToProcess.add(name);
198
+ optionalDependencyNames.add(name);
199
+ }
200
+ // Check to see if this is a local project
201
+ const projectConfiguration = projectConfigurationsByPath.get(packageJsonRealFolderPath);
202
+ if (projectConfiguration) {
203
+ if (options.includeDevDependencies) {
204
+ for (const name of Object.keys(packageJson.devDependencies || {})) {
205
+ dependencyNamesToProcess.add(name);
206
+ }
207
+ }
208
+ this._applyDependencyFilters(terminal, dependencyNamesToProcess, projectConfiguration.additionalDependenciesToInclude, projectConfiguration.dependenciesToExclude);
209
+ }
210
+ for (const dependencyPackageName of dependencyNamesToProcess) {
211
+ try {
212
+ const dependencyPackageFolderPath = await Import.resolvePackageAsync({
213
+ packageName: dependencyPackageName,
214
+ baseFolderPath: packageJsonRealFolderPath,
215
+ getRealPathAsync: async (filePath) => {
216
+ try {
217
+ return (await state.symlinkAnalyzer.analyzePathAsync({ inputPath: filePath })).nodePath;
218
+ }
219
+ catch (error) {
220
+ if (FileSystem.isFileDoesNotExistError(error)) {
221
+ return filePath;
222
+ }
223
+ throw error;
224
+ }
225
+ }
226
+ });
227
+ packageJsonFolderPathQueue.push(dependencyPackageFolderPath);
228
+ }
229
+ catch (resolveErr) {
230
+ if (optionalDependencyNames.has(dependencyPackageName)) {
231
+ // Ignore missing optional dependency
232
+ continue;
233
+ }
234
+ throw resolveErr;
235
+ }
236
+ }
237
+ // Replicate the links to the virtual store. Note that if the package has not been hoisted by
238
+ // PNPM, the package will not be resolvable from here.
239
+ // Only apply this logic for packages that were actually installed under the common/temp folder.
240
+ const realPnpmInstallFolder = targetSubspace === null || targetSubspace === void 0 ? void 0 : targetSubspace.pnpmInstallFolder;
241
+ if (realPnpmInstallFolder && Path.isUnder(packageJsonFolderPath, realPnpmInstallFolder)) {
242
+ try {
243
+ // The PNPM virtual store links are created in this folder. We will resolve the current package
244
+ // from that location and collect any additional links encountered along the way.
245
+ // TODO: This can be configured via NPMRC. We should support that.
246
+ const pnpmDotFolderPath = path.join(realPnpmInstallFolder, 'node_modules', '.pnpm');
247
+ // TODO: Investigate how package aliases are handled by PNPM in this case. For example:
248
+ //
249
+ // "dependencies": {
250
+ // "alias-name": "npm:real-name@^1.2.3"
251
+ // }
252
+ const dependencyPackageFolderPath = await Import.resolvePackageAsync({
253
+ packageName: packageJson.name,
254
+ baseFolderPath: pnpmDotFolderPath,
255
+ getRealPathAsync: async (filePath) => {
256
+ try {
257
+ return (await state.symlinkAnalyzer.analyzePathAsync({ inputPath: filePath })).nodePath;
258
+ }
259
+ catch (error) {
260
+ if (FileSystem.isFileDoesNotExistError(error)) {
261
+ return filePath;
262
+ }
263
+ throw error;
264
+ }
265
+ }
266
+ });
267
+ packageJsonFolderPathQueue.push(dependencyPackageFolderPath);
268
+ }
269
+ catch (resolveErr) {
270
+ // The virtual store link isn't guaranteed to exist, so ignore if it's missing
271
+ // NOTE: If you encounter this warning a lot, please report it to the Rush maintainers.
272
+ // eslint-disable-next-line no-console
273
+ console.log('Ignoring missing PNPM virtual store link for ' + packageJsonFolderPath);
274
+ }
275
+ }
276
+ callback();
277
+ }, {
278
+ concurrency: MAX_CONCURRENCY
279
+ });
280
+ }
281
+ _applyDependencyFilters(terminal, allDependencyNames, additionalDependenciesToInclude = [], dependenciesToExclude = []) {
282
+ // Track packages that got added/removed for reporting purposes
283
+ const extraIncludedPackageNames = [];
284
+ const extraExcludedPackageNames = [];
285
+ for (const patternWithStar of dependenciesToExclude) {
286
+ for (const dependency of allDependencyNames) {
287
+ if (matchesWithStar(patternWithStar, dependency)) {
288
+ if (allDependencyNames.delete(dependency)) {
289
+ extraExcludedPackageNames.push(dependency);
290
+ }
291
+ }
292
+ }
293
+ }
294
+ for (const dependencyToInclude of additionalDependenciesToInclude) {
295
+ if (!allDependencyNames.has(dependencyToInclude)) {
296
+ allDependencyNames.add(dependencyToInclude);
297
+ extraIncludedPackageNames.push(dependencyToInclude);
298
+ }
299
+ }
300
+ if (extraIncludedPackageNames.length > 0) {
301
+ extraIncludedPackageNames.sort();
302
+ terminal.writeLine(`Extra dependencies included by settings: ${extraIncludedPackageNames.join(', ')}`);
303
+ }
304
+ if (extraExcludedPackageNames.length > 0) {
305
+ extraExcludedPackageNames.sort();
306
+ terminal.writeLine(`Extra dependencies excluded by settings: ${extraExcludedPackageNames.join(', ')}`);
307
+ }
308
+ return allDependencyNames;
309
+ }
310
+ /**
311
+ * Copy one package folder to the extractor target folder.
312
+ */
313
+ async _extractFolderAsync(sourceFolderPath, options, state) {
314
+ const { includeNpmIgnoreFiles } = options;
315
+ const { projectConfigurationsByPath, packageJsonByPath, dependencyConfigurationsByName, assetHandler } = state;
316
+ let useNpmIgnoreFilter = false;
317
+ const sourceFolderRealPath = await FileSystem.getRealPathAsync(sourceFolderPath);
318
+ const sourceProjectConfiguration = projectConfigurationsByPath.get(sourceFolderRealPath);
319
+ const packagesJson = packageJsonByPath.get(sourceFolderRealPath);
320
+ // As this function will be used to copy folder for both project inside monorepo and third party
321
+ // dependencies insides node_modules. Third party dependencies won't have project configurations
322
+ const isLocalProject = !!sourceProjectConfiguration;
323
+ // Function to filter files inside local project or third party dependencies.
324
+ const isFileExcluded = (filePath) => {
325
+ // Encapsulate exclude logic into a function, so it can be reused.
326
+ const excludeFileByPatterns = (patternsToInclude, patternsToExclude) => {
327
+ let includeFilters;
328
+ let excludeFilters;
329
+ if (patternsToInclude === null || patternsToInclude === void 0 ? void 0 : patternsToInclude.length) {
330
+ includeFilters = patternsToInclude === null || patternsToInclude === void 0 ? void 0 : patternsToInclude.map((p) => new Minimatch(p, { dot: true }));
331
+ }
332
+ if (patternsToExclude === null || patternsToExclude === void 0 ? void 0 : patternsToExclude.length) {
333
+ excludeFilters = patternsToExclude === null || patternsToExclude === void 0 ? void 0 : patternsToExclude.map((p) => new Minimatch(p, { dot: true }));
334
+ }
335
+ // If there are no filters, then we can't exclude anything.
336
+ if (!includeFilters && !excludeFilters) {
337
+ return false;
338
+ }
339
+ const isIncluded = !includeFilters || includeFilters.some((m) => m.match(filePath));
340
+ // If the file is not included, then we don't need to check the excludeFilter. If it is included
341
+ // and there is no exclude filter, then we know that the file is not excluded. If it is included
342
+ // and there is an exclude filter, then we need to check for a match.
343
+ return !isIncluded || !!(excludeFilters === null || excludeFilters === void 0 ? void 0 : excludeFilters.some((m) => m.match(filePath)));
344
+ };
345
+ if (isLocalProject) {
346
+ return excludeFileByPatterns(sourceProjectConfiguration === null || sourceProjectConfiguration === void 0 ? void 0 : sourceProjectConfiguration.patternsToInclude, sourceProjectConfiguration === null || sourceProjectConfiguration === void 0 ? void 0 : sourceProjectConfiguration.patternsToExclude);
347
+ }
348
+ else {
349
+ if (!packagesJson) {
350
+ return false;
351
+ }
352
+ const dependenciesConfigurations = dependencyConfigurationsByName.get(packagesJson.name);
353
+ if (!dependenciesConfigurations) {
354
+ return false;
355
+ }
356
+ const matchedDependenciesConfigurations = dependenciesConfigurations.filter((d) => semver.satisfies(packagesJson.version, d.dependencyVersionRange));
357
+ return matchedDependenciesConfigurations.some((d) => excludeFileByPatterns(d.patternsToInclude, d.patternsToExclude));
358
+ }
359
+ };
360
+ if (sourceProjectConfiguration && !includeNpmIgnoreFiles) {
361
+ // Only use the npmignore filter if the project configuration explicitly asks for it
362
+ useNpmIgnoreFilter = true;
363
+ }
364
+ const targetFolderPath = remapSourcePathForTargetFolder({
365
+ ...options,
366
+ sourcePath: sourceFolderPath
367
+ });
368
+ if (useNpmIgnoreFilter) {
369
+ const npmPackFiles = await PackageExtractor.getPackageIncludedFilesAsync(sourceFolderPath);
370
+ await Async.forEachAsync(npmPackFiles, async (npmPackFile) => {
371
+ // In issue https://github.com/microsoft/rushstack/issues/2121 we found that npm-packlist sometimes returns
372
+ // duplicate file paths, for example:
373
+ //
374
+ // 'dist//index.js'
375
+ // 'dist/index.js'
376
+ //
377
+ // Filter out files that are excluded by the project configuration or dependency configuration.
378
+ if (isFileExcluded(npmPackFile)) {
379
+ return;
380
+ }
381
+ const sourceFilePath = path.resolve(sourceFolderPath, npmPackFile);
382
+ const { kind, linkStats: sourceFileStats } = await state.symlinkAnalyzer.analyzePathAsync({
383
+ inputPath: sourceFilePath
384
+ });
385
+ if (kind === 'file') {
386
+ const targetFilePath = path.resolve(targetFolderPath, npmPackFile);
387
+ await assetHandler.includeAssetAsync({
388
+ sourceFilePath,
389
+ sourceFileStats,
390
+ targetFilePath
391
+ });
392
+ }
393
+ }, {
394
+ concurrency: MAX_CONCURRENCY
395
+ });
396
+ }
397
+ else {
398
+ // use a simplistic "ignore" ruleset to filter the files
399
+ const ignoreFilter = ignore();
400
+ ignoreFilter.add([
401
+ // The top-level node_modules folder is always excluded
402
+ '/node_modules',
403
+ // Also exclude well-known folders that can contribute a lot of unnecessary files
404
+ '**/.git',
405
+ '**/.svn',
406
+ '**/.hg',
407
+ '**/.DS_Store'
408
+ ]);
409
+ // Do a breadth-first search of the source folder, copying each file to the target folder
410
+ const queue = new AsyncQueue([sourceFolderPath]);
411
+ await Async.forEachAsync(queue, async ([sourcePath, callback]) => {
412
+ const relativeSourcePath = path.relative(sourceFolderPath, sourcePath);
413
+ if (relativeSourcePath !== '' && ignoreFilter.ignores(relativeSourcePath)) {
414
+ callback();
415
+ return;
416
+ }
417
+ const sourcePathNode = await state.symlinkAnalyzer.analyzePathAsync({
418
+ inputPath: sourcePath,
419
+ // Treat all links to external paths as if they are files for this scenario. In the future, we may
420
+ // want to explore the target of the external link to see if all files within the target are
421
+ // excluded, and throw if they are not.
422
+ shouldIgnoreExternalLink: (linkSourcePath) => {
423
+ // Ignore the provided linkSourcePath since it may not be the first link in the chain. Instead,
424
+ // we will consider only the relativeSourcePath, since that would be our entrypoint into the
425
+ // link chain.
426
+ return isFileExcluded(relativeSourcePath);
427
+ }
428
+ });
429
+ if (sourcePathNode === undefined) {
430
+ // The target was a symlink that is excluded. We don't need to do anything.
431
+ callback();
432
+ return;
433
+ }
434
+ else if (sourcePathNode.kind === 'file') {
435
+ // Only ignore files and not folders to ensure that we traverse the contents of all folders. This is
436
+ // done so that we can match against subfolder patterns, ex. "src/subfolder/**/*"
437
+ if (relativeSourcePath !== '' && isFileExcluded(relativeSourcePath)) {
438
+ callback();
439
+ return;
440
+ }
441
+ const targetFilePath = path.resolve(targetFolderPath, relativeSourcePath);
442
+ await assetHandler.includeAssetAsync({
443
+ sourceFilePath: sourcePath,
444
+ sourceFileStats: sourcePathNode.linkStats,
445
+ targetFilePath
446
+ });
447
+ }
448
+ else if (sourcePathNode.kind === 'folder') {
449
+ const children = await FileSystem.readFolderItemNamesAsync(sourcePath);
450
+ for (const child of children) {
451
+ queue.push(path.join(sourcePath, child));
452
+ }
453
+ }
454
+ callback();
455
+ }, {
456
+ concurrency: MAX_CONCURRENCY
457
+ });
458
+ }
459
+ }
460
+ /**
461
+ * Write the common/deploy/deploy-metadata.json file.
462
+ */
463
+ async _writeExtractorMetadataAsync(options, state) {
464
+ const { mainProjectName, sourceRootFolder, targetRootFolder, linkCreation, linkCreationScriptPath } = options;
465
+ const { projectConfigurationsByPath } = state;
466
+ const extractorMetadataFolderPath = linkCreation === 'script' && linkCreationScriptPath
467
+ ? path.dirname(path.resolve(targetRootFolder, linkCreationScriptPath))
468
+ : targetRootFolder;
469
+ const extractorMetadataFilePath = path.join(extractorMetadataFolderPath, EXTRACTOR_METADATA_FILENAME);
470
+ const extractorMetadataJson = {
471
+ mainProjectName,
472
+ projects: [],
473
+ links: [],
474
+ files: []
475
+ };
476
+ for (const { projectFolder, projectName } of projectConfigurationsByPath.values()) {
477
+ if (state.foldersToCopy.has(projectFolder)) {
478
+ extractorMetadataJson.projects.push({
479
+ projectName,
480
+ path: remapPathForExtractorMetadata(sourceRootFolder, projectFolder)
481
+ });
482
+ }
483
+ }
484
+ // Remap the links to be relative to target folder
485
+ for (const { kind, linkPath, targetPath } of state.symlinkAnalyzer.reportSymlinks()) {
486
+ extractorMetadataJson.links.push({
487
+ kind,
488
+ linkPath: remapPathForExtractorMetadata(sourceRootFolder, linkPath),
489
+ targetPath: remapPathForExtractorMetadata(sourceRootFolder, targetPath)
490
+ });
491
+ }
492
+ for (const assetPath of state.assetHandler.assetPaths) {
493
+ extractorMetadataJson.files.push(remapPathForExtractorMetadata(targetRootFolder, assetPath));
494
+ }
495
+ const extractorMetadataFileContent = JSON.stringify(extractorMetadataJson, undefined, 0);
496
+ await state.assetHandler.includeAssetAsync({
497
+ sourceFileContent: extractorMetadataFileContent,
498
+ targetFilePath: extractorMetadataFilePath
499
+ });
500
+ }
501
+ async _makeBinLinksAsync(options, state) {
502
+ const { terminal } = options;
503
+ const extractedProjectFolderPaths = [];
504
+ for (const folderPath of state.projectConfigurationsByPath.keys()) {
505
+ if (state.foldersToCopy.has(folderPath)) {
506
+ extractedProjectFolderPaths.push(remapSourcePathForTargetFolder({ ...options, sourcePath: folderPath }));
507
+ }
508
+ }
509
+ const binFilePaths = await makeBinLinksAsync(terminal, extractedProjectFolderPaths);
510
+ await Async.forEachAsync(binFilePaths, (targetFilePath) => state.assetHandler.includeAssetAsync({ targetFilePath }), {
511
+ concurrency: MAX_CONCURRENCY
512
+ });
513
+ }
514
+ async _writeCreateLinksScriptAsync(options, state) {
515
+ const { terminal, targetRootFolder, linkCreationScriptPath } = options;
516
+ const { assetHandler } = state;
517
+ terminal.writeLine(`Creating ${CREATE_LINKS_SCRIPT_FILENAME}`);
518
+ const createLinksSourceFilePath = `${SCRIPTS_FOLDER_PATH}/${CREATE_LINKS_SCRIPT_FILENAME}`;
519
+ const createLinksTargetFilePath = path.resolve(targetRootFolder, linkCreationScriptPath || CREATE_LINKS_SCRIPT_FILENAME);
520
+ let createLinksScriptContent = await FileSystem.readFileAsync(createLinksSourceFilePath);
521
+ createLinksScriptContent = createLinksScriptContent.replace(TARGET_ROOT_SCRIPT_RELATIVE_PATH_TEMPLATE_STRING, Path.convertToSlashes(path.relative(path.dirname(createLinksTargetFilePath), targetRootFolder)));
522
+ await assetHandler.includeAssetAsync({
523
+ sourceFileContent: createLinksScriptContent,
524
+ targetFilePath: createLinksTargetFilePath
525
+ });
526
+ }
527
+ }
528
+ //# sourceMappingURL=PackageExtractor.js.map