@teambit/workspace 0.0.646 → 0.0.650

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.
package/dist/workspace.js CHANGED
@@ -606,9 +606,10 @@ class Workspace {
606
606
  }
607
607
 
608
608
  validateConfig() {
609
- const defaultScope = this.config.defaultScope;
610
609
  if (this.consumer.isLegacy) return;
611
- if (!defaultScope) throw new Error('defaultScope is missing');
610
+ if ((0, _lodash().isEmpty)(this.config)) throw new (_bitError().BitError)(`fatal: workspace config is empty. probably one of bit files is missing. consider running "bit init"`);
611
+ const defaultScope = this.config.defaultScope;
612
+ if (!defaultScope) throw new (_bitError().BitError)('defaultScope is missing');
612
613
  if (!(0, _legacyBitId().isValidScopeName)(defaultScope)) throw new (_legacyBitId().InvalidScopeName)(defaultScope);
613
614
  }
614
615
  /**
@@ -852,12 +853,14 @@ class Workspace {
852
853
  }
853
854
 
854
855
  async createAspectList(extensionDataList) {
855
- const entiresP = extensionDataList.map(async entry => {
856
- return new (_component().AspectEntry)(await this.resolveComponentId(entry.id), entry);
857
- });
856
+ const entiresP = extensionDataList.map(entry => this.extensionDataEntryToAspectEntry(entry));
858
857
  const entries = await Promise.all(entiresP);
859
858
  return this.componentAspect.createAspectListFromEntries(entries);
860
859
  }
860
+
861
+ async extensionDataEntryToAspectEntry(dataEntry) {
862
+ return new (_component().AspectEntry)(await this.resolveComponentId(dataEntry.id), dataEntry);
863
+ }
861
864
  /**
862
865
  * get a component from workspace
863
866
  * @param id component ID
@@ -879,7 +882,7 @@ class Workspace {
879
882
  try {
880
883
  this.componentLoadedSelfAsAspects.set(component.id.toString(), true);
881
884
  this.logger.debug(`trying to load self as aspect with id ${component.id.toString()}`);
882
- await this.loadAspects([component.id.toString()]); // In most cases if the load self as aspect failed we don't care about it.
885
+ await this.loadAspects([component.id.toString()], undefined, component.id); // In most cases if the load self as aspect failed we don't care about it.
883
886
  // we only need it in specific cases to work, but this workspace.get runs on different
884
887
  // cases where it might fail (like when importing aspect, after the import objects
885
888
  // when we write the package.json we run the applyTransformers which get to pkg which call
@@ -1067,14 +1070,18 @@ class Workspace {
1067
1070
  const componentDir = this.componentDir(id, {
1068
1071
  ignoreVersion: true
1069
1072
  });
1070
- const componentConfigFile = new (_componentConfigFile().ComponentConfigFile)(componentId, aspects, options.propagate);
1071
- await componentConfigFile.write(componentDir, {
1073
+ const componentConfigFile = new (_componentConfigFile().ComponentConfigFile)(componentId, aspects, componentDir, options.propagate);
1074
+ await componentConfigFile.write({
1072
1075
  override: options.override
1073
1076
  });
1074
1077
  return {
1075
1078
  configPath: _componentConfigFile().ComponentConfigFile.composePath(componentDir)
1076
1079
  };
1077
1080
  }
1081
+ /**
1082
+ * see component-aspect, createAspectListFromLegacy() method for a context why this is needed.
1083
+ */
1084
+
1078
1085
 
1079
1086
  async resolveScopeAspectListIds(aspectListFromScope) {
1080
1087
  const resolvedList = await aspectListFromScope.pmap(async entry => {
@@ -1111,11 +1118,13 @@ class Workspace {
1111
1118
  */
1112
1119
 
1113
1120
 
1114
- async idsByPattern(pattern) {
1121
+ async idsByPattern(pattern, throwForNoMatch = true) {
1115
1122
  if (!pattern.includes('*') && !pattern.includes(',')) {
1116
1123
  // if it's not a pattern but just id, resolve it without multimatch to support specifying id without scope-name
1117
1124
  const id = await this.resolveComponentId(pattern);
1118
- return [id];
1125
+ if (this.exists(id)) return [id];
1126
+ if (throwForNoMatch) throw new (_bitError().BitError)(`unable to find "${pattern}" in the workspace`);
1127
+ return [];
1119
1128
  }
1120
1129
 
1121
1130
  const ids = await this.listIds();
@@ -1123,7 +1132,13 @@ class Workspace {
1123
1132
 
1124
1133
  const idsToCheck = id => [id.toStringWithoutVersion(), id._legacy.toStringWithoutVersion()];
1125
1134
 
1126
- return ids.filter(id => (0, _multimatch().default)(idsToCheck(id), patterns).length);
1135
+ const idsFiltered = ids.filter(id => (0, _multimatch().default)(idsToCheck(id), patterns).length);
1136
+
1137
+ if (throwForNoMatch && !idsFiltered.length) {
1138
+ throw new (_bitError().BitError)(`unable to find any matching for "${pattern}" pattern`);
1139
+ }
1140
+
1141
+ return idsFiltered;
1127
1142
  }
1128
1143
  /**
1129
1144
  * useful for workspace commands, such as `bit build`, `bit compile`.
@@ -1155,6 +1170,20 @@ class Workspace {
1155
1170
  return newAndModified;
1156
1171
  }
1157
1172
 
1173
+ async getComponentsUsingEnv(env, throwIfNotFound = false) {
1174
+ const allComps = await this.list();
1175
+ const allEnvs = await this.envs.createEnvironment(allComps);
1176
+ const foundEnv = allEnvs.runtimeEnvs.find(runtimeEnv => runtimeEnv.id === env);
1177
+
1178
+ if (!foundEnv && throwIfNotFound) {
1179
+ const availableEnvs = allEnvs.runtimeEnvs.map(runtimeEnv => runtimeEnv.id);
1180
+ throw new (_bitError().BitError)(`unable to find components that using "${env}" env.
1181
+ the following envs are used in this workspace: ${availableEnvs.join(', ')}`);
1182
+ }
1183
+
1184
+ return (foundEnv === null || foundEnv === void 0 ? void 0 : foundEnv.components) || [];
1185
+ }
1186
+
1158
1187
  async getMany(ids, forCapsule = false) {
1159
1188
  return this.componentLoader.getMany(ids, forCapsule);
1160
1189
  }
@@ -1446,7 +1475,7 @@ class Workspace {
1446
1475
  const extsWithoutRemoved = extensions.filterRemovedExtensions();
1447
1476
  const selfInMergedExtensions = extsWithoutRemoved.findExtension(componentId._legacy.toStringWithoutScopeAndVersion(), true, true);
1448
1477
  const extsWithoutSelf = selfInMergedExtensions !== null && selfInMergedExtensions !== void 0 && selfInMergedExtensions.extensionId ? extsWithoutRemoved.remove(selfInMergedExtensions.extensionId) : extsWithoutRemoved;
1449
- await this.loadExtensions(extsWithoutSelf);
1478
+ await this.loadExtensions(extsWithoutSelf, componentId);
1450
1479
  const {
1451
1480
  extensionDataListFiltered,
1452
1481
  envIsCurrentlySet
@@ -1556,6 +1585,46 @@ class Workspace {
1556
1585
  await func(components, watchOpts);
1557
1586
  });
1558
1587
  }
1588
+
1589
+ async addSpecificComponentConfig(id, aspectId, config = {}) {
1590
+ const componentConfigFile = await this.componentConfigFile(id);
1591
+
1592
+ if (componentConfigFile) {
1593
+ await componentConfigFile.addAspect(aspectId, config, this.resolveComponentId.bind(this));
1594
+ await componentConfigFile.write({
1595
+ override: true
1596
+ });
1597
+ } else {
1598
+ this.bitMap.addComponentConfig(id, aspectId, config);
1599
+ }
1600
+ }
1601
+
1602
+ async removeSpecificComponentConfig(id, aspectId, markWithMinusIfNotExist) {
1603
+ const componentConfigFile = await this.componentConfigFile(id);
1604
+
1605
+ if (componentConfigFile) {
1606
+ await componentConfigFile.removeAspect(aspectId, markWithMinusIfNotExist, this.resolveComponentId.bind(this));
1607
+ await componentConfigFile.write({
1608
+ override: true
1609
+ });
1610
+ } else {
1611
+ this.bitMap.removeComponentConfig(id, aspectId, markWithMinusIfNotExist);
1612
+ }
1613
+ }
1614
+
1615
+ async getSpecificComponentConfig(id, aspectId) {
1616
+ var _this$bitMap$getBitma;
1617
+
1618
+ const componentConfigFile = await this.componentConfigFile(id);
1619
+
1620
+ if (componentConfigFile) {
1621
+ var _componentConfigFile$2;
1622
+
1623
+ return (_componentConfigFile$2 = componentConfigFile.aspects.get(aspectId)) === null || _componentConfigFile$2 === void 0 ? void 0 : _componentConfigFile$2.config;
1624
+ }
1625
+
1626
+ return (_this$bitMap$getBitma = this.bitMap.getBitmapEntry(id).config) === null || _this$bitMap$getBitma === void 0 ? void 0 : _this$bitMap$getBitma[aspectId];
1627
+ }
1559
1628
  /**
1560
1629
  * This will mutate the entries with extensionId prop to have resolved legacy id
1561
1630
  * This should be worked on the extension data list not the new aspect list
@@ -1647,8 +1716,10 @@ class Workspace {
1647
1716
  */
1648
1717
 
1649
1718
 
1650
- async loadAspects(ids = [], throwOnError = false) {
1651
- this.logger.debug(`loading ${ids.length} aspects`);
1719
+ async loadAspects(ids = [], throwOnError = false, neededFor) {
1720
+ this.logger.info(`loadAspects, loading ${ids.length} aspects.
1721
+ ids: ${ids.join(', ')}
1722
+ needed-for: ${(neededFor === null || neededFor === void 0 ? void 0 : neededFor.toString()) || '<unknown>'}`);
1652
1723
  const notLoadedIds = ids.filter(id => !this.aspectLoader.isAspectLoaded(id));
1653
1724
  if (!notLoadedIds.length) return [];
1654
1725
  const coreAspectsStringIds = this.aspectLoader.getCoreAspectIds();
@@ -1808,7 +1879,7 @@ class Workspace {
1808
1879
  */
1809
1880
 
1810
1881
 
1811
- async loadExtensions(extensions, throwOnError = false) {
1882
+ async loadExtensions(extensions, originatedFrom, throwOnError = false) {
1812
1883
  const extensionsIdsP = extensions.map(async extensionEntry => {
1813
1884
  // Core extension
1814
1885
  if (!extensionEntry.extensionId) {
@@ -1823,7 +1894,7 @@ class Workspace {
1823
1894
  const loadedExtensions = this.harmony.extensionsIds;
1824
1895
  const extensionsToLoad = (0, _lodash().difference)(extensionsIds, loadedExtensions);
1825
1896
  if (!extensionsToLoad.length) return;
1826
- await this.loadAspects(extensionsToLoad, throwOnError);
1897
+ await this.loadAspects(extensionsToLoad, throwOnError, originatedFrom);
1827
1898
  }
1828
1899
  /**
1829
1900
  * Provides a cache folder, unique per key.
@@ -2068,10 +2139,7 @@ class Workspace {
2068
2139
 
2069
2140
  async _saveManyComponentConfigFiles(componentConfigFiles) {
2070
2141
  await Promise.all(Array.from(componentConfigFiles).map(async componentConfigFile => {
2071
- const componentDir = this.componentDir(componentConfigFile.componentId, {
2072
- ignoreVersion: true
2073
- });
2074
- await componentConfigFile.write(componentDir, {
2142
+ await componentConfigFile.write({
2075
2143
  override: true
2076
2144
  });
2077
2145
  }));
@@ -2461,12 +2529,12 @@ your workspace.jsonc has this component-id set. you might want to remove/change
2461
2529
  const existsOnWorkspace = await this.hasId(envId);
2462
2530
  const envIdStrNoVersion = envId.toStringWithoutVersion();
2463
2531
  await this.unsetEnvFromComponents(componentIds);
2464
- componentIds.forEach(componentId => {
2465
- this.bitMap.addComponentConfig(componentId, existsOnWorkspace ? envIdStrNoVersion : envIdStr);
2466
- this.bitMap.addComponentConfig(componentId, _envs().EnvsAspect.id, {
2532
+ await Promise.all(componentIds.map(async componentId => {
2533
+ await this.addSpecificComponentConfig(componentId, existsOnWorkspace ? envIdStrNoVersion : envIdStr);
2534
+ await this.addSpecificComponentConfig(componentId, _envs().EnvsAspect.id, {
2467
2535
  env: envIdStrNoVersion
2468
2536
  });
2469
- });
2537
+ }));
2470
2538
  await this.bitMap.write();
2471
2539
  }
2472
2540
  /**
@@ -2477,11 +2545,8 @@ your workspace.jsonc has this component-id set. you might want to remove/change
2477
2545
  async unsetEnvFromComponents(ids) {
2478
2546
  const changed = [];
2479
2547
  const unchanged = [];
2480
- ids.forEach(id => {
2481
- var _bitMapEntry$config;
2482
-
2483
- const bitMapEntry = this.bitMap.getBitmapEntry(id);
2484
- const envsAspect = (_bitMapEntry$config = bitMapEntry.config) === null || _bitMapEntry$config === void 0 ? void 0 : _bitMapEntry$config[_envs().EnvsAspect.id];
2548
+ await Promise.all(ids.map(async id => {
2549
+ const envsAspect = await this.getSpecificComponentConfig(id, _envs().EnvsAspect.id);
2485
2550
  const currentEnv = envsAspect && envsAspect !== _config().REMOVE_EXTENSION_SPECIAL_SIGN ? envsAspect.env : null;
2486
2551
 
2487
2552
  if (!currentEnv) {
@@ -2489,10 +2554,10 @@ your workspace.jsonc has this component-id set. you might want to remove/change
2489
2554
  return;
2490
2555
  }
2491
2556
 
2492
- this.bitMap.removeComponentConfig(id, currentEnv, false);
2493
- this.bitMap.removeComponentConfig(id, _envs().EnvsAspect.id, false);
2557
+ await this.removeSpecificComponentConfig(id, currentEnv, false);
2558
+ await this.removeSpecificComponentConfig(id, _envs().EnvsAspect.id, false);
2494
2559
  changed.push(id);
2495
- });
2560
+ }));
2496
2561
  await this.bitMap.write();
2497
2562
  return {
2498
2563
  changed,