@teambit/workspace 1.0.108 → 1.0.110

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.
@@ -1,885 +0,0 @@
1
- import { CFG_CAPSULES_BUILD_COMPONENTS_BASE_DIR } from '@teambit/legacy/dist/constants';
2
- import { GlobalConfigMain } from '@teambit/global-config';
3
- import findRoot from 'find-root';
4
- import { resolveFrom } from '@teambit/toolbox.modules.module-resolver';
5
- import { Graph } from '@teambit/graph.cleargraph';
6
- import { ExtensionDataList } from '@teambit/legacy/dist/consumer/config/extension-data';
7
- import { ExtensionManifest, Harmony, Aspect } from '@teambit/harmony';
8
- import {
9
- AspectDefinition,
10
- AspectLoaderMain,
11
- AspectResolver,
12
- getAspectDef,
13
- ResolvedAspect,
14
- } from '@teambit/aspect-loader';
15
- import { MainRuntime } from '@teambit/cli';
16
- import fs from 'fs-extra';
17
- import { RequireableComponent } from '@teambit/harmony.modules.requireable-component';
18
- import { linkToNodeModulesByIds } from '@teambit/workspace.modules.node-modules-linker';
19
- import { ComponentID } from '@teambit/component-id';
20
- import { ComponentNotFound } from '@teambit/legacy/dist/scope/exceptions';
21
- import pMapSeries from 'p-map-series';
22
- import { difference, compact, groupBy, partition } from 'lodash';
23
- import { Consumer } from '@teambit/legacy/dist/consumer';
24
- import { Component, LoadAspectsOptions, ResolveAspectsOptions } from '@teambit/component';
25
- import { ScopeMain } from '@teambit/scope';
26
- import { Logger } from '@teambit/logger';
27
- import { BitError } from '@teambit/bit-error';
28
- import { EnvsMain } from '@teambit/envs';
29
- import { ConfigMain } from '@teambit/config';
30
- import { DependencyResolverMain } from '@teambit/dependency-resolver';
31
- import { ShouldLoadFunc } from './build-graph-from-fs';
32
- import type { Workspace } from './workspace';
33
- import { OnAspectsResolve, OnAspectsResolveSlot, OnRootAspectAdded, OnRootAspectAddedSlot } from './workspace.provider';
34
- import { ComponentLoadOptions } from './workspace-component/workspace-component-loader';
35
-
36
- export type GetConfiguredUserAspectsPackagesOptions = {
37
- externalsOnly?: boolean;
38
- };
39
-
40
- export type WorkspaceLoadAspectsOptions = LoadAspectsOptions & {
41
- useScopeAspectsCapsule?: boolean;
42
- runSubscribers?: boolean;
43
- skipDeps?: boolean;
44
- resolveEnvsFromRoots?: boolean;
45
- };
46
-
47
- export type AspectPackage = { packageName: string; version: string };
48
-
49
- export class WorkspaceAspectsLoader {
50
- private consumer: Consumer;
51
- private resolvedInstalledAspects: Map<string, string | null>;
52
-
53
- constructor(
54
- private workspace: Workspace,
55
- private scope: ScopeMain,
56
- private aspectLoader: AspectLoaderMain,
57
- private envs: EnvsMain,
58
- private dependencyResolver: DependencyResolverMain,
59
- private logger: Logger,
60
- private globalConfig: GlobalConfigMain,
61
- private harmony: Harmony,
62
- private onAspectsResolveSlot: OnAspectsResolveSlot,
63
- private onRootAspectAddedSlot: OnRootAspectAddedSlot,
64
- private resolveAspectsFromNodeModules = false,
65
- private resolveEnvsFromRoots = false
66
- ) {
67
- this.consumer = this.workspace.consumer;
68
- this.resolvedInstalledAspects = new Map();
69
- // Only enable this when root components is enabled as well
70
- this.resolveEnvsFromRoots = this.resolveEnvsFromRoots && this.dependencyResolver.hasRootComponents();
71
- }
72
-
73
- /**
74
- * load aspects from the workspace and if not exists in the workspace, load from the node_modules.
75
- * keep in mind that the graph may have circles.
76
- */
77
- async loadAspects(
78
- ids: string[] = [],
79
- throwOnError?: boolean,
80
- neededFor?: string,
81
- opts: WorkspaceLoadAspectsOptions = {}
82
- ): Promise<string[]> {
83
- const calculatedThrowOnError: boolean = throwOnError ?? false;
84
- const defaultOpts: Required<WorkspaceLoadAspectsOptions> = {
85
- useScopeAspectsCapsule: false,
86
- throwOnError: calculatedThrowOnError,
87
- runSubscribers: true,
88
- skipDeps: false,
89
- hideMissingModuleError: !!this.workspace.inInstallContext,
90
- ignoreErrors: false,
91
- resolveEnvsFromRoots: this.resolveEnvsFromRoots,
92
- };
93
- const mergedOpts: Required<WorkspaceLoadAspectsOptions> = { ...defaultOpts, ...opts };
94
-
95
- // generate a random callId to be able to identify the call from the logs
96
- const callId = Math.floor(Math.random() * 1000);
97
- const loggerPrefix = `[${callId}] loadAspects,`;
98
- this.logger.info(`${loggerPrefix} loading ${ids.length} aspects.
99
- ids: ${ids.join(', ')}
100
- needed-for: ${neededFor || '<unknown>'}. using opts: ${JSON.stringify(mergedOpts, null, 2)}`);
101
- const [localAspects, nonLocalAspects] = partition(ids, (id) => id.startsWith('file:'));
102
- this.workspace.localAspects = localAspects;
103
- await this.aspectLoader.loadAspectFromPath(this.workspace.localAspects);
104
- const notLoadedIds = nonLocalAspects.filter((id) => !this.aspectLoader.isAspectLoaded(id));
105
- if (!notLoadedIds.length) return [];
106
- const coreAspectsStringIds = this.aspectLoader.getCoreAspectIds();
107
- const idsWithoutCore: string[] = difference(notLoadedIds, coreAspectsStringIds);
108
-
109
- const componentIds = await this.workspace.resolveMultipleComponentIds(idsWithoutCore);
110
-
111
- const { workspaceIds, nonWorkspaceIds } = await this.groupIdsByWorkspaceExistence(
112
- componentIds,
113
- mergedOpts.resolveEnvsFromRoots
114
- );
115
-
116
- this.logFoundWorkspaceVsScope(loggerPrefix, workspaceIds, nonWorkspaceIds);
117
- let idsToLoadFromWs = componentIds;
118
- let scopeAspectIds: string[] = [];
119
-
120
- // TODO: hard coded use the old approach and loading from the scope capsules
121
- // This is because right now loading from the ws node_modules causes issues in some cases
122
- // like for the cloud app
123
- // it should be removed once we fix the issues
124
- if (!this.resolveAspectsFromNodeModules) {
125
- mergedOpts.useScopeAspectsCapsule = true;
126
- }
127
-
128
- if (mergedOpts.useScopeAspectsCapsule) {
129
- idsToLoadFromWs = workspaceIds;
130
- scopeAspectIds = await this.loadFromScopeAspectsCapsule(nonWorkspaceIds, throwOnError, neededFor);
131
- }
132
-
133
- const aspectsDefs = await this.resolveAspects(undefined, idsToLoadFromWs, {
134
- excludeCore: true,
135
- requestedOnly: false,
136
- ...mergedOpts,
137
- });
138
-
139
- const { manifests, requireableComponents } = await this.loadAspectDefsByOrder(
140
- aspectsDefs,
141
- idsWithoutCore,
142
- mergedOpts.throwOnError,
143
- mergedOpts.hideMissingModuleError,
144
- neededFor,
145
- mergedOpts.runSubscribers
146
- );
147
-
148
- const potentialPluginsIndexes = compact(
149
- manifests.map((manifest, index) => {
150
- if (this.aspectLoader.isValidAspect(manifest)) return undefined;
151
- return index;
152
- })
153
- );
154
-
155
- // Try require components for potential plugins
156
- const pluginsRequireableComponents = potentialPluginsIndexes.map((index) => {
157
- return requireableComponents[index];
158
- });
159
- // Do the require again now that the plugins defs already registered
160
- const pluginsManifests = await this.aspectLoader.getManifestsFromRequireableExtensions(
161
- pluginsRequireableComponents,
162
- throwOnError,
163
- opts.runSubscribers
164
- );
165
-
166
- await this.aspectLoader.loadExtensionsByManifests(pluginsManifests, undefined, { throwOnError });
167
- this.logger.debug(`${loggerPrefix} finish loading aspects`);
168
- const manifestIds = manifests.map((manifest) => manifest.id);
169
- return compact(manifestIds.concat(scopeAspectIds));
170
- }
171
-
172
- private async loadFromScopeAspectsCapsule(ids: ComponentID[], throwOnError?: boolean, neededFor?: string) {
173
- let scopeAspectIds: string[] = [];
174
- const currentLane = await this.consumer.getCurrentLaneObject();
175
-
176
- if (!ids.length) return [];
177
-
178
- const nonWorkspaceIdsString = ids.map((id) => id.toString());
179
- try {
180
- scopeAspectIds = await this.scope.loadAspects(
181
- nonWorkspaceIdsString,
182
- throwOnError,
183
- neededFor,
184
- currentLane || undefined,
185
- {
186
- packageManagerConfigRootDir: this.workspace.path,
187
- workspaceName: this.workspace.name,
188
- }
189
- );
190
- return scopeAspectIds;
191
- } catch (err: any) {
192
- this.throwWsJsoncAspectNotFoundError(err);
193
- return scopeAspectIds;
194
-
195
- throw err;
196
- }
197
- }
198
-
199
- throwWsJsoncAspectNotFoundError(err: any) {
200
- if (err instanceof ComponentNotFound) {
201
- const config = this.harmony.get<ConfigMain>('teambit.harmony/config');
202
- const configStr = JSON.stringify(config.workspaceConfig?.raw || {});
203
- if (configStr.includes(err.id)) {
204
- throw new BitError(`error: a component "${err.id}" was not found
205
- your workspace.jsonc has this component-id set. you might want to remove/change it.`);
206
- }
207
- }
208
- }
209
-
210
- private async loadAspectDefsByOrder(
211
- aspectsDefs: AspectDefinition[],
212
- seeders: string[],
213
- throwOnError: boolean,
214
- hideMissingModuleError: boolean,
215
- neededFor?: string,
216
- runSubscribers = true
217
- ): Promise<{ manifests: Array<Aspect | ExtensionManifest>; requireableComponents: RequireableComponent[] }> {
218
- const { nonWorkspaceDefs } = await this.groupAspectDefsByWorkspaceExistence(aspectsDefs);
219
- const scopeAspectsLoader = this.scope.getScopeAspectsLoader();
220
- const scopeIds: string[] = compact(nonWorkspaceDefs.map((aspectDef) => aspectDef.getId));
221
- const scopeIdsGrouped = await scopeAspectsLoader.groupAspectIdsByEnvOfTheList(scopeIds);
222
-
223
- // Make sure to first load envs from the list otherwise it will fail when trying to load other aspects
224
- // as their envs might not be loaded yet
225
- if (scopeIdsGrouped.envs && scopeIdsGrouped.envs.length && !runSubscribers) {
226
- await this.scope.loadAspects(scopeIdsGrouped.envs, throwOnError, 'workspace.loadAspects loading scope aspects');
227
- }
228
- const requireableComponents = this.aspectDefsToRequireableComponents(aspectsDefs);
229
- const manifests = await this.aspectLoader.getManifestsFromRequireableExtensions(
230
- requireableComponents,
231
- throwOnError,
232
- runSubscribers
233
- );
234
- await this.aspectLoader.loadExtensionsByManifests(
235
- manifests,
236
- { seeders, neededFor },
237
- { throwOnError, hideMissingModuleError }
238
- );
239
- return { manifests, requireableComponents };
240
- }
241
-
242
- async resolveAspects(
243
- runtimeName?: string,
244
- componentIds?: ComponentID[],
245
- opts?: ResolveAspectsOptions
246
- ): Promise<AspectDefinition[]> {
247
- const callId = Math.floor(Math.random() * 1000);
248
- const loggerPrefix = `[${callId}] workspace resolveAspects,`;
249
-
250
- this.logger.debug(
251
- `${loggerPrefix}, resolving aspects for - runtimeName: ${runtimeName}, componentIds: ${componentIds}`
252
- );
253
- const defaultOpts: ResolveAspectsOptions = {
254
- excludeCore: false,
255
- requestedOnly: false,
256
- filterByRuntime: true,
257
- useScopeAspectsCapsule: false,
258
- workspaceName: this.workspace.name,
259
- resolveEnvsFromRoots: this.resolveEnvsFromRoots,
260
- packageManagerConfigRootDir: this.workspace.path,
261
- };
262
- const mergedOpts = { ...defaultOpts, ...opts };
263
- const idsToResolve = componentIds ? componentIds.map((id) => id.toString()) : this.harmony.extensionsIds;
264
- const coreAspectsIds = this.aspectLoader.getCoreAspectIds();
265
- const configuredAspects = this.aspectLoader.getConfiguredAspects();
266
- // it's possible that componentIds are core-aspects that got version for some reason, remove the version to
267
- // correctly filter them out later.
268
- const userAspectsIds: string[] = componentIds
269
- ? componentIds.filter((id) => !coreAspectsIds.includes(id.toStringWithoutVersion())).map((id) => id.toString())
270
- : difference(this.harmony.extensionsIds, coreAspectsIds);
271
- const rootAspectsIds: string[] = difference(configuredAspects, coreAspectsIds);
272
- const componentIdsToResolve = await this.workspace.resolveMultipleComponentIds(userAspectsIds);
273
- const components = await this.importAndGetAspects(componentIdsToResolve);
274
-
275
- // Run the on load slot
276
- await this.runOnAspectsResolveFunctions(components);
277
-
278
- if (opts?.skipDeps) {
279
- const wsAspectDefs = await this.aspectLoader.resolveAspects(
280
- components,
281
- this.getWorkspaceAspectResolver([], runtimeName)
282
- );
283
-
284
- const coreAspectDefs = await Promise.all(
285
- coreAspectsIds.map(async (coreId) => {
286
- const rawDef = await getAspectDef(coreId, runtimeName);
287
- return this.aspectLoader.loadDefinition(rawDef);
288
- })
289
- );
290
-
291
- const idsToFilter = idsToResolve.map((idStr) => ComponentID.fromString(idStr));
292
- const targetDefs = wsAspectDefs.concat(coreAspectDefs);
293
- const finalDefs = this.aspectLoader.filterAspectDefs(targetDefs, idsToFilter, runtimeName, mergedOpts);
294
-
295
- return finalDefs;
296
- }
297
-
298
- const groupedByIsPlugin = groupBy(components, (component) => {
299
- return this.aspectLoader.hasPluginFiles(component);
300
- });
301
- const graph = await this.getAspectsGraphWithoutCore(groupedByIsPlugin.false, this.isAspect.bind(this));
302
- const aspectsComponents = graph.nodes.map((node) => node.attr).concat(groupedByIsPlugin.true || []);
303
- this.logger.debug(`${loggerPrefix} found ${aspectsComponents.length} aspects in the aspects-graph`);
304
- const { workspaceComps, nonWorkspaceComps } = await this.groupComponentsByWorkspaceExistence(
305
- aspectsComponents,
306
- mergedOpts.resolveEnvsFromRoots
307
- );
308
-
309
- const workspaceCompsIds = workspaceComps.map((c) => c.id);
310
- const nonWorkspaceCompsIds = nonWorkspaceComps.map((c) => c.id);
311
- this.logFoundWorkspaceVsScope(loggerPrefix, workspaceCompsIds, nonWorkspaceCompsIds);
312
-
313
- const stringIds: string[] = [];
314
- const wsAspectDefs = await this.aspectLoader.resolveAspects(
315
- workspaceComps,
316
- this.getWorkspaceAspectResolver(stringIds, runtimeName)
317
- );
318
-
319
- await this.linkIfMissingWorkspaceAspects(wsAspectDefs, workspaceCompsIds);
320
-
321
- // TODO: hard coded use the old approach and loading from the scope capsules
322
- // This is because right now loading from the ws node_modules causes issues in some cases
323
- // like for the cloud app
324
- // it should be removed once we fix the issues
325
- if (!this.resolveAspectsFromNodeModules) {
326
- mergedOpts.useScopeAspectsCapsule = true;
327
- }
328
-
329
- let componentsToResolveFromScope = nonWorkspaceComps;
330
- let componentsToResolveFromInstalled: Component[] = [];
331
- if (!mergedOpts.useScopeAspectsCapsule) {
332
- const nonWorkspaceCompsGroups = groupBy(nonWorkspaceComps, (component) => this.envs.isEnv(component));
333
- componentsToResolveFromScope = nonWorkspaceCompsGroups.true || [];
334
- componentsToResolveFromInstalled = nonWorkspaceCompsGroups.false || [];
335
- }
336
-
337
- const scopeIds = componentsToResolveFromScope.map((c) => c.id);
338
- this.logger.debug(
339
- `${loggerPrefix} ${
340
- scopeIds.length
341
- } components are not in the workspace and are loaded from the scope capsules:\n${scopeIds
342
- .map((id) => id.toString())
343
- .join('\n')}`
344
- );
345
- const scopeAspectsDefs: AspectDefinition[] = scopeIds.length
346
- ? await this.scope.resolveAspects(runtimeName, scopeIds, mergedOpts)
347
- : [];
348
-
349
- this.logger.debug(
350
- `${loggerPrefix} ${
351
- componentsToResolveFromInstalled.length
352
- } components are not in the workspace and are loaded from the node_modules:\n${componentsToResolveFromInstalled
353
- .map((c) => c.id.toString())
354
- .join('\n')}`
355
- );
356
- const installedAspectsDefs: AspectDefinition[] = componentsToResolveFromInstalled.length
357
- ? await this.aspectLoader.resolveAspects(
358
- componentsToResolveFromInstalled,
359
- this.getInstalledAspectResolver(graph, rootAspectsIds, runtimeName, {
360
- throwOnError: opts?.throwOnError ?? false,
361
- })
362
- )
363
- : [];
364
-
365
- let coreAspectDefs = await Promise.all(
366
- coreAspectsIds.map(async (coreId) => {
367
- const rawDef = await getAspectDef(coreId, runtimeName);
368
- return this.aspectLoader.loadDefinition(rawDef);
369
- })
370
- );
371
-
372
- // due to lack of workspace and scope runtimes. TODO: fix after adding them.
373
- if (runtimeName && mergedOpts.filterByRuntime) {
374
- coreAspectDefs = coreAspectDefs.filter((coreAspect) => {
375
- return coreAspect.runtimePath;
376
- });
377
- }
378
- const localResolved = await this.aspectLoader.resolveLocalAspects(this.workspace.localAspects, runtimeName);
379
- const allDefsExceptLocal = [...wsAspectDefs, ...coreAspectDefs, ...scopeAspectsDefs, ...installedAspectsDefs];
380
- const withoutLocalAspects = allDefsExceptLocal.filter((aspectId) => {
381
- return !localResolved.find((localAspect) => {
382
- return localAspect.id === aspectId.component?.id?.toStringWithoutVersion();
383
- });
384
- });
385
- const allDefs = [...withoutLocalAspects, ...localResolved];
386
- const idsToFilter = idsToResolve.map((idStr) => ComponentID.fromString(idStr));
387
- const filteredDefs = this.aspectLoader.filterAspectDefs(allDefs, idsToFilter, runtimeName, mergedOpts);
388
- return filteredDefs;
389
- }
390
-
391
- shouldUseHashForCapsules(): boolean {
392
- return !this.globalConfig.getSync(CFG_CAPSULES_BUILD_COMPONENTS_BASE_DIR);
393
- }
394
-
395
- getCapsulePath() {
396
- const defaultPath = this.workspace.path;
397
- return this.globalConfig.getSync(CFG_CAPSULES_BUILD_COMPONENTS_BASE_DIR) || defaultPath;
398
- }
399
-
400
- private logFoundWorkspaceVsScope(loggerPrefix: string, workspaceIds: ComponentID[], nonWorkspaceIds: ComponentID[]) {
401
- const workspaceIdsStr = workspaceIds.length ? workspaceIds.map((id) => id.toString()).join('\n') : '';
402
- const nonWorkspaceIdsStr = nonWorkspaceIds.length ? nonWorkspaceIds.map((id) => id.toString()).join('\n') : '';
403
- this.logger.debug(
404
- `${loggerPrefix} found ${workspaceIds.length} components in the workspace, ${nonWorkspaceIds.length} not in the workspace`
405
- );
406
- if (workspaceIdsStr) this.logger.debug(`${loggerPrefix} workspace components:\n${workspaceIdsStr}`);
407
- if (nonWorkspaceIdsStr)
408
- this.logger.debug(
409
- `${loggerPrefix} non workspace components (loaded from the scope capsules or from the node_modules):\n${nonWorkspaceIdsStr}`
410
- );
411
- }
412
-
413
- async use(aspectIdStr: string): Promise<string> {
414
- let aspectId = await this.workspace.resolveComponentId(aspectIdStr);
415
- const inWs = await this.workspace.hasId(aspectId);
416
- let aspectIdToAdd = aspectId.toStringWithoutVersion();
417
-
418
- let aspectsComponent;
419
- // let aspectPackage;
420
- if (!inWs) {
421
- const aspectsComponents = await this.importAndGetAspects([aspectId]);
422
- if (aspectsComponents[0]) {
423
- aspectsComponent = aspectsComponents[0];
424
- aspectId = aspectsComponent.id;
425
- aspectIdToAdd = aspectId.toString();
426
- }
427
- }
428
-
429
- const config = this.harmony.get<ConfigMain>('teambit.harmony/config').workspaceConfig;
430
- if (!config) {
431
- throw new Error(`use() unable to get the workspace config`);
432
- }
433
- config.setExtension(
434
- aspectIdToAdd,
435
- {},
436
- {
437
- overrideExisting: false,
438
- ignoreVersion: false,
439
- }
440
- );
441
- await config.write();
442
- this.aspectLoader.addInMemoryConfiguredAspect(aspectIdToAdd);
443
- await this.runOnRootAspectAddedFunctions(aspectId, inWs);
444
- return aspectIdToAdd;
445
- }
446
-
447
- async getConfiguredUserAspectsPackages(
448
- options: GetConfiguredUserAspectsPackagesOptions = {}
449
- ): Promise<AspectPackage[]> {
450
- const configuredAspects = this.aspectLoader.getConfiguredAspects();
451
- const coreAspectsIds = this.aspectLoader.getCoreAspectIds();
452
- const userAspectsIds: string[] = difference(configuredAspects, coreAspectsIds);
453
- const componentIdsToResolve = await this.workspace.resolveMultipleComponentIds(
454
- userAspectsIds.filter((id) => !id.startsWith('file:'))
455
- );
456
- const aspectsComponents = await this.importAndGetAspects(componentIdsToResolve);
457
- let componentsToGetPackages = aspectsComponents;
458
- if (options.externalsOnly) {
459
- const { nonWorkspaceComps } = await this.groupComponentsByWorkspaceExistence(aspectsComponents);
460
- componentsToGetPackages = nonWorkspaceComps;
461
- }
462
- const packages = componentsToGetPackages.map((aspectComponent) => {
463
- const packageName = this.dependencyResolver.getPackageName(aspectComponent);
464
- const version = aspectComponent.id.version || '*';
465
- return { packageName, version };
466
- });
467
- return packages;
468
- }
469
-
470
- private aspectDefsToRequireableComponents(aspectDefs: AspectDefinition[]): RequireableComponent[] {
471
- const requireableComponents = aspectDefs.map((aspectDef) => {
472
- const localPath = aspectDef.aspectPath;
473
- const component = aspectDef.component;
474
- if (!component) return undefined;
475
- const requireFunc = async () => {
476
- const plugins = this.aspectLoader.getPlugins(component, localPath);
477
- if (plugins.has()) {
478
- return plugins.load(MainRuntime.name);
479
- }
480
-
481
- const isModule = await this.aspectLoader.isEsmModule(localPath);
482
-
483
- const aspect = !isModule
484
- ? // eslint-disable-next-line global-require, import/no-dynamic-require
485
- require(localPath)
486
- : // : await this.aspectLoader.loadEsm(join(localPath, 'dist', 'index.js'));
487
- await this.aspectLoader.loadEsm(localPath);
488
-
489
- // require aspect runtimes
490
- const runtimePath = await this.aspectLoader.getRuntimePath(component, localPath, MainRuntime.name);
491
- if (runtimePath) {
492
- if (isModule) await this.aspectLoader.loadEsm(runtimePath);
493
- // eslint-disable-next-line global-require, import/no-dynamic-require
494
- require(runtimePath);
495
- }
496
- return aspect;
497
- };
498
- return new RequireableComponent(component, requireFunc);
499
- });
500
- return compact(requireableComponents);
501
- }
502
-
503
- private async linkIfMissingWorkspaceAspects(aspects: AspectDefinition[], ids: ComponentID[]) {
504
- let missingPaths = false;
505
- const existsP = aspects.map(async (aspect) => {
506
- const exist = await fs.pathExists(aspect.aspectPath);
507
- if (!exist) {
508
- missingPaths = true;
509
- }
510
- });
511
- await Promise.all(existsP);
512
- // TODO: this should be done properly by the install aspect by slot
513
- if (missingPaths) {
514
- const bitIds: ComponentID[] = ids.map((id) => id);
515
- return linkToNodeModulesByIds(this.workspace, bitIds);
516
- }
517
- return Promise.resolve();
518
- }
519
-
520
- /**
521
- * This will return a resolver that knows to resolve aspects which are part of the workspace.
522
- * means aspects exist in the bitmap file
523
- * @param stringIds
524
- * @param runtimeName
525
- * @returns
526
- */
527
- private getWorkspaceAspectResolver(stringIds: string[], runtimeName?: string): AspectResolver {
528
- const workspaceAspectResolver = async (component: Component): Promise<ResolvedAspect> => {
529
- const compStringId = component.id.toString();
530
- stringIds.push(compStringId);
531
- const localPath = await this.workspace.getComponentPackagePath(component);
532
-
533
- const runtimePath = runtimeName
534
- ? await this.aspectLoader.getRuntimePath(component, localPath, runtimeName)
535
- : null;
536
-
537
- const aspectFilePath = await this.aspectLoader.getAspectFilePath(component, localPath);
538
-
539
- this.logger.debug(
540
- `workspace resolveAspects, resolving id: ${compStringId}, localPath: ${localPath}, runtimePath: ${runtimePath}`
541
- );
542
- return {
543
- aspectPath: localPath,
544
- aspectFilePath,
545
- runtimePath,
546
- };
547
- };
548
- return workspaceAspectResolver;
549
- }
550
-
551
- private async runOnAspectsResolveFunctions(aspectsComponents: Component[]): Promise<void> {
552
- const funcs = this.getOnAspectsResolveFunctions();
553
- await pMapSeries(funcs, async (func) => {
554
- try {
555
- await func(aspectsComponents);
556
- } catch (err) {
557
- this.logger.error('failed running onAspectsResolve function', err);
558
- }
559
- });
560
- }
561
-
562
- private getOnAspectsResolveFunctions(): OnAspectsResolve[] {
563
- const aspectsResolveFunctions = this.onAspectsResolveSlot.values();
564
- return aspectsResolveFunctions;
565
- }
566
-
567
- private async runOnRootAspectAddedFunctions(aspectsId: ComponentID, inWs: boolean): Promise<void> {
568
- const funcs = this.getOnRootAspectAddedFunctions();
569
- await pMapSeries(funcs, async (func) => {
570
- try {
571
- await func(aspectsId, inWs);
572
- } catch (err) {
573
- this.logger.error('failed running onRootAspectAdded function', err);
574
- }
575
- });
576
- }
577
-
578
- private getOnRootAspectAddedFunctions(): OnRootAspectAdded[] {
579
- const RootAspectAddedFunctions = this.onRootAspectAddedSlot.values();
580
- return RootAspectAddedFunctions;
581
- }
582
-
583
- /**
584
- * This will return a resolver that knows to resolve aspects which are not part of the workspace.
585
- * means aspects that does not exist in the bitmap file
586
- * instead it will resolve them from the node_modules recursively
587
- * @param graph
588
- * @param rootIds
589
- * @param runtimeName
590
- * @returns
591
- */
592
- private getInstalledAspectResolver(
593
- graph: Graph<Component, string>,
594
- rootIds: string[],
595
- runtimeName?: string,
596
- opts: { throwOnError: boolean } = { throwOnError: false }
597
- ): AspectResolver {
598
- const installedAspectsResolver = async (component: Component): Promise<ResolvedAspect | undefined> => {
599
- const compStringId = component.id.toString();
600
- // stringIds.push(compStringId);
601
- const localPath = await this.resolveInstalledAspectRecursively(component, rootIds, graph, opts);
602
- if (!localPath) return undefined;
603
-
604
- const runtimePath = runtimeName
605
- ? await this.aspectLoader.getRuntimePath(component, localPath, runtimeName)
606
- : null;
607
-
608
- const aspectFilePath = await this.aspectLoader.getAspectFilePath(component, localPath);
609
-
610
- this.logger.debug(
611
- `workspace resolveInstalledAspects, resolving id: ${compStringId}, localPath: ${localPath}, runtimePath: ${runtimePath}`
612
- );
613
- return {
614
- aspectPath: localPath,
615
- aspectFilePath,
616
- runtimePath,
617
- };
618
- };
619
- return installedAspectsResolver;
620
- }
621
-
622
- private async resolveInstalledAspectRecursively(
623
- aspectComponent: Component,
624
- rootIds: string[],
625
- graph: Graph<Component, string>,
626
- opts: { throwOnError: boolean } = { throwOnError: false }
627
- ): Promise<string | null | undefined> {
628
- const aspectStringId = aspectComponent.id.toString();
629
- if (this.resolvedInstalledAspects.has(aspectStringId)) {
630
- const resolvedPath = this.resolvedInstalledAspects.get(aspectStringId);
631
- return resolvedPath;
632
- }
633
- if (rootIds.includes(aspectStringId)) {
634
- const localPath = await this.workspace.getComponentPackagePath(aspectComponent);
635
- this.resolvedInstalledAspects.set(aspectStringId, localPath);
636
- return localPath;
637
- }
638
- const parent = graph.predecessors(aspectStringId)[0];
639
- if (!parent) return undefined;
640
- const parentPath = await this.resolveInstalledAspectRecursively(parent.attr, rootIds, graph);
641
- if (!parentPath) {
642
- this.resolvedInstalledAspects.set(aspectStringId, null);
643
- return undefined;
644
- }
645
- const packageName = this.dependencyResolver.getPackageName(aspectComponent);
646
- try {
647
- const resolvedPath = resolveFrom(parentPath, [packageName]);
648
- const localPath = findRoot(resolvedPath);
649
- this.resolvedInstalledAspects.set(aspectStringId, localPath);
650
- return localPath;
651
- } catch (error: any) {
652
- this.resolvedInstalledAspects.set(aspectStringId, null);
653
- if (opts.throwOnError) {
654
- throw error;
655
- }
656
- this.logger.consoleWarning(
657
- `failed resolving aspect ${aspectStringId} from ${parentPath}, error: ${error.message}`
658
- );
659
- return undefined;
660
- }
661
- }
662
-
663
- /**
664
- * Create a graph of aspects without the core aspects.
665
- * @param components
666
- * @param isAspect
667
- * @returns
668
- */
669
- private async getAspectsGraphWithoutCore(
670
- components: Component[] = [],
671
- isAspect?: ShouldLoadFunc
672
- ): Promise<Graph<Component, string>> {
673
- const ids = components.map((component) => component.id);
674
- const coreAspectsStringIds = this.aspectLoader.getCoreAspectIds();
675
- // TODO: @gilad it causes many issues we need to find a better solution. removed for now.
676
- // const coreAspectsComponentIds = coreAspectsStringIds.map((id) => ComponentID.fromString(id));
677
- // const aspectsIds = components.reduce((acc, curr) => {
678
- // const currIds = curr.state.aspects.ids;
679
- // acc = acc.concat(currIds);
680
- // return acc;
681
- // }, [] as any);
682
- // const otherDependenciesMap = components.reduce((acc, curr) => {
683
- // // const currIds = curr.state.dependencies.dependencies.map(dep => dep.id.toString());
684
- // const currMap = curr.state.dependencies.getIdsMap();
685
- // Object.assign(acc, currMap);
686
- // return acc;
687
- // }, {});
688
-
689
- // const depsWhichAreNotAspects = difference(Object.keys(otherDependenciesMap), aspectsIds);
690
- // const depsWhichAreNotAspectsBitIds = depsWhichAreNotAspects.map((strId) => otherDependenciesMap[strId]);
691
- // We only want to load into the graph components which are aspects and not regular dependencies
692
- // This come to solve a circular loop when an env aspect use an aspect (as regular dep) and the aspect use the env aspect as its env
693
- return this.workspace.buildOneGraphForComponents(ids, coreAspectsStringIds, isAspect);
694
- }
695
-
696
- /**
697
- * Load all unloaded extensions from an extension list
698
- * this will resolve the extensions from the scope aspects capsules if they are not in the ws
699
- * Only use it for component extensions
700
- * for workspace/scope root aspect use the load aspects directly
701
- *
702
- * The reason we are loading component extensions with "scope aspects capsules" is because for component extensions
703
- * we might have the same extension in multiple versions
704
- * (for example I might have 2 components using different versions of the same env)
705
- * in such case, I can't install both version into the root of the node_modules so I need to place it somewhere else (capsules)
706
- * @param extensions list of extensions with config to load
707
- */
708
- async loadComponentsExtensions(
709
- extensions: ExtensionDataList,
710
- originatedFrom?: ComponentID,
711
- opts: WorkspaceLoadAspectsOptions = {}
712
- ): Promise<void> {
713
- const defaultOpts: WorkspaceLoadAspectsOptions = {
714
- useScopeAspectsCapsule: true,
715
- throwOnError: false,
716
- hideMissingModuleError: !!this.workspace.inInstallContext,
717
- resolveEnvsFromRoots: this.resolveEnvsFromRoots,
718
- };
719
- const mergedOpts = { ...defaultOpts, ...opts };
720
- const extensionsIdsP = extensions.map(async (extensionEntry) => {
721
- // Core extension
722
- if (!extensionEntry.extensionId) {
723
- return extensionEntry.stringId as string;
724
- }
725
-
726
- const id = await this.workspace.resolveComponentId(extensionEntry.extensionId);
727
- // return this.resolveComponentId(extensionEntry.extensionId);
728
- return id.toString();
729
- });
730
- const extensionsIds: string[] = await Promise.all(extensionsIdsP);
731
- const harmonyExtensions = this.harmony.extensionsIds;
732
- const loadedExtensions = harmonyExtensions.filter((extId) => {
733
- return this.harmony.extensions.get(extId)?.loaded;
734
- });
735
- const extensionsToLoad = difference(extensionsIds, loadedExtensions);
736
- if (!extensionsToLoad.length) return;
737
- await this.loadAspects(extensionsToLoad, undefined, originatedFrom?.toString(), mergedOpts);
738
- }
739
-
740
- private async isAspect(id: ComponentID) {
741
- const component = await this.workspace.get(id);
742
- const isUsingAspectEnv = this.envs.isUsingAspectEnv(component);
743
- const isUsingEnvEnv = this.envs.isUsingEnvEnv(component);
744
- const isValidAspect = isUsingAspectEnv || isUsingEnvEnv;
745
- return isValidAspect;
746
- }
747
-
748
- /**
749
- * same as `this.importAndGetMany()` with a specific error handling of ComponentNotFound
750
- */
751
- private async importAndGetAspects(componentIds: ComponentID[]): Promise<Component[]> {
752
- try {
753
- // We don't want to load the seeders as aspects as it will cause an infinite loop
754
- // once you try to load the seeder it will try to load the workspace component
755
- // that will arrive here again and again
756
- const loadOpts: ComponentLoadOptions = {
757
- idsToNotLoadAsAspects: componentIds.map((id) => id.toString()),
758
- };
759
- return await this.workspace.importAndGetMany(componentIds, 'to load aspects from the workspace', loadOpts);
760
- } catch (err: any) {
761
- this.throwWsJsoncAspectNotFoundError(err);
762
-
763
- throw err;
764
- }
765
- }
766
-
767
- /**
768
- * split the provided components into 2 groups, one which are workspace components and the other which are not.
769
- * @param components
770
- * @returns
771
- */
772
- private async groupComponentsByWorkspaceExistence(
773
- components: Component[],
774
- resolveEnvsFromRoots?: boolean
775
- ): Promise<{ workspaceComps: Component[]; nonWorkspaceComps: Component[] }> {
776
- let workspaceComps: Component[] = [];
777
- let nonWorkspaceComps: Component[] = [];
778
- await Promise.all(
779
- components.map(async (component) => {
780
- const existOnWorkspace = await this.workspace.hasId(component.id);
781
- existOnWorkspace ? workspaceComps.push(component) : nonWorkspaceComps.push(component);
782
- })
783
- );
784
- if (resolveEnvsFromRoots) {
785
- const { rootComps, nonRootComps } = await this.groupComponentsByLoadFromRootComps(nonWorkspaceComps);
786
- workspaceComps = workspaceComps.concat(rootComps);
787
- nonWorkspaceComps = nonRootComps;
788
- }
789
- return { workspaceComps, nonWorkspaceComps };
790
- }
791
-
792
- private async groupComponentsByLoadFromRootComps(
793
- components: Component[]
794
- ): Promise<{ rootComps: Component[]; nonRootComps: Component[] }> {
795
- const rootComps: Component[] = [];
796
- const nonRootComps: Component[] = [];
797
- await Promise.all(
798
- components.map(async (component) => {
799
- const shouldLoadFromRootComps = await this.shouldLoadFromRootComps(component);
800
- if (shouldLoadFromRootComps) {
801
- rootComps.push(component);
802
- return;
803
- }
804
- nonRootComps.push(component);
805
- })
806
- );
807
- return { rootComps, nonRootComps };
808
- }
809
-
810
- private async shouldLoadFromRootComps(component: Component): Promise<boolean> {
811
- const rootDir = await this.workspace.getComponentPackagePath(component);
812
- const rootDirExist = await fs.pathExists(rootDir);
813
- const aspectFilePath = await this.aspectLoader.getAspectFilePath(component, rootDir);
814
- const aspectFilePathExist = aspectFilePath ? await fs.pathExists(aspectFilePath) : false;
815
- const pluginFiles = await this.aspectLoader.getPluginFiles(component, rootDir);
816
-
817
- // checking that we have the root dir (this means it's an aspect that needs to be loaded from there)
818
- // and validate that localPathExist so we can
819
- // really load the component from that path (if it's there it means that it's an env)
820
- if (rootDirExist && (aspectFilePathExist || pluginFiles.length)) {
821
- return true;
822
- }
823
- // If the component has env.jsonc we want to list it to be loaded from the root folder
824
- // even if it's not there yet
825
- // in that case we will fail to load it, and the user will need to run bit install
826
- if (this.envs.hasEnvManifest(component)) {
827
- return true;
828
- }
829
- return false;
830
- }
831
-
832
- /**
833
- * split the provided components into 2 groups, one which are workspace components and the other which are not.
834
- * @param components
835
- * @returns
836
- */
837
- private async groupAspectDefsByWorkspaceExistence(
838
- aspectDefs: AspectDefinition[]
839
- ): Promise<{ workspaceDefs: AspectDefinition[]; nonWorkspaceDefs: AspectDefinition[] }> {
840
- const workspaceDefs: AspectDefinition[] = [];
841
- const nonWorkspaceDefs: AspectDefinition[] = [];
842
- await Promise.all(
843
- aspectDefs.map(async (aspectDef) => {
844
- const id = aspectDef.component?.id;
845
- const existOnWorkspace = id ? await this.workspace.hasId(id) : true;
846
- if (existOnWorkspace) {
847
- workspaceDefs.push(aspectDef);
848
- return;
849
- }
850
- const shouldLoadFromRootComps = aspectDef.component
851
- ? await this.shouldLoadFromRootComps(aspectDef.component)
852
- : undefined;
853
- if (shouldLoadFromRootComps) {
854
- workspaceDefs.push(aspectDef);
855
- return;
856
- }
857
- nonWorkspaceDefs.push(aspectDef);
858
- })
859
- );
860
- return { workspaceDefs, nonWorkspaceDefs };
861
- }
862
-
863
- private async groupIdsByWorkspaceExistence(
864
- ids: ComponentID[],
865
- resolveEnvsFromRoots?: boolean
866
- ): Promise<{ workspaceIds: ComponentID[]; nonWorkspaceIds: ComponentID[] }> {
867
- let workspaceIds: ComponentID[] = [];
868
- let nonWorkspaceIds: ComponentID[] = [];
869
- await Promise.all(
870
- ids.map(async (id) => {
871
- const existOnWorkspace = await this.workspace.hasId(id);
872
- existOnWorkspace ? workspaceIds.push(id) : nonWorkspaceIds.push(id);
873
- })
874
- );
875
- // We need to bring the components in order to really group them with taking the root comps into account
876
- const scopeComponents = await this.importAndGetAspects(nonWorkspaceIds);
877
- const { nonWorkspaceComps, workspaceComps } = await this.groupComponentsByWorkspaceExistence(
878
- scopeComponents,
879
- resolveEnvsFromRoots
880
- );
881
- workspaceIds = workspaceIds.concat(workspaceComps.map((c) => c.id));
882
- nonWorkspaceIds = nonWorkspaceComps.map((c) => c.id);
883
- return { workspaceIds, nonWorkspaceIds };
884
- }
885
- }