@teambit/workspace 1.0.105 → 1.0.107

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.
@@ -4,6 +4,20 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.WorkspaceComponentLoader = void 0;
7
+ function _pMap() {
8
+ const data = _interopRequireDefault(require("p-map"));
9
+ _pMap = function () {
10
+ return data;
11
+ };
12
+ return data;
13
+ }
14
+ function _concurrency() {
15
+ const data = require("@teambit/legacy/dist/utils/concurrency");
16
+ _concurrency = function () {
17
+ return data;
18
+ };
19
+ return data;
20
+ }
7
21
  function _component() {
8
22
  const data = require("@teambit/component");
9
23
  _component = function () {
@@ -124,29 +138,390 @@ function _mergeConfigConflict() {
124
138
  return data;
125
139
  }
126
140
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
141
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
142
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
127
143
  function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
128
144
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); }
129
145
  function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
130
146
  class WorkspaceComponentLoader {
131
147
  // cache loaded components
132
- constructor(workspace, logger, dependencyResolver, envs) {
148
+ constructor(workspace, logger, dependencyResolver, envs, aspectLoader) {
133
149
  this.workspace = workspace;
134
150
  this.logger = logger;
135
151
  this.dependencyResolver = dependencyResolver;
136
152
  this.envs = envs;
153
+ this.aspectLoader = aspectLoader;
137
154
  _defineProperty(this, "componentsCache", void 0);
155
+ // cache loaded components
156
+ /**
157
+ * Cache components that loaded from scope (especially for get many for perf improvements)
158
+ */
159
+ _defineProperty(this, "scopeComponentsCache", void 0);
160
+ /**
161
+ * Cache extension list for components. used by get many for perf improvements.
162
+ * And to make sure we load extensions first.
163
+ */
164
+ _defineProperty(this, "componentsExtensionsCache", void 0);
165
+ _defineProperty(this, "componentLoadedSelfAsAspects", void 0);
138
166
  this.componentsCache = (0, _cacheFactory().createInMemoryCache)({
139
167
  maxSize: (0, _inMemoryCache().getMaxSizeForComponents)()
140
168
  });
169
+ this.scopeComponentsCache = (0, _cacheFactory().createInMemoryCache)({
170
+ maxSize: (0, _inMemoryCache().getMaxSizeForComponents)()
171
+ });
172
+ this.componentsExtensionsCache = (0, _cacheFactory().createInMemoryCache)({
173
+ maxSize: (0, _inMemoryCache().getMaxSizeForComponents)()
174
+ });
175
+ this.componentLoadedSelfAsAspects = (0, _cacheFactory().createInMemoryCache)({
176
+ maxSize: (0, _inMemoryCache().getMaxSizeForComponents)()
177
+ });
141
178
  }
142
179
  async getMany(ids, loadOpts, throwOnFailure = true) {
143
180
  const idsWithoutEmpty = (0, _lodash().compact)(ids);
144
- const errors = [];
181
+ this.logger.setStatusLine(`loading ${ids.length} component(s)`);
182
+ const loadOptsWithDefaults = Object.assign(
183
+ // We don't want to load extension or execute the load slot at this step
184
+ // we will do it later
185
+ // this important for better performance
186
+ // We don't want to resolveExtensionsVersions as with get many we call aspect merger merge before update dependencies
187
+ // so we will have the correct versions for extensions already and update them after will resolve wrong versions
188
+ // in some cases
189
+ {
190
+ loadExtensions: false,
191
+ executeLoadSlot: false,
192
+ loadSeedersAsAspects: true,
193
+ resolveExtensionsVersions: false
194
+ }, loadOpts || {});
195
+ const loadOrCached = {
196
+ idsToLoad: [],
197
+ fromCache: []
198
+ };
199
+ idsWithoutEmpty.forEach(id => {
200
+ const componentFromCache = this.getFromCache(id, loadOptsWithDefaults);
201
+ if (componentFromCache) {
202
+ loadOrCached.fromCache.push(componentFromCache);
203
+ } else {
204
+ loadOrCached.idsToLoad.push(id);
205
+ }
206
+ }, loadOrCached);
207
+ const {
208
+ components: loadedComponents,
209
+ invalidComponents
210
+ } = await this.getAndLoadSlotOrdered(loadOrCached.idsToLoad || [], loadOptsWithDefaults, throwOnFailure);
211
+ const components = [...loadedComponents, ...loadOrCached.fromCache];
212
+
213
+ // this.logger.clearStatusLine();
214
+ return {
215
+ components,
216
+ invalidComponents
217
+ };
218
+ }
219
+ async getAndLoadSlotOrdered(ids, loadOpts, throwOnFailure = true) {
220
+ if (!(ids !== null && ids !== void 0 && ids.length)) return {
221
+ components: [],
222
+ invalidComponents: []
223
+ };
224
+ const workspaceScopeIdsMap = await this.groupAndUpdateIds(ids);
225
+ const groupsToHandle = await this.buildLoadGroups(workspaceScopeIdsMap);
226
+ const groupsRes = (0, _lodash().compact)(await (0, _pMapSeries().default)(groupsToHandle, async group => {
227
+ const {
228
+ scopeIds,
229
+ workspaceIds,
230
+ aspects,
231
+ core,
232
+ seeders
233
+ } = group;
234
+ if (!workspaceIds.length && !scopeIds.length) return undefined;
235
+ const res = await this.getAndLoadSlot(workspaceIds, scopeIds, _objectSpread(_objectSpread({}, loadOpts), {}, {
236
+ core,
237
+ seeders,
238
+ aspects
239
+ }), throwOnFailure);
240
+ // We don't want to return components that were not asked originally (we do want to load them)
241
+ if (!group.seeders) return undefined;
242
+ return res;
243
+ }));
244
+ const finalRes = groupsRes.reduce((acc, curr) => {
245
+ return {
246
+ components: [...acc.components, ...curr.components],
247
+ invalidComponents: [...acc.invalidComponents, ...curr.invalidComponents]
248
+ };
249
+ }, {
250
+ components: [],
251
+ invalidComponents: []
252
+ });
253
+ return finalRes;
254
+ }
255
+ async buildLoadGroups(workspaceScopeIdsMap) {
256
+ const allIds = [...workspaceScopeIdsMap.workspaceIds.values(), ...workspaceScopeIdsMap.scopeIds.values()];
257
+ const groupedByIsCoreEnvs = (0, _lodash().groupBy)(allIds, id => {
258
+ return this.envs.isCoreEnv(id.toStringWithoutVersion());
259
+ });
260
+ const nonCoreEnvs = groupedByIsCoreEnvs.false || [];
261
+ await this.populateScopeAndExtensionsCache(nonCoreEnvs, workspaceScopeIdsMap);
262
+ const allExtIds = new Map();
263
+ nonCoreEnvs.forEach(id => {
264
+ const idStr = id.toString();
265
+ const fromCache = this.componentsExtensionsCache.get(idStr);
266
+ if (!fromCache || !fromCache.extensions) {
267
+ return;
268
+ }
269
+ fromCache.extensions.forEach(ext => {
270
+ if (!allExtIds.has(ext.stringId) && ext.newExtensionId) {
271
+ allExtIds.set(ext.stringId, ext.newExtensionId);
272
+ }
273
+ });
274
+ });
275
+ const allExtCompIds = Array.from(allExtIds.values());
276
+ await this.populateScopeAndExtensionsCache(allExtCompIds || [], workspaceScopeIdsMap);
277
+ const allExtIdsStr = allExtCompIds.map(id => id.toString());
278
+ const groupedByIsExtOfAnother = (0, _lodash().groupBy)(nonCoreEnvs, id => {
279
+ return allExtIdsStr.includes(id.toString());
280
+ });
281
+ const extIdsFromTheList = (groupedByIsExtOfAnother.true || []).map(id => id.toString());
282
+ const extsNotFromTheList = [];
283
+ for (const [, id] of allExtIds.entries()) {
284
+ if (!extIdsFromTheList.includes(id.toString())) {
285
+ extsNotFromTheList.push(id);
286
+ }
287
+ }
288
+ await this.groupAndUpdateIds(extsNotFromTheList, workspaceScopeIdsMap);
289
+ const layerdExtFromTheList = this.regroupExtIdsFromTheList(groupedByIsExtOfAnother.true);
290
+ const layerdExtGroups = layerdExtFromTheList.map(ids => {
291
+ return {
292
+ ids,
293
+ core: false,
294
+ aspects: true,
295
+ seeders: true
296
+ };
297
+ });
298
+ const groupsToHandle = [
299
+ // Always load first core envs
300
+ {
301
+ ids: groupedByIsCoreEnvs.true || [],
302
+ core: true,
303
+ aspects: true,
304
+ seeders: true
305
+ }, {
306
+ ids: extsNotFromTheList || [],
307
+ core: false,
308
+ aspects: true,
309
+ seeders: false
310
+ }, ...layerdExtGroups, {
311
+ ids: groupedByIsExtOfAnother.false || [],
312
+ core: false,
313
+ aspects: false,
314
+ seeders: true
315
+ }];
316
+ const groupsByWsScope = groupsToHandle.map(group => {
317
+ const groupedByWsScope = (0, _lodash().groupBy)(group.ids, id => {
318
+ return workspaceScopeIdsMap.workspaceIds.has(id.toString());
319
+ });
320
+ return {
321
+ workspaceIds: groupedByWsScope.true || [],
322
+ scopeIds: groupedByWsScope.false || [],
323
+ core: group.core,
324
+ aspects: group.aspects,
325
+ seeders: group.seeders
326
+ };
327
+ });
328
+ return groupsByWsScope;
329
+ }
330
+ regroupExtIdsFromTheList(ids) {
331
+ // TODO: implement this function
332
+ // this should handle a case when you have:
333
+ // compA that has extA and that extA has extB
334
+ // in that case we now get the following group:
335
+ // ids: [extA, extB]
336
+ // while we need extB to be in a different group before extA
337
+ return [ids];
338
+ }
339
+ async getAndLoadSlot(workspaceIds, scopeIds, loadOpts, throwOnFailure = true) {
340
+ const {
341
+ workspaceComponents,
342
+ scopeComponents,
343
+ invalidComponents
344
+ } = await this.getComponentsWithoutLoadExtensions(workspaceIds, scopeIds, loadOpts, throwOnFailure);
345
+ const components = workspaceComponents.concat(scopeComponents);
346
+ const allExtensions = components.map(component => {
347
+ return component.state._consumer.extensions;
348
+ });
349
+
350
+ // Ensure we won't load the same extension many times
351
+ // We don't want to ignore version here, as we do want to load different extensions with same id but different versions here
352
+ const mergedExtensions = _config().ExtensionDataList.mergeConfigs(allExtensions, false);
353
+ await this.workspace.loadComponentsExtensions(mergedExtensions);
354
+ let wsComponentsWithAspects = workspaceComponents;
355
+ // if (loadOpts.seeders) {
356
+ wsComponentsWithAspects = await (0, _pMap().default)(workspaceComponents, component => this.executeLoadSlot(component), {
357
+ concurrency: (0, _concurrency().concurrentComponentsLimit)()
358
+ });
359
+ await this.warnAboutMisconfiguredEnvs(wsComponentsWithAspects);
360
+ // }
361
+
362
+ const withAspects = wsComponentsWithAspects.concat(scopeComponents);
363
+
364
+ // It's important to load the workspace components as aspects here
365
+ // otherwise the envs from the workspace won't be loaded at time
366
+ // so we will get wrong dependencies from component who uses envs from the workspace
367
+ if (loadOpts.loadSeedersAsAspects || loadOpts.core && loadOpts.aspects) {
368
+ await this.loadCompsAsAspects(workspaceComponents.concat(scopeComponents), {
369
+ loadApps: true,
370
+ loadEnvs: true,
371
+ loadAspects: loadOpts.aspects,
372
+ core: loadOpts.core,
373
+ seeders: loadOpts.seeders,
374
+ idsToNotLoadAsAspects: loadOpts.idsToNotLoadAsAspects
375
+ });
376
+ }
377
+ return {
378
+ components: withAspects,
379
+ invalidComponents
380
+ };
381
+ }
382
+
383
+ // TODO: this is similar to scope.main.runtime loadCompAspects func, we should merge them.
384
+ async loadCompsAsAspects(components, opts = {
385
+ loadApps: true,
386
+ loadEnvs: true,
387
+ loadAspects: true
388
+ }) {
389
+ const aspectIds = [];
390
+ components.forEach(component => {
391
+ var _opts$idsToNotLoadAsA, _appData$data, _envsData$data, _envsData$data2, _envsData$data3, _envsData$data4;
392
+ const firstTimeToLoad = this.componentLoadedSelfAsAspects.get(component.id.toString()) === undefined;
393
+ const excluded = (_opts$idsToNotLoadAsA = opts.idsToNotLoadAsAspects) === null || _opts$idsToNotLoadAsA === void 0 ? void 0 : _opts$idsToNotLoadAsA.includes(component.id.toString());
394
+ const isCore = this.aspectLoader.isCoreAspect(component.id.toStringWithoutVersion());
395
+ const alreadyLoaded = this.aspectLoader.isAspectLoaded(component.id.toString());
396
+ const skipLoading = excluded || isCore || alreadyLoaded || !firstTimeToLoad;
397
+ if (skipLoading) {
398
+ return;
399
+ }
400
+ const idStr = component.id.toString();
401
+ const appData = component.state.aspects.get('teambit.harmony/application');
402
+ if (opts.loadApps && appData !== null && appData !== void 0 && (_appData$data = appData.data) !== null && _appData$data !== void 0 && _appData$data.appName) {
403
+ aspectIds.push(idStr);
404
+ this.componentLoadedSelfAsAspects.set(idStr, true);
405
+ }
406
+ const envsData = component.state.aspects.get(_envs().EnvsAspect.id);
407
+ if (opts.loadEnvs && (envsData !== null && envsData !== void 0 && (_envsData$data = envsData.data) !== null && _envsData$data !== void 0 && _envsData$data.services || envsData !== null && envsData !== void 0 && (_envsData$data2 = envsData.data) !== null && _envsData$data2 !== void 0 && _envsData$data2.self || (envsData === null || envsData === void 0 || (_envsData$data3 = envsData.data) === null || _envsData$data3 === void 0 ? void 0 : _envsData$data3.type) === 'env')) {
408
+ aspectIds.push(idStr);
409
+ this.componentLoadedSelfAsAspects.set(idStr, true);
410
+ }
411
+ if (opts.loadAspects && (envsData === null || envsData === void 0 || (_envsData$data4 = envsData.data) === null || _envsData$data4 === void 0 ? void 0 : _envsData$data4.type) === 'aspect') {
412
+ aspectIds.push(idStr);
413
+ this.componentLoadedSelfAsAspects.set(idStr, true);
414
+ }
415
+ });
416
+ if (!aspectIds.length) return;
417
+ try {
418
+ await this.workspace.loadAspects(aspectIds, true, 'self loading aspects', {});
419
+ } catch (err) {
420
+ this.logger.warn(`failed loading components as aspects for components ${aspectIds.join(', ')}`, err);
421
+ // we ignore that errors at the moment
422
+ }
423
+ }
424
+ async populateScopeAndExtensionsCache(ids, workspaceScopeIdsMap) {
425
+ return (0, _pMapSeries().default)(ids, async id => {
426
+ const idStr = id.toString();
427
+ let componentFromScope;
428
+ if (!this.scopeComponentsCache.has(idStr)) {
429
+ try {
430
+ // Do not import automatically if it's missing, it will throw an error later
431
+ componentFromScope = await this.workspace.scope.get(id, undefined, false);
432
+ if (componentFromScope) {
433
+ this.scopeComponentsCache.set(idStr, componentFromScope);
434
+ }
435
+ // This is fine here, as it will be handled later in the process
436
+ } catch (err) {
437
+ const wsAspectLoader = this.workspace.getWorkspaceAspectsLoader();
438
+ wsAspectLoader.throwWsJsoncAspectNotFoundError(err);
439
+ this.logger.warn(`populateScopeAndExtensionsCache - failed loading component ${idStr} from scope`, err);
440
+ }
441
+ }
442
+ if (!this.componentsExtensionsCache.has(idStr) && workspaceScopeIdsMap.workspaceIds.has(idStr)) {
443
+ componentFromScope = componentFromScope || this.scopeComponentsCache.get(idStr);
444
+ const {
445
+ extensions,
446
+ errors
447
+ } = await this.workspace.componentExtensions(id, componentFromScope, undefined, {
448
+ loadExtensions: false
449
+ });
450
+ this.componentsExtensionsCache.set(idStr, {
451
+ extensions,
452
+ errors
453
+ });
454
+ }
455
+ });
456
+ }
457
+ async warnAboutMisconfiguredEnvs(components) {
458
+ const allIds = (0, _lodash().uniq)(components.map(component => this.envs.getEnvId(component)));
459
+ return Promise.all(allIds.map(envId => this.workspace.warnAboutMisconfiguredEnv(envId)));
460
+ }
461
+ async groupAndUpdateIds(ids, existingGroups) {
462
+ const result = existingGroups || {
463
+ scopeIds: new Map(),
464
+ workspaceIds: new Map()
465
+ };
466
+ await Promise.all(ids.map(async componentId => {
467
+ const inWs = await this.isInWsIncludeDeleted(componentId);
468
+ if (!inWs) {
469
+ result.scopeIds.set(componentId.toString(), componentId);
470
+ return undefined;
471
+ }
472
+ const resolvedVersions = this.resolveVersion(componentId);
473
+ result.workspaceIds.set(resolvedVersions.toString(), resolvedVersions);
474
+ return undefined;
475
+ }));
476
+ return result;
477
+ }
478
+ async isInWsIncludeDeleted(componentId) {
479
+ const nonDeletedWsIds = await this.workspace.listIds();
480
+ const deletedWsIds = await this.workspace.locallyDeletedIds();
481
+ const allWsIds = nonDeletedWsIds.concat(deletedWsIds);
482
+ const inWs = allWsIds.find(id => id.isEqual(componentId, {
483
+ ignoreVersion: !componentId.hasVersion()
484
+ }));
485
+ return !!inWs;
486
+ }
487
+ async getComponentsWithoutLoadExtensions(workspaceIds, scopeIds, loadOpts, throwOnFailure = true) {
145
488
  const invalidComponents = [];
146
- const longProcessLogger = this.logger.createLongProcessLogger('loading components', ids.length);
147
- const componentsP = (0, _pMapSeries().default)(idsWithoutEmpty, async id => {
148
- longProcessLogger.logProgress(id.toString());
149
- return this.get(id, undefined, undefined, undefined, loadOpts).catch(err => {
489
+ const errors = [];
490
+ const loadOptsWithDefaults = Object.assign(
491
+ // We don't want to load extension or execute the load slot at this step
492
+ // we will do it later
493
+ // this important for better performance
494
+ // We don't want to store deps in fs cache, as at this point extensions are not loaded yet
495
+ // so it might save a wrong deps into the cache
496
+ {
497
+ loadExtensions: false,
498
+ executeLoadSlot: false
499
+ }, loadOpts || {});
500
+ const idsIndex = {};
501
+ workspaceIds.forEach(id => {
502
+ idsIndex[id.toString()] = id;
503
+ });
504
+ const {
505
+ components: legacyComponents,
506
+ invalidComponents: legacyInvalidComponents,
507
+ removedComponents
508
+ } = await this.workspace.consumer.loadComponents(_componentId().ComponentIdList.fromArray(workspaceIds), false, loadOptsWithDefaults);
509
+ const allLegacyComponents = legacyComponents.concat(removedComponents);
510
+ legacyInvalidComponents.forEach(invalidComponent => {
511
+ const entry = {
512
+ id: idsIndex[invalidComponent.id.toString()],
513
+ err: invalidComponent.error
514
+ };
515
+ if (_component2().default.isComponentInvalidByErrorType(invalidComponent.error)) {
516
+ if (throwOnFailure) throw invalidComponent.error;
517
+ invalidComponents.push(entry);
518
+ }
519
+ if (this.isComponentNotExistsError(invalidComponent.error) || invalidComponent.error instanceof _componentNotFoundInPath().default) {
520
+ errors.push(entry);
521
+ }
522
+ });
523
+ const getWithCatch = (id, legacyComponent) => {
524
+ return this.get(id, legacyComponent, undefined, undefined, loadOptsWithDefaults).catch(err => {
150
525
  if (_component2().default.isComponentInvalidByErrorType(err) && !throwOnFailure) {
151
526
  invalidComponents.push({
152
527
  id,
@@ -163,19 +538,49 @@ class WorkspaceComponentLoader {
163
538
  }
164
539
  throw err;
165
540
  });
541
+ };
542
+
543
+ // await this.getConsumerComponent(id, loadOpts)
544
+ const componentsP = (0, _pMap().default)(allLegacyComponents, legacyComponent => {
545
+ // const componentsP = Promise.all(
546
+ // allLegacyComponents.map(async (legacyComponent) => {
547
+ let id = idsIndex[legacyComponent.id.toString()];
548
+ if (!id) {
549
+ const withoutVersion = idsIndex[legacyComponent.id.toStringWithoutVersion()] || legacyComponent.id;
550
+ if (withoutVersion) {
551
+ id = withoutVersion.changeVersion(legacyComponent.id.version);
552
+ idsIndex[legacyComponent.id.toString()] = id;
553
+ }
554
+ }
555
+ return getWithCatch(id, legacyComponent);
556
+ }, {
557
+ concurrency: (0, _concurrency().concurrentComponentsLimit)()
166
558
  });
167
- const components = await componentsP;
168
559
  errors.forEach(err => {
169
560
  this.logger.console(`failed loading component ${err.id.toString()}, see full error in debug.log file`);
170
561
  this.logger.warn(`failed loading component ${err.id.toString()}`, err.err);
171
562
  });
172
- // remove errored components
173
- const filteredComponents = (0, _lodash().compact)(components);
174
- longProcessLogger.end();
175
- return {
176
- components: filteredComponents,
177
- invalidComponents
178
- };
563
+ const components = (0, _lodash().compact)(await componentsP);
564
+
565
+ // Here we need to load many, otherwise we will get wrong overrides dependencies data
566
+ // as when loading the next batch of components (next group) we won't have the envs loaded
567
+
568
+ try {
569
+ // We don't want to load envs as part of this step, they will be loaded later
570
+ const scopeComponents = await this.workspace.scope.loadMany(scopeIds, undefined, {
571
+ loadApps: false,
572
+ loadEnvs: false
573
+ });
574
+ return {
575
+ workspaceComponents: components,
576
+ scopeComponents,
577
+ invalidComponents
578
+ };
579
+ } catch (err) {
580
+ const wsAspectLoader = this.workspace.getWorkspaceAspectsLoader();
581
+ wsAspectLoader.throwWsJsoncAspectNotFoundError(err);
582
+ throw err;
583
+ }
179
584
  }
180
585
  async getInvalid(ids) {
181
586
  const idsWithoutEmpty = (0, _lodash().compact)(ids);
@@ -198,20 +603,30 @@ class WorkspaceComponentLoader {
198
603
  });
199
604
  return errors;
200
605
  }
201
- async get(componentId, legacyComponent, useCache = true, storeInCache = true, loadOpts) {
202
- const bitIdWithVersion = (0, _utils().getLatestVersionNumber)(this.workspace.consumer.bitmapIdsFromCurrentLaneIncludeRemoved, componentId);
203
- const id = bitIdWithVersion.version ? componentId.changeVersion(bitIdWithVersion.version) : componentId;
204
- const fromCache = this.getFromCache(id, loadOpts);
606
+ async get(componentId, legacyComponent, useCache = true, storeInCache = true, loadOpts, getOpts = {
607
+ resolveIdVersion: true
608
+ }) {
609
+ const loadOptsWithDefaults = Object.assign({
610
+ loadExtensions: true,
611
+ executeLoadSlot: true
612
+ }, loadOpts || {});
613
+ const id = getOpts !== null && getOpts !== void 0 && getOpts.resolveIdVersion ? this.resolveVersion(componentId) : componentId;
614
+ const fromCache = this.getFromCache(componentId, loadOptsWithDefaults);
205
615
  if (fromCache && useCache) {
206
616
  return fromCache;
207
617
  }
208
- const consumerComponent = legacyComponent || (await this.getConsumerComponent(id, loadOpts));
618
+ let consumerComponent = legacyComponent;
619
+ const inWs = await this.isInWsIncludeDeleted(componentId);
620
+ if (inWs && !consumerComponent) {
621
+ consumerComponent = await this.getConsumerComponent(id, loadOptsWithDefaults);
622
+ }
623
+
209
624
  // in case of out-of-sync, the id may changed during the load process
210
625
  const updatedId = consumerComponent ? consumerComponent.id : id;
211
- const component = await this.loadOne(updatedId, consumerComponent, loadOpts);
626
+ const component = await this.loadOne(updatedId, consumerComponent, loadOptsWithDefaults);
212
627
  if (storeInCache) {
213
628
  this.addMultipleEnvsIssueIfNeeded(component); // it's in storeInCache block, otherwise, it wasn't fully loaded
214
- this.saveInCache(component, loadOpts);
629
+ this.saveInCache(component, loadOptsWithDefaults);
215
630
  }
216
631
  return component;
217
632
  }
@@ -225,6 +640,11 @@ class WorkspaceComponentLoader {
225
640
  throw err;
226
641
  }
227
642
  }
643
+ resolveVersion(componentId) {
644
+ const bitIdWithVersion = (0, _utils().getLatestVersionNumber)(this.workspace.consumer.bitmapIdsFromCurrentLaneIncludeRemoved, componentId);
645
+ const id = bitIdWithVersion.version ? componentId.changeVersion(bitIdWithVersion.version) : componentId;
646
+ return id;
647
+ }
228
648
  addMultipleEnvsIssueIfNeeded(component) {
229
649
  const envs = this.envs.getAllEnvsConfiguredOnComponent(component);
230
650
  const envIds = (0, _lodash().uniq)(envs.map(env => env.id));
@@ -235,25 +655,35 @@ class WorkspaceComponentLoader {
235
655
  }
236
656
  clearCache() {
237
657
  this.componentsCache.deleteAll();
658
+ this.scopeComponentsCache.deleteAll();
659
+ this.componentsExtensionsCache.deleteAll();
660
+ this.componentLoadedSelfAsAspects.deleteAll();
238
661
  }
239
662
  clearComponentCache(id) {
240
663
  const idStr = id.toString();
241
- for (const cacheKey of this.componentsCache.keys()) {
242
- if (cacheKey === idStr || cacheKey.startsWith(`${idStr}:`)) {
243
- this.componentsCache.delete(cacheKey);
664
+ const cachesToClear = [this.componentsCache, this.scopeComponentsCache, this.componentsExtensionsCache, this.componentLoadedSelfAsAspects];
665
+ cachesToClear.forEach(cache => {
666
+ for (const cacheKey of cache.keys()) {
667
+ if (cacheKey === idStr || cacheKey.startsWith(`${idStr}:`)) {
668
+ cache.delete(cacheKey);
669
+ }
244
670
  }
245
- }
671
+ });
246
672
  }
247
673
  async loadOne(id, consumerComponent, loadOpts) {
248
- const componentFromScope = await this.workspace.scope.get(id);
674
+ const idStr = id.toString();
675
+ const componentFromScope = this.scopeComponentsCache.has(idStr) ? this.scopeComponentsCache.get(idStr) : await this.workspace.scope.get(id);
249
676
  if (!consumerComponent) {
250
677
  if (!componentFromScope) throw new (_exceptions().MissingBitMapComponent)(id.toString());
251
678
  return componentFromScope;
252
679
  }
680
+ const extErrorsFromCache = this.componentsExtensionsCache.has(idStr) ? this.componentsExtensionsCache.get(idStr) : undefined;
253
681
  const {
254
682
  extensions,
255
683
  errors
256
- } = await this.workspace.componentExtensions(id, componentFromScope);
684
+ } = extErrorsFromCache || (await this.workspace.componentExtensions(id, componentFromScope, undefined, {
685
+ loadExtensions: loadOpts === null || loadOpts === void 0 ? void 0 : loadOpts.loadExtensions
686
+ }));
257
687
  if (errors !== null && errors !== void 0 && errors.some(err => err instanceof _mergeConfigConflict().MergeConfigConflict)) {
258
688
  consumerComponent.issues.getOrCreate(_componentIssues().IssuesClasses.MergeConfigHasConflict).data = true;
259
689
  }
@@ -267,10 +697,17 @@ class WorkspaceComponentLoader {
267
697
  // componentFromScope.state = state;
268
698
  // const workspaceComponent = WorkspaceComponent.fromComponent(componentFromScope, this.workspace);
269
699
  const workspaceComponent = new (_workspaceComponent().WorkspaceComponent)(componentFromScope.id, componentFromScope.head, state, componentFromScope.tags, this.workspace);
270
- const updatedComp = await this.executeLoadSlot(workspaceComponent, loadOpts);
271
- return updatedComp;
700
+ if (loadOpts !== null && loadOpts !== void 0 && loadOpts.executeLoadSlot) {
701
+ return this.executeLoadSlot(workspaceComponent, loadOpts);
702
+ }
703
+ // const updatedComp = await this.executeLoadSlot(workspaceComponent, loadOpts);
704
+ return workspaceComponent;
272
705
  }
273
- return this.executeLoadSlot(this.newComponentFromState(id, state), loadOpts);
706
+ const newComponent = this.newComponentFromState(id, state);
707
+ if (!(loadOpts !== null && loadOpts !== void 0 && loadOpts.executeLoadSlot)) {
708
+ return newComponent;
709
+ }
710
+ return this.executeLoadSlot(newComponent, loadOpts);
274
711
  }
275
712
  saveInCache(component, loadOpts) {
276
713
  const cacheKey = createComponentCacheKey(component.id, loadOpts);
@@ -284,9 +721,19 @@ class WorkspaceComponentLoader {
284
721
  * as a result, when out-of-sync is happening and the id is changed to include scope-name in the
285
722
  * legacy-id, the component is the cache has the old id.
286
723
  */
287
- getFromCache(id, loadOpts) {
724
+ getFromCache(componentId, loadOpts) {
725
+ const bitIdWithVersion = this.resolveVersion(componentId);
726
+ const id = bitIdWithVersion.version ? componentId.changeVersion(bitIdWithVersion.version) : componentId;
288
727
  const cacheKey = createComponentCacheKey(id, loadOpts);
289
- const fromCache = this.componentsCache.get(cacheKey);
728
+ // If we try to look for the cache without load extensions/ without execute load slot
729
+ // but there is an entry after the load, we want to use it as well.
730
+ // as we want the component, so if we already loaded it with everything, it's fine.
731
+ // this sometime relevant for cases with tiny cache size (during tag)
732
+ const cacheKeyWithTrueLoadOpts = createComponentCacheKey(id, {
733
+ loadExtensions: true,
734
+ executeLoadSlot: true
735
+ });
736
+ const fromCache = this.componentsCache.get(cacheKey) || this.componentsCache.get(cacheKeyWithTrueLoadOpts);
290
737
  if (fromCache && fromCache.id.isEqual(id)) {
291
738
  return fromCache;
292
739
  }
@@ -372,7 +819,8 @@ class WorkspaceComponentLoader {
372
819
  }
373
820
  exports.WorkspaceComponentLoader = WorkspaceComponentLoader;
374
821
  function createComponentCacheKey(id, loadOpts) {
375
- return `${id.toString()}:${JSON.stringify(sortKeys(loadOpts ?? {}))}`;
822
+ const relevantOpts = (0, _lodash().pick)(loadOpts, ['loadExtensions', 'executeLoadSlot']);
823
+ return `${id.toString()}:${JSON.stringify(sortKeys(relevantOpts ?? {}))}`;
376
824
  }
377
825
  function sortKeys(obj) {
378
826
  return (0, _lodash().fromPairs)(Object.entries(obj).sort(([k1], [k2]) => k1.localeCompare(k2)));