@teambit/dependency-resolver 1.0.107 → 1.0.108

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 (78) hide show
  1. package/apply-updates.spec.ts +215 -0
  2. package/apply-updates.ts +81 -0
  3. package/dependency-detector.ts +5 -0
  4. package/dependency-env.ts +6 -0
  5. package/dependency-installer.ts +347 -0
  6. package/dependency-linker.ts +719 -0
  7. package/dependency-resolver.aspect.ts +7 -0
  8. package/dependency-resolver.graphql.ts +99 -0
  9. package/dependency-resolver.main.runtime.spec.ts +530 -0
  10. package/dependency-resolver.main.runtime.ts +1797 -0
  11. package/dependency-version-resolver.ts +42 -0
  12. package/dist/apply-updates.js +2 -4
  13. package/dist/apply-updates.js.map +1 -1
  14. package/dist/dependencies/base-dependency.d.ts +7 -7
  15. package/dist/dependencies/base-dependency.js +1 -2
  16. package/dist/dependencies/base-dependency.js.map +1 -1
  17. package/dist/dependencies/component-dependency/component-dependency.js +1 -2
  18. package/dist/dependencies/component-dependency/component-dependency.js.map +1 -1
  19. package/dist/dependencies/dependency-list-factory.js +2 -4
  20. package/dist/dependencies/dependency-list-factory.js.map +1 -1
  21. package/dist/dependencies/dependency-list.d.ts +2 -2
  22. package/dist/dependencies/dependency-list.js +1 -4
  23. package/dist/dependencies/dependency-list.js.map +1 -1
  24. package/dist/dependencies/dependency.d.ts +5 -5
  25. package/dist/dependencies.service.d.ts +4 -4
  26. package/dist/dependencies.service.js +3 -3
  27. package/dist/dependencies.service.js.map +1 -1
  28. package/dist/dependency-installer.d.ts +10 -9
  29. package/dist/dependency-installer.js +5 -8
  30. package/dist/dependency-installer.js.map +1 -1
  31. package/dist/dependency-linker.d.ts +10 -9
  32. package/dist/dependency-linker.js +7 -11
  33. package/dist/dependency-linker.js.map +1 -1
  34. package/dist/dependency-resolver.composition.d.ts +2 -2
  35. package/dist/dependency-resolver.main.runtime.d.ts +15 -15
  36. package/dist/dependency-resolver.main.runtime.js +33 -39
  37. package/dist/dependency-resolver.main.runtime.js.map +1 -1
  38. package/dist/dependency-version-resolver.d.ts +2 -1
  39. package/dist/get-all-policy-pkgs.d.ts +4 -4
  40. package/dist/manifest/deduping/dedupe-dependencies.d.ts +4 -4
  41. package/dist/manifest/deduping/dedupe-dependencies.js +1 -1
  42. package/dist/manifest/deduping/dedupe-dependencies.js.map +1 -1
  43. package/dist/manifest/deduping/hoist-dependencies.js +2 -3
  44. package/dist/manifest/deduping/hoist-dependencies.js.map +1 -1
  45. package/dist/manifest/deduping/hoist-dependencies.spec.js +6 -8
  46. package/dist/manifest/deduping/hoist-dependencies.spec.js.map +1 -1
  47. package/dist/manifest/deduping/index-by-dep-id.d.ts +4 -4
  48. package/dist/manifest/deduping/index-by-dep-id.js +1 -2
  49. package/dist/manifest/deduping/index-by-dep-id.js.map +1 -1
  50. package/dist/manifest/manifest.d.ts +4 -4
  51. package/dist/manifest/update-dependency-version.js +2 -2
  52. package/dist/manifest/update-dependency-version.js.map +1 -1
  53. package/dist/manifest/workspace-manifest-factory.d.ts +3 -3
  54. package/dist/manifest/workspace-manifest-factory.js +3 -5
  55. package/dist/manifest/workspace-manifest-factory.js.map +1 -1
  56. package/dist/manifest/workspace-manifest.js +1 -2
  57. package/dist/manifest/workspace-manifest.js.map +1 -1
  58. package/dist/package-manager-legacy.d.ts +1 -1
  59. package/dist/package-manager.d.ts +5 -5
  60. package/dist/policy/env-policy/env-policy.d.ts +7 -7
  61. package/dist/policy/policy.d.ts +9 -9
  62. package/dist/policy/variant-policy/variant-policy.d.ts +9 -9
  63. package/dist/policy/workspace-policy/workspace-policy.d.ts +11 -11
  64. package/dist/{preview-1703590665075.js → preview-1703647408454.js} +2 -2
  65. package/dist/registry/registry.d.ts +6 -6
  66. package/dist/show-fragments/serialize-by-lifecycle.js +1 -2
  67. package/dist/show-fragments/serialize-by-lifecycle.js.map +1 -1
  68. package/dist/types.d.ts +8 -8
  69. package/extend-with-components-from-dir.ts +29 -0
  70. package/get-all-policy-pkgs.spec.ts +82 -0
  71. package/get-all-policy-pkgs.ts +126 -0
  72. package/index.ts +70 -0
  73. package/package-manager-legacy.ts +137 -0
  74. package/package-manager.ts +174 -0
  75. package/package.json +27 -34
  76. package/tsconfig.json +16 -21
  77. package/types/asset.d.ts +15 -3
  78. package/types.ts +77 -0
@@ -0,0 +1,1797 @@
1
+ import multimatch from 'multimatch';
2
+ import mapSeries from 'p-map-series';
3
+ import { MainRuntime } from '@teambit/cli';
4
+ import { getAllCoreAspectsIds } from '@teambit/bit';
5
+ import { getRelativeRootComponentDir } from '@teambit/bit-roots';
6
+ import ComponentAspect, { Component, ComponentMap, ComponentMain, IComponent } from '@teambit/component';
7
+ import type { ConfigMain } from '@teambit/config';
8
+ import { join } from 'path';
9
+ import { compact, get, pick, uniq, omit } from 'lodash';
10
+ import { ConfigAspect } from '@teambit/config';
11
+ import { DependenciesEnv, EnvDefinition, EnvsAspect, EnvsMain } from '@teambit/envs';
12
+ import { Slot, SlotRegistry, ExtensionManifest, Aspect, RuntimeManifest } from '@teambit/harmony';
13
+ import { RequireableComponent } from '@teambit/harmony.modules.requireable-component';
14
+ import type { LoggerMain } from '@teambit/logger';
15
+ import { GraphqlAspect, GraphqlMain } from '@teambit/graphql';
16
+ import { Logger, LoggerAspect } from '@teambit/logger';
17
+ import {
18
+ CFG_PACKAGE_MANAGER_CACHE,
19
+ CFG_REGISTRY_URL_KEY,
20
+ CFG_USER_TOKEN_KEY,
21
+ CFG_ISOLATED_SCOPE_CAPSULES,
22
+ getCloudDomain,
23
+ } from '@teambit/legacy/dist/constants';
24
+ import { ExtensionDataList } from '@teambit/legacy/dist/consumer/config/extension-data';
25
+ import componentIdToPackageName from '@teambit/legacy/dist/utils/bit/component-id-to-package-name';
26
+ import { DetectorHook } from '@teambit/legacy/dist/consumer/component/dependencies/files-dependency-builder/detector-hook';
27
+ import { Http, ProxyConfig, NetworkConfig } from '@teambit/legacy/dist/scope/network/http';
28
+ import { onTagIdTransformer } from '@teambit/snapping';
29
+ import LegacyComponent from '@teambit/legacy/dist/consumer/component';
30
+ import fs from 'fs-extra';
31
+ import { ComponentID } from '@teambit/component-id';
32
+ import { readCAFileSync } from '@pnpm/network.ca-file';
33
+ import { SourceFile } from '@teambit/legacy/dist/consumer/component/sources';
34
+ import { PeerDependencyRules, ProjectManifest } from '@pnpm/types';
35
+ import semver, { SemVer } from 'semver';
36
+ import AspectLoaderAspect, { AspectLoaderMain } from '@teambit/aspect-loader';
37
+ import GlobalConfigAspect, { GlobalConfigMain } from '@teambit/global-config';
38
+ import { PackageJsonTransformer } from '@teambit/workspace.modules.node-modules-linker';
39
+ import { Registries, Registry } from './registry';
40
+ import { applyUpdates, UpdatedComponent } from './apply-updates';
41
+ import { ROOT_NAME } from './dependencies/constants';
42
+ import {
43
+ DependencyInstaller,
44
+ PreInstallSubscriberList,
45
+ PostInstallSubscriberList,
46
+ DepInstallerContext,
47
+ } from './dependency-installer';
48
+ import { DependencyResolverAspect } from './dependency-resolver.aspect';
49
+ import { DependencyVersionResolver } from './dependency-version-resolver';
50
+ import { DepLinkerContext, DependencyLinker, LinkingOptions } from './dependency-linker';
51
+ import { ComponentModelVersion, getAllPolicyPkgs, OutdatedPkg, CurrentPkgSource } from './get-all-policy-pkgs';
52
+ import { InvalidVersionWithPrefix, PackageManagerNotFound } from './exceptions';
53
+ import {
54
+ CreateFromComponentsOptions,
55
+ WorkspaceManifest,
56
+ WorkspaceManifestFactory,
57
+ ManifestDependenciesObject,
58
+ } from './manifest';
59
+ import {
60
+ WorkspacePolicyConfigObject,
61
+ VariantPolicyConfigObject,
62
+ WorkspacePolicy,
63
+ WorkspacePolicyFactory,
64
+ VariantPolicy,
65
+ WorkspacePolicyAddEntryOptions,
66
+ WorkspacePolicyEntry,
67
+ SerializedVariantPolicy,
68
+ } from './policy';
69
+ import {
70
+ PackageImportMethod,
71
+ PackageManager,
72
+ PeerDependencyIssuesByProjects,
73
+ PackageManagerGetPeerDependencyIssuesOptions,
74
+ } from './package-manager';
75
+
76
+ import {
77
+ SerializedDependency,
78
+ DependencyListFactory,
79
+ DependencyFactory,
80
+ ComponentDependencyFactory,
81
+ COMPONENT_DEP_TYPE,
82
+ DependencyList,
83
+ ComponentDependency,
84
+ } from './dependencies';
85
+ import { DependenciesFragment, DevDependenciesFragment, PeerDependenciesFragment } from './show-fragments';
86
+ import { dependencyResolverSchema } from './dependency-resolver.graphql';
87
+ import { DependencyDetector } from './dependency-detector';
88
+ import { DependenciesService } from './dependencies.service';
89
+ import { EnvPolicy } from './policy/env-policy';
90
+
91
+ export const BIT_CLOUD_REGISTRY = `https://node-registry.${getCloudDomain()}/`;
92
+ export const NPM_REGISTRY = 'https://registry.npmjs.org/';
93
+
94
+ export { ProxyConfig, NetworkConfig } from '@teambit/legacy/dist/scope/network/http';
95
+
96
+ export type NodeLinker = 'hoisted' | 'isolated';
97
+
98
+ export interface DependencyResolverComponentData {
99
+ packageName: string;
100
+ policy: SerializedVariantPolicy;
101
+ dependencies: SerializedDependency;
102
+ }
103
+
104
+ export interface DependencyResolverWorkspaceConfig {
105
+ policy: WorkspacePolicyConfigObject;
106
+ /**
107
+ * choose the package manager for Bit to use. you can choose between 'npm', 'yarn', 'pnpm'
108
+ * and 'librarian'. our recommendation is use 'librarian' which reduces package duplicates
109
+ * and totally removes the need of a 'node_modules' directory in your project.
110
+ */
111
+ packageManager: string;
112
+
113
+ /**
114
+ * A proxy server for out going network requests by the package manager
115
+ * Used for both http and https requests (unless the httpsProxy is defined)
116
+ */
117
+ proxy?: string;
118
+
119
+ /**
120
+ * A proxy server for outgoing https requests by the package manager (fallback to proxy server if not defined)
121
+ * Use this in case you want different proxy for http and https requests.
122
+ */
123
+ httpsProxy?: string;
124
+
125
+ /**
126
+ * A path to a file containing one or multiple Certificate Authority signing certificates.
127
+ * allows for multiple CA's, as well as for the CA information to be stored in a file on disk.
128
+ */
129
+ ca?: string;
130
+
131
+ /**
132
+ * Whether or not to do SSL key validation when making requests to the registry via https
133
+ */
134
+ strictSsl?: string;
135
+
136
+ /**
137
+ * A client certificate to pass when accessing the registry. Values should be in PEM format (Windows calls it "Base-64 encoded X.509 (.CER)") with newlines replaced by the string "\n". For example:
138
+ * cert="-----BEGIN CERTIFICATE-----\nXXXX\nXXXX\n-----END CERTIFICATE-----"
139
+ * It is not the path to a certificate file (and there is no "certfile" option).
140
+ */
141
+ cert?: string;
142
+
143
+ /**
144
+ * A client key to pass when accessing the registry. Values should be in PEM format with newlines replaced by the string "\n". For example:
145
+ * key="-----BEGIN PRIVATE KEY-----\nXXXX\nXXXX\n-----END PRIVATE KEY-----"
146
+ * It is not the path to a key file (and there is no "keyfile" option).
147
+ */
148
+ key?: string;
149
+
150
+ /**
151
+ * A comma-separated string of domain extensions that a proxy should not be used for.
152
+ */
153
+ noProxy?: string;
154
+
155
+ /**
156
+ * The IP address of the local interface to use when making connections to the npm registry.
157
+ */
158
+ localAddress?: string;
159
+
160
+ /**
161
+ * How many times to retry if Bit fails to fetch from the registry.
162
+ */
163
+ fetchRetries?: number;
164
+
165
+ /*
166
+ * The exponential factor for retry backoff.
167
+ */
168
+ fetchRetryFactor?: number;
169
+
170
+ /*
171
+ * The minimum (base) timeout for retrying requests.
172
+ */
173
+ fetchRetryMintimeout?: number;
174
+
175
+ /*
176
+ * The maximum fallback timeout to ensure the retry factor does not make requests too long.
177
+ */
178
+ fetchRetryMaxtimeout?: number;
179
+
180
+ /*
181
+ * The maximum amount of time (in milliseconds) to wait for HTTP requests to complete.
182
+ */
183
+ fetchTimeout?: number;
184
+
185
+ /*
186
+ * The maximum number of connections to use per origin (protocol/host/port combination).
187
+ */
188
+ maxSockets?: number;
189
+
190
+ /*
191
+ * Controls the maximum number of HTTP(S) requests to process simultaneously.
192
+ */
193
+ networkConcurrency?: number;
194
+
195
+ /*
196
+ * Set the prefix to use when adding dependency to workspace.jsonc via bit install
197
+ * to lock version to exact version you can use empty string (default)
198
+ */
199
+ savePrefix?: string;
200
+
201
+ /*
202
+ * in case you want to disable this proxy set this config to false
203
+ *
204
+ */
205
+ installFromBitDevRegistry?: boolean;
206
+
207
+ /*
208
+ * map of extra arguments to pass to the configured package manager upon the installation
209
+ * of dependencies.
210
+ */
211
+ packageManagerArgs?: string[];
212
+
213
+ /*
214
+ * This field allows to instruct the package manager to override any dependency in the dependency graph.
215
+ * This is useful to enforce all your packages to use a single version of a dependency, backport a fix,
216
+ * or replace a dependency with a fork.
217
+ */
218
+ overrides?: Record<string, string>;
219
+
220
+ /**
221
+ * This is similar to overrides, but will only affect installation in capsules.
222
+ * In case overrides is configured and this not, the regular overrides will affect capsules as well.
223
+ * in case both configured, capsulesOverrides will be used for capsules, and overrides will affect the workspace.
224
+ */
225
+ capsulesOverrides?: Record<string, string>;
226
+
227
+ /*
228
+ * Defines what linker should be used for installing Node.js packages.
229
+ * Supported values are hoisted and isolated.
230
+ */
231
+ nodeLinker?: NodeLinker;
232
+
233
+ /*
234
+ * Controls the way packages are imported from the store.
235
+ */
236
+ packageImportMethod?: PackageImportMethod;
237
+
238
+ /*
239
+ * Use and cache the results of (pre/post)install hooks.
240
+ */
241
+ sideEffectsCache?: boolean;
242
+
243
+ /*
244
+ * The list of components that should be installed in isolation from the workspace.
245
+ * The component's package names should be used in this list, not their component IDs.
246
+ */
247
+ rootComponents?: boolean;
248
+
249
+ /*
250
+ * The node version to use when checking a package's engines setting.
251
+ */
252
+ nodeVersion?: string;
253
+
254
+ /*
255
+ * Refuse to install any package that claims to not be compatible with the current Node.js version.
256
+ */
257
+ engineStrict?: boolean;
258
+
259
+ /*
260
+ * Rules to mute specific peer dependeny warnings.
261
+ */
262
+ peerDependencyRules?: PeerDependencyRules;
263
+
264
+ /*
265
+ * This setting is "true" by default and tells bit to link core aspects to the node_modules of the workspace.
266
+ * It only makes sense to set this to "false" in a workspace in which core aspects are actually developed.
267
+ */
268
+ linkCoreAspects?: boolean;
269
+
270
+ /**
271
+ * When false, Bit will create a shared node_modules directory for all components in a capsule.
272
+ */
273
+ isolatedCapsules?: boolean;
274
+
275
+ /*
276
+ * Ignore the builds of specific dependencies. The "preinstall", "install", and "postinstall" scripts
277
+ * of the listed packages will not be executed during installation.
278
+ */
279
+ neverBuiltDependencies?: string[];
280
+
281
+ /**
282
+ * If true, staleness checks for cached data will be bypassed, but missing data will be requested from the server.
283
+ */
284
+ preferOffline?: boolean;
285
+ }
286
+
287
+ export interface DependencyResolverVariantConfig {
288
+ policy: VariantPolicyConfigObject;
289
+ }
290
+
291
+ export type RootPolicyRegistry = SlotRegistry<WorkspacePolicy>;
292
+ export type PoliciesRegistry = SlotRegistry<VariantPolicyConfigObject>;
293
+ export type PackageManagerSlot = SlotRegistry<PackageManager>;
294
+ export type DependencyFactorySlot = SlotRegistry<DependencyFactory[]>;
295
+ export type PreInstallSlot = SlotRegistry<PreInstallSubscriberList>;
296
+ export type PostInstallSlot = SlotRegistry<PostInstallSubscriberList>;
297
+
298
+ export type MergeDependenciesFunc = (configuredExtensions: ExtensionDataList) => Promise<VariantPolicyConfigObject>;
299
+
300
+ export type GetInstallerOptions = {
301
+ rootDir?: string;
302
+ packageManager?: string;
303
+ cacheRootDirectory?: string;
304
+ installingContext?: DepInstallerContext;
305
+ nodeLinker?: NodeLinker;
306
+ };
307
+
308
+ export type GetLinkerOptions = {
309
+ rootDir?: string;
310
+ linkingOptions?: LinkingOptions;
311
+ linkingContext?: DepLinkerContext;
312
+ };
313
+
314
+ export type GetDependenciesOptions = {
315
+ includeHidden?: boolean;
316
+ };
317
+
318
+ export type GetVersionResolverOptions = {
319
+ cacheRootDirectory?: string;
320
+ };
321
+
322
+ const defaultLinkingOptions: LinkingOptions = {
323
+ linkTeambitBit: true,
324
+ linkCoreAspects: true,
325
+ };
326
+
327
+ const defaultCreateFromComponentsOptions: CreateFromComponentsOptions = {
328
+ filterComponentsFromManifests: true,
329
+ createManifestForComponentsWithoutDependencies: true,
330
+ };
331
+
332
+ export class DependencyResolverMain {
333
+ /**
334
+ * cache the workspace policy to improve performance. when workspace.jsonc is changed, this gets cleared.
335
+ * @see workspace.triggerOnWorkspaceConfigChange
336
+ */
337
+ private _workspacePolicy: WorkspacePolicy | undefined;
338
+ constructor(
339
+ /**
340
+ * Dependency resolver extension configuration.
341
+ */
342
+ public config: DependencyResolverWorkspaceConfig,
343
+
344
+ /**
345
+ * Registry for changes by other extensions.
346
+ */
347
+ private rootPolicyRegistry: RootPolicyRegistry,
348
+
349
+ /**
350
+ * Registry for changes by other extensions.
351
+ */
352
+ private policiesRegistry: PoliciesRegistry,
353
+
354
+ /**
355
+ * envs extension.
356
+ */
357
+ private envs: EnvsMain,
358
+
359
+ private logger: Logger,
360
+
361
+ private configAspect: ConfigMain,
362
+
363
+ private aspectLoader: AspectLoaderMain,
364
+
365
+ private globalConfig: GlobalConfigMain,
366
+
367
+ /**
368
+ * component aspect.
369
+ */
370
+ readonly componentAspect: ComponentMain,
371
+
372
+ private packageManagerSlot: PackageManagerSlot,
373
+
374
+ private dependencyFactorySlot: DependencyFactorySlot,
375
+
376
+ private preInstallSlot: PreInstallSlot,
377
+
378
+ private postInstallSlot: PostInstallSlot
379
+ ) {}
380
+
381
+ /**
382
+ * Save list of envs that doesn't contains env.jsonc file
383
+ * this is used to show warning / instuctions to the user
384
+ */
385
+ public envsWithoutManifest = new Set<string>();
386
+
387
+ /**
388
+ * This function is a temporary workaround for installation in capsules with pnpm.
389
+ * Currently pnpm breaks the root node_modules inside the capsule because it removes deps from it.
390
+ * Install runs several times in the same capsule and pnpm removes deps from the previous runs.
391
+ *
392
+ * This workaround unfortunately also breaks pnpm on angular projects. Because dedupe doesn't work properly.
393
+ * To fix this issue we'll either have to switch to root components or try to change pnpm code.
394
+ *
395
+ * Here is the PR where initially dedupe was turned off for pnpm: https://github.com/teambit/bit/pull/5410
396
+ */
397
+ supportsDedupingOnExistingRoot(): boolean {
398
+ const packageManager = this.getPackageManager();
399
+ return packageManager?.supportsDedupingOnExistingRoot?.() === true && !this.isolatedCapsules();
400
+ }
401
+
402
+ setConfig(config: DependencyResolverWorkspaceConfig) {
403
+ this.config = config;
404
+ }
405
+
406
+ hasRootComponents(): boolean {
407
+ return Boolean(this.config.rootComponents);
408
+ }
409
+
410
+ isolatedCapsules(): boolean {
411
+ const globalConfig = this.globalConfig.getSync(CFG_ISOLATED_SCOPE_CAPSULES);
412
+ // @ts-ignore
413
+ const defaultVal = globalConfig !== undefined ? globalConfig === true || globalConfig === 'true' : true;
414
+ const res = this.config.isolatedCapsules ?? defaultVal;
415
+ return res;
416
+ }
417
+
418
+ hasHarmonyInRootPolicy(): boolean {
419
+ const rootPolicy = this.getWorkspacePolicyFromConfig();
420
+ return rootPolicy.entries.some(({ dependencyId }) => dependencyId === '@teambit/harmony');
421
+ }
422
+
423
+ nodeLinker(packageManagerName?: string): NodeLinker {
424
+ if (this.config.nodeLinker) return this.config.nodeLinker;
425
+ const pmName = packageManagerName || this.config.packageManager;
426
+ if (pmName === 'teambit.dependencies/yarn') return 'hoisted';
427
+ return 'isolated';
428
+ }
429
+
430
+ linkCoreAspects(): boolean {
431
+ return this.config.linkCoreAspects ?? DependencyResolverMain.defaultConfig.linkCoreAspects;
432
+ }
433
+
434
+ /**
435
+ * register a new package manager to the dependency resolver.
436
+ */
437
+ registerPackageManager(packageManager: PackageManager) {
438
+ this.packageManagerSlot.register(packageManager);
439
+ }
440
+
441
+ registerDependencyFactories(factories: DependencyFactory[]) {
442
+ this.dependencyFactorySlot.register(factories);
443
+ }
444
+
445
+ registerPreInstallSubscribers(subscribers: PreInstallSubscriberList) {
446
+ this.preInstallSlot.register(subscribers);
447
+ }
448
+
449
+ registerPostInstallSubscribers(subscribers: PreInstallSubscriberList) {
450
+ this.postInstallSlot.register(subscribers);
451
+ }
452
+
453
+ getSavePrefix(): string {
454
+ return this.config.savePrefix || '^';
455
+ }
456
+
457
+ getVersionWithSavePrefix({
458
+ version,
459
+ overridePrefix,
460
+ wantedRange,
461
+ }: {
462
+ version: string;
463
+ overridePrefix?: string;
464
+ wantedRange?: string;
465
+ }): string {
466
+ if (wantedRange && ['~', '^'].includes(wantedRange[0])) {
467
+ return wantedRange;
468
+ }
469
+ const prefix = overridePrefix || this.getSavePrefix();
470
+ const versionWithPrefix = `${prefix}${version}`;
471
+ if (!semver.validRange(versionWithPrefix)) {
472
+ throw new InvalidVersionWithPrefix(versionWithPrefix);
473
+ }
474
+ return versionWithPrefix;
475
+ }
476
+
477
+ async getPolicy(component: Component): Promise<VariantPolicy> {
478
+ const entry = component.state.aspects.get(DependencyResolverAspect.id);
479
+ if (!entry) {
480
+ return VariantPolicy.getEmpty();
481
+ }
482
+ const serializedPolicy: SerializedVariantPolicy = get(entry, ['data', 'policy'], []);
483
+ return VariantPolicy.parse(serializedPolicy);
484
+ }
485
+
486
+ /**
487
+ * This function called on component load in order to calculate the dependencies based on the legacy (consumer) component
488
+ * and write them to the dependencyResolver data.
489
+ * Do not use this function for other purpose.
490
+ * If you want to get the component dependencies call getDependencies (which will give you the dependencies from the data itself)
491
+ * TODO: once we switch deps resolver <> workspace relation we should make it private
492
+ * TODO: once we switch deps resolver <> workspace relation we should remove the resolveId func here
493
+ * @param component
494
+ */
495
+ async extractDepsFromLegacy(component: Component, policy?: VariantPolicy): Promise<DependencyList> {
496
+ const componentPolicy = policy || (await this.getPolicy(component));
497
+ const legacyComponent: LegacyComponent = component.state._consumer;
498
+ const listFactory = this.getDependencyListFactory();
499
+ const dependencyList = await listFactory.fromLegacyComponent(legacyComponent);
500
+ dependencyList.forEach((dep) => {
501
+ const found = componentPolicy.find(dep.id);
502
+ // if no policy found, the dependency was auto-resolved from the source code
503
+ dep.source = found?.source || 'auto';
504
+ dep.hidden = found?.hidden;
505
+ dep.optional = found?.optional;
506
+ });
507
+ return dependencyList;
508
+ }
509
+
510
+ private getDependencyListFactory(): DependencyListFactory {
511
+ const factories = this.dependencyFactorySlot.values().flat();
512
+ const factoriesMap = factories.reduce((acc, factory) => {
513
+ acc[factory.type] = factory;
514
+ return acc;
515
+ }, {});
516
+ const listFactory = new DependencyListFactory(factoriesMap);
517
+ return listFactory;
518
+ }
519
+
520
+ /**
521
+ * Main function to get the dependency list of a given component
522
+ * @param component
523
+ */
524
+ async getDependencies(
525
+ component: IComponent,
526
+ { includeHidden = false }: GetDependenciesOptions = {}
527
+ ): Promise<DependencyList> {
528
+ const entry = component.get(DependencyResolverAspect.id);
529
+ if (!entry) {
530
+ return DependencyList.fromArray([]);
531
+ }
532
+ const serializedDependencies: SerializedDependency[] = entry?.data?.dependencies || [];
533
+ const depList = await this.getDependenciesFromSerializedDependencies(serializedDependencies);
534
+ if (includeHidden) return depList;
535
+ return depList.filterHidden();
536
+ }
537
+
538
+ /**
539
+ * returns only the dependencies that are bit-components.
540
+ */
541
+ async getComponentDependencies(component: IComponent): Promise<ComponentDependency[]> {
542
+ const dependencyList = await this.getDependencies(component);
543
+ return dependencyList.getComponentDependencies();
544
+ }
545
+
546
+ private async getDependenciesFromSerializedDependencies(
547
+ dependencies: SerializedDependency[]
548
+ ): Promise<DependencyList> {
549
+ if (!dependencies.length) {
550
+ return DependencyList.fromArray([]);
551
+ }
552
+ const listFactory = this.getDependencyListFactory();
553
+ const deps = await listFactory.fromSerializedDependencies(dependencies);
554
+ return deps;
555
+ }
556
+
557
+ /**
558
+ * Getting the merged workspace policy (from dep resolver config and others like root package.json)
559
+ */
560
+ getWorkspacePolicy(): WorkspacePolicy {
561
+ if (!this._workspacePolicy) {
562
+ const policyFromConfig = this.getWorkspacePolicyFromConfig();
563
+ const externalPolicies = this.rootPolicyRegistry.toArray().map(([, policy]) => policy);
564
+ this._workspacePolicy = this.mergeWorkspacePolices([policyFromConfig, ...externalPolicies]);
565
+ }
566
+ return this._workspacePolicy;
567
+ }
568
+
569
+ getWorkspacePolicyManifest() {
570
+ const workspacePolicy = this.getWorkspacePolicy();
571
+ return workspacePolicy.toManifest();
572
+ }
573
+
574
+ clearCache() {
575
+ this._workspacePolicy = undefined;
576
+ }
577
+
578
+ /**
579
+ * Getting the workspace policy as defined in the workspace.jsonc in the dependencyResolver aspect
580
+ * This will not take into account packages that defined in the package.json of the root for example
581
+ * in most cases you should use getWorkspacePolicy
582
+ * @returns
583
+ */
584
+ getWorkspacePolicyFromConfig(): WorkspacePolicy {
585
+ const factory = new WorkspacePolicyFactory();
586
+ return factory.fromConfigObject(this.config.policy);
587
+ }
588
+
589
+ getWorkspacePolicyFromConfigObject(obj: WorkspacePolicyConfigObject): WorkspacePolicy {
590
+ const factory = new WorkspacePolicyFactory();
591
+ return factory.fromConfigObject(obj);
592
+ }
593
+
594
+ getWorkspacePolicyFromPackageJson(packageJson: Record<string, any>): WorkspacePolicy {
595
+ const factory = new WorkspacePolicyFactory();
596
+ return factory.fromPackageJson(packageJson);
597
+ }
598
+
599
+ mergeWorkspacePolices(polices: WorkspacePolicy[]): WorkspacePolicy {
600
+ return WorkspacePolicy.mergePolices(polices);
601
+ }
602
+
603
+ /**
604
+ * Create a workspace manifest
605
+ * The term workspace here is not the same as "bit workspace" but a workspace that represent a shared root
606
+ * for installation of many components (sometime it might point to the workspace path)
607
+ * in other case it can be for example the capsules root dir
608
+ *
609
+ * @param {string} [name=ROOT_NAME]
610
+ * @param {SemVer} [version=new SemVer('1.0.0')]
611
+ * @param {ManifestDependenciesObject} dependencies
612
+ * @param {string} rootDir
613
+ * @param {Component[]} components
614
+ * @param {CreateFromComponentsOptions} [options={
615
+ * filterComponentsFromManifests: true,
616
+ * createManifestForComponentsWithoutDependencies: true,
617
+ * }]
618
+ * @returns {WorkspaceManifest}
619
+ * @memberof DependencyResolverMain
620
+ */
621
+ async getWorkspaceManifest(
622
+ name: string = ROOT_NAME,
623
+ version: SemVer = new SemVer('1.0.0'),
624
+ rootPolicy: WorkspacePolicy,
625
+ rootDir: string,
626
+ components: Component[],
627
+ options: CreateFromComponentsOptions = defaultCreateFromComponentsOptions,
628
+ context: DepInstallerContext = {}
629
+ ): Promise<WorkspaceManifest> {
630
+ const statusMessage = context?.inCapsule
631
+ ? `(capsule) deduping dependencies for installation in root dir ${rootDir}`
632
+ : 'deduping dependencies for installation';
633
+ if (!context?.inCapsule) {
634
+ this.logger.setStatusLine(statusMessage);
635
+ }
636
+ const concreteOpts = {
637
+ ...defaultCreateFromComponentsOptions,
638
+ ...options,
639
+ };
640
+ const workspaceManifestFactory = new WorkspaceManifestFactory(this, this.aspectLoader);
641
+ const res = await workspaceManifestFactory.createFromComponents(
642
+ name,
643
+ version,
644
+ rootPolicy,
645
+ rootDir,
646
+ components,
647
+ concreteOpts
648
+ );
649
+ if (!context?.inCapsule) {
650
+ this.logger.consoleSuccess();
651
+ }
652
+ return res;
653
+ }
654
+
655
+ /**
656
+ * get the package name of a component.
657
+ */
658
+ getPackageName(component: Component): string {
659
+ return this.getDepResolverData(component)?.packageName ?? this.calcPackageName(component);
660
+ }
661
+
662
+ getDepResolverData(component: Component): DependencyResolverComponentData | undefined {
663
+ return component.state.aspects.get(DependencyResolverAspect.id)?.data as DependencyResolverComponentData;
664
+ }
665
+
666
+ calcPackageName(component: Component): string {
667
+ return componentIdToPackageName(component.state._consumer);
668
+ }
669
+
670
+ /*
671
+ * Returns the location where the component is installed with its peer dependencies
672
+ * This is used in cases you want to actually run the components and make sure all the dependencies (especially peers) are resolved correctly
673
+ */
674
+ getRuntimeModulePath(component: Component, isInWorkspace = false) {
675
+ if (!this.hasRootComponents()) {
676
+ const modulePath = this.getModulePath(component);
677
+ return modulePath;
678
+ }
679
+ const pkgName = this.getPackageName(component);
680
+ const selfRootDir = getRelativeRootComponentDir(
681
+ !isInWorkspace ? component.id.toString() : component.id.toStringWithoutVersion()
682
+ );
683
+ // In case the component is it's own root we want to load it from it's own root folder
684
+ if (fs.pathExistsSync(selfRootDir)) {
685
+ const innerDir = join(selfRootDir, 'node_modules', pkgName);
686
+ if (fs.pathExistsSync(innerDir)) return innerDir;
687
+ // sometime for the env itself we don't have the env package in the env root dir, because it was hoisted
688
+ // in that case we return the dir from the root node_modules
689
+ return this.getModulePath(component);
690
+ }
691
+ const envId = this.envs.getEnvId(component);
692
+ const dirInEnvRoot = join(getRelativeRootComponentDir(envId), 'node_modules', pkgName);
693
+ if (fs.pathExistsSync(dirInEnvRoot)) return dirInEnvRoot;
694
+ return this.getModulePath(component);
695
+ }
696
+
697
+ /**
698
+ * returns the package path in the /node_modules/ folder
699
+ * In case you call this in order to run the code from the path, please refer to the `getRuntimeModulePath` API
700
+ */
701
+ getModulePath(component: Component) {
702
+ const pkgName = this.getPackageName(component);
703
+ const relativePath = join('node_modules', pkgName);
704
+ return relativePath;
705
+ }
706
+
707
+ /**
708
+ * get a component dependency installer.
709
+ */
710
+ getInstaller(options: GetInstallerOptions = {}) {
711
+ const packageManagerName = options.packageManager || this.config.packageManager;
712
+ const packageManager = this.packageManagerSlot.get(packageManagerName);
713
+ const cacheRootDir = options.cacheRootDirectory || this.globalConfig.getSync(CFG_PACKAGE_MANAGER_CACHE);
714
+
715
+ if (!packageManager) {
716
+ throw new PackageManagerNotFound(this.config.packageManager);
717
+ }
718
+
719
+ if (cacheRootDir && !fs.pathExistsSync(cacheRootDir)) {
720
+ this.logger.debug(`creating package manager cache dir at ${cacheRootDir}`);
721
+ fs.ensureDirSync(cacheRootDir);
722
+ }
723
+ const preInstallSubscribers = this.getPreInstallSubscribers();
724
+ const postInstallSubscribers = this.getPostInstallSubscribers();
725
+ // TODO: we should somehow pass the cache root dir to the package manager constructor
726
+ return new DependencyInstaller(
727
+ packageManager,
728
+ this.aspectLoader,
729
+ this.logger,
730
+ this,
731
+ options.rootDir,
732
+ cacheRootDir,
733
+ preInstallSubscribers,
734
+ postInstallSubscribers,
735
+ options.nodeLinker || this.nodeLinker(packageManagerName),
736
+ this.config.packageImportMethod,
737
+ this.config.sideEffectsCache,
738
+ this.config.nodeVersion,
739
+ this.config.engineStrict,
740
+ this.config.peerDependencyRules,
741
+ this.config.neverBuiltDependencies,
742
+ this.config.preferOffline,
743
+ options.installingContext
744
+ );
745
+ }
746
+
747
+ private getPreInstallSubscribers(): PreInstallSubscriberList {
748
+ return this.preInstallSlot.values().flat();
749
+ }
750
+
751
+ private getPostInstallSubscribers(): PostInstallSubscriberList {
752
+ return this.postInstallSlot.values().flat();
753
+ }
754
+
755
+ /**
756
+ * get a component dependency linker.
757
+ */
758
+ getLinker(options: GetLinkerOptions = {}) {
759
+ const linkingOptions = Object.assign({}, defaultLinkingOptions, options?.linkingOptions || {});
760
+ // TODO: we should somehow pass the cache root dir to the package manager constructor
761
+ return new DependencyLinker(
762
+ this,
763
+ this.aspectLoader,
764
+ this.componentAspect,
765
+ this.envs,
766
+ this.logger,
767
+ options.rootDir,
768
+ linkingOptions,
769
+ options.linkingContext
770
+ );
771
+ }
772
+
773
+ /**
774
+ * This function returns the package manager if it exists, otherwise it returns undefined.
775
+ * @returns The `getPackageManager()` function returns a `PackageManager` object or `undefined`.
776
+ */
777
+ getPackageManager(): PackageManager | undefined {
778
+ const packageManager = this.packageManagerSlot.get(this.config.packageManager);
779
+ return packageManager;
780
+ }
781
+
782
+ getPackageManagerName() {
783
+ return this.config.packageManager;
784
+ }
785
+
786
+ async getVersionResolver(options: GetVersionResolverOptions = {}) {
787
+ const packageManager = this.getPackageManager();
788
+ const cacheRootDir = options.cacheRootDirectory || this.globalConfig.getSync(CFG_PACKAGE_MANAGER_CACHE);
789
+
790
+ if (!packageManager) {
791
+ throw new PackageManagerNotFound(this.config.packageManager);
792
+ }
793
+
794
+ if (cacheRootDir && !fs.pathExistsSync(cacheRootDir)) {
795
+ this.logger.debug(`creating package manager cache dir at ${cacheRootDir}`);
796
+ fs.ensureDirSync(cacheRootDir);
797
+ }
798
+ const { networkConcurrency } = await this.getNetworkConfig();
799
+ // TODO: we should somehow pass the cache root dir to the package manager constructor
800
+ return new DependencyVersionResolver(packageManager, cacheRootDir, networkConcurrency);
801
+ }
802
+
803
+ /**
804
+ * these ids should not be in the dependencyResolver policy normally.
805
+ * one exception is bit itself, which needs teambit.harmony/harmony in the dependencies.
806
+ *
807
+ * returns component-ids string without a version.
808
+ */
809
+ getCompIdsThatShouldNotBeInPolicy(): string[] {
810
+ return [...getAllCoreAspectsIds(), 'teambit.harmony/harmony'];
811
+ }
812
+
813
+ /**
814
+ * return the system configured package manager. by default pnpm.
815
+ */
816
+ getSystemPackageManager(): PackageManager {
817
+ const defaultPm = 'teambit.dependencies/pnpm';
818
+ const packageManager = this.packageManagerSlot.get(defaultPm);
819
+ if (!packageManager) throw new Error(`default package manager: ${defaultPm} was not found`);
820
+ return packageManager;
821
+ }
822
+
823
+ async getProxyConfig(): Promise<ProxyConfig> {
824
+ const proxyConfigFromDepResolverConfig = this.getProxyConfigFromDepResolverConfig();
825
+ let httpProxy = proxyConfigFromDepResolverConfig.httpProxy;
826
+ let httpsProxy = proxyConfigFromDepResolverConfig.httpsProxy;
827
+
828
+ // Take config from the aspect config if defined
829
+ if (httpProxy || httpsProxy) {
830
+ this.logger.debug(
831
+ `proxy config taken from the dep resolver config. proxy: ${httpProxy} httpsProxy: ${httpsProxy}`
832
+ );
833
+ return proxyConfigFromDepResolverConfig;
834
+ }
835
+
836
+ // Take config from the package manager (npmrc) config if defined
837
+ const proxyConfigFromPackageManager = await this.getProxyConfigFromPackageManager();
838
+ if (proxyConfigFromPackageManager?.httpProxy || proxyConfigFromPackageManager?.httpsProxy) {
839
+ this.logger.debug(
840
+ `proxy config taken from the package manager config (npmrc). proxy: ${proxyConfigFromPackageManager.httpProxy} httpsProxy: ${proxyConfigFromPackageManager.httpsProxy}`
841
+ );
842
+ return proxyConfigFromPackageManager;
843
+ }
844
+
845
+ // Take config from global bit config
846
+ const proxyConfigFromGlobalConfig = await this.getProxyConfigFromGlobalConfig();
847
+ httpProxy = proxyConfigFromGlobalConfig.httpProxy;
848
+ httpsProxy = proxyConfigFromGlobalConfig.httpsProxy;
849
+ if (httpProxy || httpsProxy) {
850
+ this.logger.debug(`proxy config taken from the global bit config. proxy: ${httpProxy} httpsProxy: ${httpsProxy}`);
851
+ return proxyConfigFromGlobalConfig;
852
+ }
853
+ return {};
854
+ }
855
+
856
+ private getProxyConfigFromDepResolverConfig(): ProxyConfig {
857
+ return {
858
+ httpProxy: this.config.proxy,
859
+ httpsProxy: this.config.httpsProxy || this.config.proxy,
860
+ noProxy: this.config.noProxy,
861
+ };
862
+ }
863
+
864
+ async getNetworkConfig(): Promise<NetworkConfig> {
865
+ const networkConfig = {
866
+ ...(await this.getNetworkConfigFromGlobalConfig()),
867
+ ...(await this.getNetworkConfigFromPackageManager()),
868
+ ...this.getNetworkConfigFromDepResolverConfig(),
869
+ };
870
+ this.logger.debug(
871
+ `the next network configuration is used in dependency-resolver: ${JSON.stringify(
872
+ {
873
+ ...networkConfig,
874
+ key: networkConfig.key ? 'set' : 'not set', // this is sensitive information, we should not log it
875
+ },
876
+ null,
877
+ 2
878
+ )}`
879
+ );
880
+ return networkConfig;
881
+ }
882
+
883
+ private async getNetworkConfigFromGlobalConfig(): Promise<NetworkConfig> {
884
+ const globalNetworkConfig = await Http.getNetworkConfig();
885
+ if (!globalNetworkConfig.ca && globalNetworkConfig.cafile) {
886
+ globalNetworkConfig.ca = readCAFileSync(globalNetworkConfig.cafile);
887
+ }
888
+ return globalNetworkConfig;
889
+ }
890
+
891
+ private getNetworkConfigFromDepResolverConfig(): NetworkConfig {
892
+ const config: NetworkConfig = pick(this.config, [
893
+ 'fetchTimeout',
894
+ 'fetchRetries',
895
+ 'fetchRetryFactor',
896
+ 'fetchRetryMintimeout',
897
+ 'fetchRetryMaxtimeout',
898
+ 'maxSockets',
899
+ 'networkConcurrency',
900
+ 'key',
901
+ 'cert',
902
+ 'ca',
903
+ 'cafile',
904
+ ]);
905
+ if (this.config.strictSsl != null) {
906
+ config.strictSSL =
907
+ typeof this.config.strictSsl === 'string'
908
+ ? this.config.strictSsl.toLowerCase() === 'true'
909
+ : this.config.strictSsl;
910
+ }
911
+ return config;
912
+ }
913
+
914
+ private async getNetworkConfigFromPackageManager(): Promise<NetworkConfig> {
915
+ const packageManager = this.getPackageManager();
916
+ let networkConfigFromPackageManager: NetworkConfig = {};
917
+ if (typeof packageManager?.getNetworkConfig === 'function') {
918
+ networkConfigFromPackageManager = await packageManager?.getNetworkConfig();
919
+ } else {
920
+ const systemPm = this.getSystemPackageManager();
921
+ if (!systemPm.getNetworkConfig) throw new Error('system package manager must implement `getNetworkConfig()`');
922
+ networkConfigFromPackageManager = await systemPm.getNetworkConfig();
923
+ }
924
+ return networkConfigFromPackageManager;
925
+ }
926
+
927
+ private async getProxyConfigFromPackageManager(): Promise<ProxyConfig> {
928
+ const packageManager = this.getPackageManager();
929
+ let proxyConfigFromPackageManager: ProxyConfig = {};
930
+ if (packageManager?.getProxyConfig && typeof packageManager?.getProxyConfig === 'function') {
931
+ proxyConfigFromPackageManager = await packageManager?.getProxyConfig();
932
+ } else {
933
+ const systemPm = this.getSystemPackageManager();
934
+ if (!systemPm.getProxyConfig) throw new Error('system package manager must implement `getProxyConfig()`');
935
+ proxyConfigFromPackageManager = await systemPm.getProxyConfig();
936
+ }
937
+ return proxyConfigFromPackageManager;
938
+ }
939
+
940
+ private getProxyConfigFromGlobalConfig(): Promise<ProxyConfig> {
941
+ return Http.getProxyConfig();
942
+ }
943
+
944
+ /**
945
+ * Return the peer dependencies and their ranges that may be installed
946
+ * without causing unmet peer dependency issues in some of the dependencies.
947
+ */
948
+ async getMissingPeerDependencies(
949
+ rootDir: string,
950
+ rootPolicy: WorkspacePolicy,
951
+ componentDirectoryMap: ComponentMap<string>,
952
+ options: PackageManagerGetPeerDependencyIssuesOptions
953
+ ): Promise<Record<string, string>> {
954
+ this.logger.setStatusLine('finding missing peer dependencies');
955
+ const packageManager = this.getPackageManager();
956
+ let peerDependencyIssues!: PeerDependencyIssuesByProjects;
957
+ const installer = this.getInstaller();
958
+ const manifests = await installer.getComponentManifests({
959
+ ...options,
960
+ componentDirectoryMap,
961
+ rootPolicy,
962
+ rootDir,
963
+ });
964
+ if (packageManager?.getPeerDependencyIssues && typeof packageManager?.getPeerDependencyIssues === 'function') {
965
+ peerDependencyIssues = await packageManager?.getPeerDependencyIssues(rootDir, manifests, options);
966
+ } else {
967
+ const systemPm = this.getSystemPackageManager();
968
+ if (!systemPm.getPeerDependencyIssues)
969
+ throw new Error('system package manager must implement `getPeerDependencyIssues()`');
970
+ peerDependencyIssues = await systemPm?.getPeerDependencyIssues(rootDir, manifests, options);
971
+ }
972
+ this.logger.consoleSuccess();
973
+ return peerDependencyIssues['.']?.intersections;
974
+ }
975
+
976
+ async getRegistries(): Promise<Registries> {
977
+ const packageManager = this.getPackageManager();
978
+ let registries;
979
+ if (packageManager?.getRegistries && typeof packageManager?.getRegistries === 'function') {
980
+ registries = await packageManager?.getRegistries();
981
+ } else {
982
+ const systemPm = this.getSystemPackageManager();
983
+ if (!systemPm.getRegistries) throw new Error('system package manager must implement `getRegistries()`');
984
+ registries = await systemPm.getRegistries();
985
+ }
986
+
987
+ const getDefaultBitRegistry = (): Registry => {
988
+ const bitGlobalConfigRegistry = this.globalConfig.getSync(CFG_REGISTRY_URL_KEY);
989
+ const bitRegistry = bitGlobalConfigRegistry || BIT_CLOUD_REGISTRY;
990
+
991
+ const { bitOriginalAuthType, bitAuthHeaderValue, bitOriginalAuthValue } = this.getBitAuthConfig();
992
+
993
+ const alwaysAuth = !!bitAuthHeaderValue;
994
+ const bitDefaultRegistry = new Registry(
995
+ bitRegistry,
996
+ alwaysAuth,
997
+ bitAuthHeaderValue,
998
+ bitOriginalAuthType,
999
+ bitOriginalAuthValue
1000
+ );
1001
+ return bitDefaultRegistry;
1002
+ };
1003
+
1004
+ const bitDefaultRegistry = getDefaultBitRegistry();
1005
+
1006
+ const installFromBitDevRegistry = this.config.installFromBitDevRegistry ?? true;
1007
+
1008
+ // Override default registry to use bit registry in case npmjs is the default - bit registry will proxy it
1009
+ // We check also NPM_REGISTRY.startsWith because the uri might not have the trailing / we have in NPM_REGISTRY
1010
+ if (
1011
+ installFromBitDevRegistry &&
1012
+ (!registries.defaultRegistry.uri ||
1013
+ registries.defaultRegistry.uri === NPM_REGISTRY ||
1014
+ NPM_REGISTRY.startsWith(registries.defaultRegistry.uri))
1015
+ ) {
1016
+ // TODO: this will not handle cases where you have token for private npm registries stored on npmjs
1017
+ // it should be handled by somehow in such case (default is npmjs and there is token for default) by sending the token of npmjs to the registry
1018
+ // (for example by setting some special header in the request)
1019
+ // then in the registry server it should be use it when proxies
1020
+ registries = registries.setDefaultRegistry(bitDefaultRegistry);
1021
+ }
1022
+
1023
+ registries = this.addAuthToScopedBitRegistries(registries);
1024
+ return registries;
1025
+ }
1026
+
1027
+ /**
1028
+ * This will mutate any registry which point to BIT_DEV_REGISTRY to have the auth config from the @bit scoped registry or from the user.token in bit's config
1029
+ */
1030
+ private addAuthToScopedBitRegistries(registries: Registries): Registries {
1031
+ const { bitOriginalAuthType, bitAuthHeaderValue, bitOriginalAuthValue } = this.getBitAuthConfig();
1032
+ const alwaysAuth = bitAuthHeaderValue !== undefined;
1033
+ let updatedRegistries = registries;
1034
+ Object.entries(registries.scopes).map(([name, registry]) => {
1035
+ if (!registry.authHeaderValue && BIT_CLOUD_REGISTRY.includes(registry.uri)) {
1036
+ const registryWithAuth = new Registry(
1037
+ registry.uri,
1038
+ alwaysAuth,
1039
+ bitAuthHeaderValue,
1040
+ bitOriginalAuthType,
1041
+ bitOriginalAuthValue
1042
+ );
1043
+ updatedRegistries = updatedRegistries.updateScopedRegistry(name, registryWithAuth);
1044
+ }
1045
+ return updatedRegistries;
1046
+ });
1047
+ return updatedRegistries;
1048
+ }
1049
+
1050
+ private getBitAuthConfig(): Partial<{
1051
+ bitOriginalAuthType: string;
1052
+ bitAuthHeaderValue: string;
1053
+ bitOriginalAuthValue: string;
1054
+ }> {
1055
+ const bitGlobalConfigToken = this.globalConfig.getSync(CFG_USER_TOKEN_KEY);
1056
+ const res = {
1057
+ bitOriginalAuthType: '',
1058
+ bitAuthHeaderValue: '',
1059
+ bitOriginalAuthValue: '',
1060
+ };
1061
+
1062
+ // In case there is no auth configuration in the npmrc, but there is token in bit config, take it from the config
1063
+ if (bitGlobalConfigToken) {
1064
+ res.bitOriginalAuthType = 'authToken';
1065
+ res.bitAuthHeaderValue = `Bearer ${bitGlobalConfigToken}`;
1066
+ res.bitOriginalAuthValue = bitGlobalConfigToken;
1067
+ }
1068
+
1069
+ return res;
1070
+ }
1071
+
1072
+ get packageManagerName(): string {
1073
+ return this.config.packageManager;
1074
+ }
1075
+
1076
+ addToRootPolicy(entries: WorkspacePolicyEntry[], options?: WorkspacePolicyAddEntryOptions): WorkspacePolicy {
1077
+ const workspacePolicy = this.getWorkspacePolicyFromConfig();
1078
+ entries.forEach((entry) => workspacePolicy.add(entry, options));
1079
+ this.updateConfigPolicy(workspacePolicy);
1080
+ return workspacePolicy;
1081
+ }
1082
+
1083
+ removeFromRootPolicy(dependencyIds: string[]) {
1084
+ const workspacePolicy = this.getWorkspacePolicyFromConfig();
1085
+ const workspacePolicyUpdated = workspacePolicy.remove(dependencyIds);
1086
+ this.updateConfigPolicy(workspacePolicyUpdated);
1087
+ }
1088
+
1089
+ private updateConfigPolicy(workspacePolicy: WorkspacePolicy) {
1090
+ const workspacePolicyObject = workspacePolicy.toConfigObject();
1091
+ this.config.policy = workspacePolicyObject;
1092
+ this.configAspect.setExtension(DependencyResolverAspect.id, this.config, {
1093
+ overrideExisting: true,
1094
+ ignoreVersion: true,
1095
+ });
1096
+ }
1097
+
1098
+ async persistConfig(workspaceDir?: string) {
1099
+ await this.configAspect.workspaceConfig?.write({ dir: workspaceDir });
1100
+ this.clearCache();
1101
+ }
1102
+
1103
+ /**
1104
+ * register new dependencies policies
1105
+ */
1106
+ registerDependenciesPolicies(policy: VariantPolicyConfigObject): void {
1107
+ return this.policiesRegistry.register(policy);
1108
+ }
1109
+
1110
+ /**
1111
+ * register new dependencies policies
1112
+ */
1113
+ registerRootPolicy(policy: WorkspacePolicy): void {
1114
+ return this.rootPolicyRegistry.register(policy);
1115
+ }
1116
+
1117
+ // async getComponentEnvPolicyFromExtension(configuredExtensions: ExtensionDataList): Promise<EnvPolicy> {
1118
+ // const env = this.envs.calculateEnvFromExtensions(configuredExtensions);
1119
+ // const fromFile = await this.getEnvPolicyFromFile(env.id);
1120
+ // if (fromFile) return fromFile;
1121
+ // return this.getComponentEnvPolicyFromEnv(env.env);
1122
+ // }
1123
+
1124
+ async getComponentEnvPolicyFromExtension(configuredExtensions: ExtensionDataList): Promise<EnvPolicy> {
1125
+ const envId = await this.envs.calculateEnvIdFromExtensions(configuredExtensions);
1126
+ if (this.envs.isCoreEnv(envId)) {
1127
+ const env = await this.envs.calculateEnvFromExtensions(configuredExtensions);
1128
+ return this.getComponentEnvPolicyFromEnv(env.env, { envId });
1129
+ }
1130
+
1131
+ const fromFile = await this.getEnvPolicyFromFile(envId);
1132
+ if (fromFile) return fromFile;
1133
+ const env = await this.envs.calculateEnvFromExtensions(configuredExtensions);
1134
+ return this.getComponentEnvPolicyFromEnv(env.env, { envId });
1135
+ }
1136
+
1137
+ async getEnvPolicyFromEnvId(id: ComponentID, legacyFiles?: SourceFile[]): Promise<EnvPolicy | undefined> {
1138
+ return this.getEnvPolicyFromEnvLegacyId(id, legacyFiles);
1139
+ }
1140
+
1141
+ async getEnvPolicyFromEnvLegacyId(id: ComponentID, legacyFiles?: SourceFile[]): Promise<EnvPolicy | undefined> {
1142
+ const fromFile = await this.getEnvPolicyFromFile(id.toString(), legacyFiles);
1143
+ if (fromFile) return fromFile;
1144
+ const envDef = await this.envs.getEnvDefinitionByLegacyId(id);
1145
+ if (!envDef) return undefined;
1146
+ const env = envDef.env;
1147
+ return this.getComponentEnvPolicyFromEnv(env, {
1148
+ envId: id.toStringWithoutVersion(),
1149
+ });
1150
+ }
1151
+
1152
+ async getComponentEnvPolicy(component: Component): Promise<EnvPolicy> {
1153
+ // const envComponent = await this.envs.getEnvComponent(component);
1154
+ const envId = await this.envs.calculateEnvId(component);
1155
+ const envIdWithoutVersion = envId.toStringWithoutVersion();
1156
+ if (this.envs.isCoreEnv(envIdWithoutVersion)) {
1157
+ const env = this.envs.getEnv(component).env;
1158
+ return this.getComponentEnvPolicyFromEnv(env, { envId: envIdWithoutVersion });
1159
+ }
1160
+ const fromFile = await this.getEnvPolicyFromFile(envId.toString());
1161
+ if (fromFile) return fromFile;
1162
+ this.envsWithoutManifest.add(envId.toString());
1163
+ const env = this.envs.getEnv(component).env;
1164
+ return this.getComponentEnvPolicyFromEnv(env, { envId: envIdWithoutVersion });
1165
+ }
1166
+
1167
+ getEnvManifest(envComponent?: Component, legacyFiles?: SourceFile[]): EnvPolicy | undefined {
1168
+ const object = this.envs.getEnvManifest(envComponent, legacyFiles) as any;
1169
+ const policy = object?.policy;
1170
+ if (!policy) return undefined;
1171
+ const allPoliciesFromEnv = EnvPolicy.fromConfigObject(policy, {
1172
+ includeLegacyPeersInSelfPolicy: envComponent && this.envs.isCoreEnv(envComponent.id.toStringWithoutVersion()),
1173
+ });
1174
+ return allPoliciesFromEnv;
1175
+ }
1176
+
1177
+ private async getEnvPolicyFromFile(envId: string, legacyFiles?: SourceFile[]): Promise<EnvPolicy | undefined> {
1178
+ const isCoreEnv = this.envs.isCoreEnv(envId);
1179
+ if (isCoreEnv) return undefined;
1180
+ if (legacyFiles) {
1181
+ return this.getEnvManifest(undefined, legacyFiles);
1182
+ }
1183
+ const envComponent = await this.envs.getEnvComponentByEnvId(envId, envId);
1184
+ return this.getEnvManifest(envComponent);
1185
+ }
1186
+
1187
+ async getComponentEnvPolicyFromEnv(env: DependenciesEnv, options: { envId: string }): Promise<EnvPolicy> {
1188
+ if (env.getDependencies && typeof env.getDependencies === 'function') {
1189
+ const policiesFromEnvConfig = await env.getDependencies();
1190
+ if (policiesFromEnvConfig) {
1191
+ const idWithoutVersion = options.envId.split('@')[0];
1192
+ const allPoliciesFromEnv = EnvPolicy.fromConfigObject(policiesFromEnvConfig, {
1193
+ includeLegacyPeersInSelfPolicy: this.envs.isCoreEnv(idWithoutVersion),
1194
+ });
1195
+ return allPoliciesFromEnv;
1196
+ }
1197
+ }
1198
+ return EnvPolicy.getEmpty();
1199
+ }
1200
+
1201
+ async getComponentEnvPolicyFromEnvDefinition(envDef: EnvDefinition): Promise<EnvPolicy> {
1202
+ const fromFile = await this.getEnvPolicyFromFile(envDef.id);
1203
+ if (fromFile) return fromFile;
1204
+ return this.getComponentEnvPolicyFromEnv(envDef.env, { envId: envDef.id });
1205
+ }
1206
+
1207
+ /**
1208
+ *
1209
+ * dependencies that will bundled as part of the env template and will configured as externals for the component bundle
1210
+ * these dependencies will be available in the preview on the window.
1211
+ * these dependencies will have only one instance on the page.
1212
+ * for dev server these dependencies will be aliased.
1213
+ * TODO: this should probably moved to the preview aspect. the main issue is that is used for dev server which can't bring the preview aspect.
1214
+ * @param env
1215
+ */
1216
+ async getPreviewHostDependenciesFromEnv(env: DependenciesEnv): Promise<string[]> {
1217
+ let hostDeps: string[] = [];
1218
+ if (env.getAdditionalHostDependencies && typeof env.getAdditionalHostDependencies === 'function') {
1219
+ hostDeps = await env.getAdditionalHostDependencies();
1220
+ }
1221
+ return uniq(hostDeps);
1222
+ }
1223
+
1224
+ /**
1225
+ * Merge the dependencies provided by:
1226
+ * 1. envs configured in the component - via dependencies method
1227
+ * 2. extensions that registered to the registerDependencyPolicy slot (and configured for the component)
1228
+ * 3. props defined by the user (they are the strongest one)
1229
+ * @param configuredExtensions
1230
+ */
1231
+ async mergeVariantPolicies(
1232
+ configuredExtensions: ExtensionDataList,
1233
+ id: ComponentID,
1234
+ legacyFiles?: SourceFile[]
1235
+ ): Promise<VariantPolicy> {
1236
+ let policiesFromSlots: VariantPolicy = VariantPolicy.getEmpty();
1237
+ let policiesFromConfig: VariantPolicy = VariantPolicy.getEmpty();
1238
+ const policiesFromEnv: VariantPolicy = await this.getComponentEnvPolicyFromExtension(configuredExtensions);
1239
+ const configuredIds = configuredExtensions.ids;
1240
+ const policiesTuples = this.policiesRegistry.toArray();
1241
+ configuredIds.forEach((extId) => {
1242
+ // TODO: change this way of search, once we have workspace as dep-resolver dependency
1243
+ // we can use something like:
1244
+ // const resolvedId = this.workspace.resolveComponentId(extId)
1245
+ // const currentPolicy = this.policiesRegistry.get(resolvedId.toString());
1246
+ // Only get props from configured extensions on this specific component
1247
+ const policyTupleToApply = policiesTuples.find(([policyRegistrar]) => {
1248
+ return policyRegistrar === extId || policyRegistrar.includes(extId);
1249
+ });
1250
+
1251
+ if (policyTupleToApply && policyTupleToApply[1]) {
1252
+ const currentPolicy = VariantPolicy.fromConfigObject(policyTupleToApply[1], { source: 'slots' });
1253
+ policiesFromSlots = VariantPolicy.mergePolices([policiesFromSlots, currentPolicy]);
1254
+ }
1255
+ });
1256
+ const currentExtension = configuredExtensions.findExtension(DependencyResolverAspect.id);
1257
+ const currentConfig = currentExtension?.config as unknown as DependencyResolverVariantConfig;
1258
+ if (currentConfig && currentConfig.policy) {
1259
+ policiesFromConfig = VariantPolicy.fromConfigObject(currentConfig.policy, { source: 'config' });
1260
+ }
1261
+ const policiesFromEnvForItself =
1262
+ (await this.getPoliciesFromEnvForItself(id, legacyFiles)) ?? VariantPolicy.getEmpty();
1263
+
1264
+ const result = VariantPolicy.mergePolices([
1265
+ policiesFromEnv,
1266
+ policiesFromEnvForItself,
1267
+ policiesFromSlots,
1268
+ policiesFromConfig,
1269
+ ]);
1270
+ return result;
1271
+ }
1272
+
1273
+ /**
1274
+ * These are the policies that the env itself defines for itself.
1275
+ * So policies installed only locally for the env, not to any components that use the env.
1276
+ */
1277
+ async getPoliciesFromEnvForItself(id: ComponentID, legacyFiles?: SourceFile[]): Promise<VariantPolicy | undefined> {
1278
+ const envPolicy = await this.getEnvPolicyFromEnvLegacyId(id, legacyFiles);
1279
+ return envPolicy?.selfPolicy;
1280
+ }
1281
+
1282
+ updateDepsOnLegacyTag(component: LegacyComponent, idTransformer: onTagIdTransformer): LegacyComponent {
1283
+ const entry = component.extensions.findCoreExtension(DependencyResolverAspect.id);
1284
+ if (!entry) {
1285
+ return component;
1286
+ }
1287
+ const dependencies = get(entry, ['data', 'dependencies'], []);
1288
+ dependencies.forEach((dep) => {
1289
+ if (dep.__type === COMPONENT_DEP_TYPE) {
1290
+ // @todo: it's unclear why "dep.componentId" randomly becomes a ComponentID instance.
1291
+ // this check is added because on Ripple in some scenarios it was throwing:
1292
+ // "ComponentID.fromObject expect to get an object, got an instance of ComponentID" (locally it didn't happen)
1293
+ const depId =
1294
+ dep.componentId instanceof ComponentID ? dep.componentId : ComponentID.fromObject(dep.componentId);
1295
+ const newDepId = idTransformer(depId);
1296
+ dep.componentId = (newDepId || depId).serialize();
1297
+ dep.id = (newDepId || depId).toString();
1298
+ dep.version = (newDepId || depId).version;
1299
+ }
1300
+ });
1301
+ return component;
1302
+ }
1303
+
1304
+ /**
1305
+ * Register a new dependency detector. Detectors allow to extend Bit's dependency detection
1306
+ * mechanism to support new file extensions and types.
1307
+ */
1308
+ registerDetector(detector: DependencyDetector) {
1309
+ DetectorHook.hooks.push(detector);
1310
+ return this;
1311
+ }
1312
+
1313
+ /**
1314
+ * This function called on component load in order to calculate the custom
1315
+ * dependency detectors from an env, which is got by extension data list.
1316
+ * Do not use this function for other purposes.
1317
+ */
1318
+ async calcComponentEnvDepDetectors(extensions: ExtensionDataList) {
1319
+ const envDef = await this.envs.calculateEnvFromExtensions(extensions);
1320
+ const depEnv = envDef.env as DependenciesEnv;
1321
+ if (typeof depEnv?.getDepDetectors === 'function') {
1322
+ return depEnv.getDepDetectors();
1323
+ }
1324
+ return null;
1325
+ }
1326
+
1327
+ /**
1328
+ * This function registered to the onLoadRequireableExtensionSlot of the aspect-loader
1329
+ * Update the aspect / manifest deps versions in the runtimes (recursively)
1330
+ * This function mutate the manifest directly as otherwise it becomes very complicated
1331
+ * TODO: think if this funciton should be here as it about dependencies, or on the aspect loader
1332
+ * (as it's aware of the internal structure of aspects)
1333
+ * Maybe only register the dep resolution part to the aspect loader
1334
+ * at the moment it here for simplify the process
1335
+ * @param requireableExtension
1336
+ * @param manifest
1337
+ * @returns
1338
+ */
1339
+ async onLoadRequireableExtensionSubscriber(
1340
+ requireableExtension: RequireableComponent,
1341
+ manifest: ExtensionManifest | Aspect
1342
+ ): Promise<ExtensionManifest | Aspect> {
1343
+ const parentComponent = requireableExtension.component;
1344
+ return this.resolveRequireableExtensionManifestDepsVersionsRecursively(parentComponent, manifest);
1345
+ }
1346
+
1347
+ /**
1348
+ * Update the aspect / manifest deps versions in the runtimes (recursively)
1349
+ * @param parentComponent
1350
+ * @param manifest
1351
+ */
1352
+ private async resolveRequireableExtensionManifestDepsVersionsRecursively(
1353
+ // Allow getting here string for lazy load the component
1354
+ // we only want to load the component in case there are deps to resolve
1355
+ parentComponent: Component | string,
1356
+ manifest: ExtensionManifest | Aspect
1357
+ // TODO: add visited = new Map() for performence improve
1358
+ ): Promise<ExtensionManifest | Aspect> {
1359
+ // Not resolve it immediately for performance sake
1360
+ let resolvedParentComponent: Component | undefined;
1361
+ let resolvedParentDeps: DependencyList;
1362
+ const updateDirectDepsVersions = (deps: Array<ExtensionManifest | Aspect>): Promise<void[]> => {
1363
+ return mapSeries(deps, async (dep) => {
1364
+ // Nothing to update (this shouldn't happen ever)
1365
+ if (!dep.id) return;
1366
+ // In case of core aspect, do not update the version, as it's loaded to harmony without version
1367
+ if (this.aspectLoader.isCoreAspect(dep.id)) return;
1368
+ // Lazily get the parent component
1369
+ if (typeof parentComponent === 'string') {
1370
+ const parentComponentId = await this.componentAspect.getHost().resolveComponentId(parentComponent);
1371
+ resolvedParentComponent = await this.componentAspect.getHost().get(parentComponentId);
1372
+ } else {
1373
+ // it's of type component;
1374
+ resolvedParentComponent = parentComponent;
1375
+ }
1376
+ if (!resolvedParentComponent) {
1377
+ this.logger.error(
1378
+ `could not resolve the component ${parentComponent} during manifest deps resolution. it shouldn't happen`
1379
+ );
1380
+ return;
1381
+ }
1382
+ // Lazily get the dependencies
1383
+ resolvedParentDeps = resolvedParentDeps || (await this.getDependencies(resolvedParentComponent));
1384
+ const resolvedDep = resolvedParentDeps.findDependency(dep.id, { ignoreVersion: true });
1385
+ dep.id = resolvedDep?.id ?? dep.id;
1386
+ await this.resolveRequireableExtensionManifestDepsVersionsRecursively(dep.id, dep);
1387
+ });
1388
+ };
1389
+ if (manifest.dependencies) {
1390
+ manifest.dependencies = manifest.dependencies.map((dep) => this.aspectLoader.cloneManifest(dep));
1391
+ await updateDirectDepsVersions(manifest.dependencies);
1392
+ }
1393
+ // @ts-ignore
1394
+ if (manifest?._runtimes) {
1395
+ // @ts-ignore
1396
+ await mapSeries(manifest?._runtimes, async (runtime: RuntimeManifest) => {
1397
+ if (runtime.dependencies) {
1398
+ runtime.dependencies = runtime.dependencies.map((dep) => this.aspectLoader.cloneManifest(dep));
1399
+ await updateDirectDepsVersions(runtime.dependencies);
1400
+ }
1401
+ });
1402
+ }
1403
+
1404
+ return manifest;
1405
+ }
1406
+
1407
+ /**
1408
+ * Return a list of outdated policy dependencies.
1409
+ */
1410
+ async getOutdatedPkgsFromPolicies({
1411
+ rootDir,
1412
+ variantPoliciesByPatterns,
1413
+ componentPolicies,
1414
+ components,
1415
+ patterns,
1416
+ forceVersionBump,
1417
+ }: {
1418
+ rootDir: string;
1419
+ variantPoliciesByPatterns: Record<string, VariantPolicyConfigObject>;
1420
+ componentPolicies: Array<{ componentId: ComponentID; policy: any }>;
1421
+ components: Component[];
1422
+ patterns?: string[];
1423
+ forceVersionBump?: 'major' | 'minor' | 'patch' | 'compatible';
1424
+ }): Promise<MergedOutdatedPkg[] | null> {
1425
+ const localComponentPkgNames = new Set(components.map((component) => this.getPackageName(component)));
1426
+ const componentModelVersions: ComponentModelVersion[] = (
1427
+ await Promise.all(
1428
+ components.map(async (component) => {
1429
+ const depList = await this.getDependencies(component);
1430
+ return depList
1431
+ .filter(
1432
+ (dep) =>
1433
+ typeof dep.getPackageName === 'function' &&
1434
+ // If the dependency is referenced not via a valid range it means that it wasn't yet published to the registry
1435
+ semver.validRange(dep.version) != null &&
1436
+ !dep['isExtension'] && // eslint-disable-line
1437
+ dep.lifecycle !== 'peer' &&
1438
+ !localComponentPkgNames.has(dep.getPackageName())
1439
+ )
1440
+ .map((dep) => ({
1441
+ name: dep.getPackageName!(), // eslint-disable-line
1442
+ version: dep.version,
1443
+ isAuto: dep.source === 'auto',
1444
+ componentId: component.id,
1445
+ lifecycleType: dep.lifecycle,
1446
+ }));
1447
+ })
1448
+ )
1449
+ ).flat();
1450
+ let allPkgs = getAllPolicyPkgs({
1451
+ rootPolicy: this.getWorkspacePolicyFromConfig(),
1452
+ variantPoliciesByPatterns,
1453
+ componentPolicies,
1454
+ componentModelVersions,
1455
+ });
1456
+ if (patterns?.length) {
1457
+ const selectedPkgNames = new Set(
1458
+ multimatch(
1459
+ allPkgs.map(({ name }) => name),
1460
+ patterns
1461
+ )
1462
+ );
1463
+ allPkgs = allPkgs.filter(({ name }) => selectedPkgNames.has(name));
1464
+ if (!allPkgs.length) {
1465
+ return null;
1466
+ }
1467
+ }
1468
+ const outdatedPkgs = await this.getOutdatedPkgs({ rootDir, forceVersionBump }, allPkgs);
1469
+ return mergeOutdatedPkgs(outdatedPkgs);
1470
+ }
1471
+
1472
+ /**
1473
+ * Accepts a list of package dependency policies and returns a list of outdated policies extended with their "latestRange"
1474
+ */
1475
+ async getOutdatedPkgs<T>(
1476
+ {
1477
+ rootDir,
1478
+ forceVersionBump,
1479
+ }: {
1480
+ rootDir: string;
1481
+ forceVersionBump?: 'major' | 'minor' | 'patch' | 'compatible';
1482
+ },
1483
+ pkgs: Array<
1484
+ { name: string; currentRange: string; source: 'variants' | 'component' | 'rootPolicy' | 'component-model' } & T
1485
+ >
1486
+ ): Promise<Array<{ name: string; currentRange: string; latestRange: string } & T>> {
1487
+ this.logger.setStatusLine('checking the latest versions of dependencies');
1488
+ const resolver = await this.getVersionResolver();
1489
+ const tryResolve = async (spec: string) => {
1490
+ try {
1491
+ return (
1492
+ await resolver.resolveRemoteVersion(spec, {
1493
+ rootDir,
1494
+ })
1495
+ ).version;
1496
+ } catch {
1497
+ // If latest cannot be found for the package, then just ignore it
1498
+ return null;
1499
+ }
1500
+ };
1501
+ const outdatedPkgs = compact(
1502
+ await Promise.all(
1503
+ pkgs.map(async (pkg) => {
1504
+ const latestVersion = await tryResolve(
1505
+ `${pkg.name}@${newVersionRange(pkg.currentRange, { pkgSource: pkg.source, forceVersionBump })}`
1506
+ );
1507
+ if (!latestVersion) return null;
1508
+ const currentVersion = semver.valid(pkg.currentRange.replace(/[\^~]/, ''));
1509
+ // If the current version is newer than the latest, then no need to update the dependency
1510
+ if (currentVersion && (semver.gt(currentVersion, latestVersion) || currentVersion === latestVersion))
1511
+ return null;
1512
+ return {
1513
+ ...pkg,
1514
+ latestRange:
1515
+ pkg.source === 'component-model' && this.config.savePrefix != null
1516
+ ? `${this.config.savePrefix}${latestVersion}`
1517
+ : repeatPrefix(pkg.currentRange, latestVersion),
1518
+ } as any;
1519
+ })
1520
+ )
1521
+ );
1522
+ this.logger.consoleSuccess();
1523
+ return outdatedPkgs;
1524
+ }
1525
+
1526
+ /**
1527
+ * Update the specified packages to their latest versions in all policies;
1528
+ * root polcies, variant pocilicies, and component configuration policies (component.json).
1529
+ */
1530
+ applyUpdates(
1531
+ outdatedPkgs: Array<Omit<OutdatedPkg, 'currentRange'>>,
1532
+ options: {
1533
+ variantPoliciesByPatterns: Record<string, any>;
1534
+ }
1535
+ ): {
1536
+ updatedVariants: string[];
1537
+ updatedComponents: UpdatedComponent[];
1538
+ } {
1539
+ const { updatedVariants, updatedComponents, updatedWorkspacePolicyEntries } = applyUpdates(outdatedPkgs, {
1540
+ variantPoliciesByPatterns: options.variantPoliciesByPatterns,
1541
+ });
1542
+ this.addToRootPolicy(updatedWorkspacePolicyEntries, {
1543
+ updateExisting: true,
1544
+ });
1545
+ return {
1546
+ updatedVariants,
1547
+ updatedComponents,
1548
+ };
1549
+ }
1550
+
1551
+ static runtime = MainRuntime;
1552
+ static dependencies = [
1553
+ EnvsAspect,
1554
+ LoggerAspect,
1555
+ ConfigAspect,
1556
+ AspectLoaderAspect,
1557
+ ComponentAspect,
1558
+ GraphqlAspect,
1559
+ GlobalConfigAspect,
1560
+ ];
1561
+
1562
+ static slots = [
1563
+ Slot.withType<WorkspacePolicy>(),
1564
+ Slot.withType<VariantPolicyConfigObject>(),
1565
+ Slot.withType<PackageManager>(),
1566
+ Slot.withType<RegExp>(),
1567
+ Slot.withType<DependencyFactory>(),
1568
+ Slot.withType<PreInstallSubscriberList>(),
1569
+ Slot.withType<PostInstallSubscriberList>(),
1570
+ Slot.withType<DependencyDetector>(),
1571
+ ];
1572
+
1573
+ static defaultConfig: DependencyResolverWorkspaceConfig &
1574
+ Required<Pick<DependencyResolverWorkspaceConfig, 'linkCoreAspects'>> = {
1575
+ /**
1576
+ * default package manager.
1577
+ */
1578
+ packageManager: 'teambit.dependencies/pnpm',
1579
+ policy: {},
1580
+ linkCoreAspects: true,
1581
+ };
1582
+
1583
+ static async provider(
1584
+ [envs, loggerExt, configMain, aspectLoader, componentAspect, graphql, globalConfig]: [
1585
+ EnvsMain,
1586
+ LoggerMain,
1587
+ ConfigMain,
1588
+ AspectLoaderMain,
1589
+ ComponentMain,
1590
+ GraphqlMain,
1591
+ GlobalConfigMain
1592
+ ],
1593
+ config: DependencyResolverWorkspaceConfig,
1594
+ [
1595
+ rootPolicyRegistry,
1596
+ policiesRegistry,
1597
+ packageManagerSlot,
1598
+ dependencyFactorySlot,
1599
+ preInstallSlot,
1600
+ postInstallSlot,
1601
+ ]: [
1602
+ RootPolicyRegistry,
1603
+ PoliciesRegistry,
1604
+ PackageManagerSlot,
1605
+ DependencyFactorySlot,
1606
+ PreInstallSlot,
1607
+ PostInstallSlot
1608
+ ]
1609
+ ) {
1610
+ // const packageManager = new PackageManagerLegacy(config.packageManager, logger);
1611
+ const logger = loggerExt.createLogger(DependencyResolverAspect.id);
1612
+ const dependencyResolver = new DependencyResolverMain(
1613
+ config,
1614
+ rootPolicyRegistry,
1615
+ policiesRegistry,
1616
+ envs,
1617
+ logger,
1618
+ configMain,
1619
+ aspectLoader,
1620
+ globalConfig,
1621
+ componentAspect,
1622
+ packageManagerSlot,
1623
+ dependencyFactorySlot,
1624
+ preInstallSlot,
1625
+ postInstallSlot
1626
+ );
1627
+
1628
+ componentAspect.registerShowFragments([
1629
+ new DependenciesFragment(dependencyResolver),
1630
+ new DevDependenciesFragment(dependencyResolver),
1631
+ new PeerDependenciesFragment(dependencyResolver),
1632
+ ]);
1633
+ // TODO: solve this generics issue and remove the ts-ignore
1634
+ // @ts-ignore
1635
+ dependencyResolver.registerDependencyFactories([new ComponentDependencyFactory(componentAspect)]);
1636
+
1637
+ LegacyComponent.registerOnComponentOverridesLoading(
1638
+ DependencyResolverAspect.id,
1639
+ async (configuredExtensions: ExtensionDataList, id: ComponentID, legacyFiles: SourceFile[]) => {
1640
+ const policy = await dependencyResolver.mergeVariantPolicies(configuredExtensions, id, legacyFiles);
1641
+ return policy.toLegacyDepsOverrides();
1642
+ }
1643
+ );
1644
+ if (aspectLoader)
1645
+ aspectLoader.registerOnLoadRequireableExtensionSlot(
1646
+ dependencyResolver.onLoadRequireableExtensionSubscriber.bind(dependencyResolver)
1647
+ );
1648
+
1649
+ graphql.register(dependencyResolverSchema(dependencyResolver));
1650
+ envs.registerService(new DependenciesService());
1651
+
1652
+ // this is needed because during tag process, the data.dependencies can be loaded and the componentId can become
1653
+ // an instance of ComponentID class. it needs to be serialized before saved into objects.
1654
+ const serializeDepResolverDataBeforePersist = (extDataList: ExtensionDataList) => {
1655
+ const entry = extDataList.findCoreExtension(DependencyResolverAspect.id);
1656
+ if (!entry) return;
1657
+ const dependencies = get(entry, ['data', 'dependencies'], []);
1658
+ dependencies.forEach((dep) => {
1659
+ if (dep.__type === COMPONENT_DEP_TYPE) {
1660
+ dep.componentId = dep.componentId instanceof ComponentID ? dep.componentId.serialize() : dep.componentId;
1661
+ }
1662
+ });
1663
+ };
1664
+ ExtensionDataList.toModelObjectsHook.push(serializeDepResolverDataBeforePersist);
1665
+ PackageJsonTransformer.registerPackageJsonTransformer(async (component, packageJsonObject) => {
1666
+ const deps = await dependencyResolver.getDependencies(component);
1667
+ const { optionalDependencies, peerDependenciesMeta } = deps.toDependenciesManifest();
1668
+ packageJsonObject.optionalDependencies = optionalDependencies;
1669
+ packageJsonObject.peerDependenciesMeta = peerDependenciesMeta;
1670
+ return packageJsonObject;
1671
+ });
1672
+
1673
+ return dependencyResolver;
1674
+ }
1675
+
1676
+ getEmptyDepsObject(): ManifestDependenciesObject {
1677
+ return {
1678
+ dependencies: {},
1679
+ devDependencies: {},
1680
+ peerDependencies: {},
1681
+ };
1682
+ }
1683
+
1684
+ /**
1685
+ * Returns a list of target locations where that given component was hard linked to.
1686
+ *
1687
+ * @param rootDir - The root directory of the workspace
1688
+ * @param componentDir - Relative path to the component's directory
1689
+ * @param packageName - The injected component's packageName
1690
+ */
1691
+ async getInjectedDirs(rootDir: string, componentDir: string, packageName: string): Promise<string[]> {
1692
+ const packageManager = this.getPackageManager();
1693
+ if (typeof packageManager?.getInjectedDirs === 'function') {
1694
+ return packageManager.getInjectedDirs(rootDir, componentDir, packageName);
1695
+ }
1696
+ return [];
1697
+ }
1698
+
1699
+ getWorkspaceDepsOfBitRoots(manifests: ProjectManifest[]): Record<string, string> {
1700
+ const packageManager = this.getPackageManager();
1701
+ if (!packageManager) {
1702
+ throw new PackageManagerNotFound(this.config.packageManager);
1703
+ }
1704
+ return packageManager.getWorkspaceDepsOfBitRoots(manifests);
1705
+ }
1706
+ }
1707
+
1708
+ DependencyResolverAspect.addRuntime(DependencyResolverMain);
1709
+
1710
+ function repeatPrefix(originalSpec: string, newVersion: string): string {
1711
+ switch (originalSpec[0]) {
1712
+ case '~':
1713
+ case '^':
1714
+ return `${originalSpec[0]}${newVersion}`;
1715
+ default:
1716
+ return newVersion;
1717
+ }
1718
+ }
1719
+
1720
+ function newVersionRange(
1721
+ currentRange: string,
1722
+ opts: { pkgSource: CurrentPkgSource; forceVersionBump?: 'major' | 'minor' | 'patch' | 'compatible' }
1723
+ ) {
1724
+ if (opts.forceVersionBump == null || opts.forceVersionBump === 'major') return 'latest';
1725
+ const currentVersion = semver.valid(currentRange.replace(/[\^~]/, ''));
1726
+ if (opts.forceVersionBump === 'compatible') {
1727
+ if ((opts.pkgSource === 'component' || opts.pkgSource === 'component-model') && currentVersion === currentRange) {
1728
+ return `^${currentVersion}`;
1729
+ }
1730
+ return currentRange;
1731
+ }
1732
+ if (!currentVersion) return null;
1733
+ const [major, minor] = currentVersion.split('.');
1734
+ switch (opts.forceVersionBump) {
1735
+ case 'patch':
1736
+ return `>=${currentVersion} <${major}.${+minor + 1}.0`;
1737
+ case 'minor':
1738
+ return `>=${currentVersion} <${+major + 1}.0.0`;
1739
+ default:
1740
+ return null;
1741
+ }
1742
+ }
1743
+
1744
+ export interface MergedOutdatedPkg extends OutdatedPkg {
1745
+ dependentComponents?: ComponentID[];
1746
+ hasDifferentRanges?: boolean;
1747
+ }
1748
+
1749
+ function mergeOutdatedPkgs(outdatedPkgs: OutdatedPkg[]): MergedOutdatedPkg[] {
1750
+ const mergedOutdatedPkgs: Record<
1751
+ string,
1752
+ MergedOutdatedPkg & Required<Pick<MergedOutdatedPkg, 'dependentComponents'>>
1753
+ > = {};
1754
+ const outdatedPkgsNotFromComponentModel: OutdatedPkg[] = [];
1755
+ for (const outdatedPkg of outdatedPkgs) {
1756
+ if (outdatedPkg.source === 'component-model' && outdatedPkg.componentId) {
1757
+ if (!mergedOutdatedPkgs[outdatedPkg.name]) {
1758
+ mergedOutdatedPkgs[outdatedPkg.name] = {
1759
+ ...omit(outdatedPkg, ['componentId']),
1760
+ source: 'rootPolicy',
1761
+ dependentComponents: [outdatedPkg.componentId],
1762
+ };
1763
+ } else {
1764
+ if (mergedOutdatedPkgs[outdatedPkg.name].currentRange !== outdatedPkg.currentRange) {
1765
+ mergedOutdatedPkgs[outdatedPkg.name].hasDifferentRanges = true;
1766
+ }
1767
+ mergedOutdatedPkgs[outdatedPkg.name].currentRange = tryPickLowestRange(
1768
+ mergedOutdatedPkgs[outdatedPkg.name].currentRange,
1769
+ outdatedPkg.currentRange
1770
+ );
1771
+ mergedOutdatedPkgs[outdatedPkg.name].dependentComponents.push(outdatedPkg.componentId);
1772
+ if (outdatedPkg.targetField === 'dependencies') {
1773
+ mergedOutdatedPkgs[outdatedPkg.name].targetField = outdatedPkg.targetField;
1774
+ }
1775
+ }
1776
+ } else {
1777
+ outdatedPkgsNotFromComponentModel.push(outdatedPkg);
1778
+ }
1779
+ }
1780
+ return [...Object.values(mergedOutdatedPkgs), ...outdatedPkgsNotFromComponentModel];
1781
+ }
1782
+
1783
+ function tryPickLowestRange(range1: string, range2: string) {
1784
+ if (range1 === '*' || range2 === '*') return '*';
1785
+ try {
1786
+ return semver.lt(rangeToVersion(range1), rangeToVersion(range2)) ? range1 : range2;
1787
+ } catch {
1788
+ return '*';
1789
+ }
1790
+ }
1791
+
1792
+ function rangeToVersion(range: string) {
1793
+ if (range.startsWith('~') || range.startsWith('^')) {
1794
+ return range.substring(1);
1795
+ }
1796
+ return range;
1797
+ }