@teambit/merging 1.0.133 → 1.0.135

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.
@@ -11,27 +11,6 @@ function _cli() {
11
11
  };
12
12
  return data;
13
13
  }
14
- function _semver() {
15
- const data = _interopRequireDefault(require("semver"));
16
- _semver = function () {
17
- return data;
18
- };
19
- return data;
20
- }
21
- function _tempy() {
22
- const data = _interopRequireDefault(require("tempy"));
23
- _tempy = function () {
24
- return data;
25
- };
26
- return data;
27
- }
28
- function _fsExtra() {
29
- const data = _interopRequireDefault(require("fs-extra"));
30
- _fsExtra = function () {
31
- return data;
32
- };
33
- return data;
34
- }
35
14
  function _workspace() {
36
15
  const data = _interopRequireWildcard(require("@teambit/workspace"));
37
16
  _workspace = function () {
@@ -165,13 +144,6 @@ function _lodash() {
165
144
  };
166
145
  return data;
167
146
  }
168
- function _dependencyResolver() {
169
- const data = require("@teambit/dependency-resolver");
170
- _dependencyResolver = function () {
171
- return data;
172
- };
173
- return data;
174
- }
175
147
  function _checkout() {
176
148
  const data = require("@teambit/checkout");
177
149
  _checkout = function () {
@@ -179,13 +151,6 @@ function _checkout() {
179
151
  };
180
152
  return data;
181
153
  }
182
- function _constants() {
183
- const data = require("@teambit/legacy/dist/constants");
184
- _constants = function () {
185
- return data;
186
- };
187
- return data;
188
- }
189
154
  function _deleteComponentFiles() {
190
155
  const data = _interopRequireDefault(require("@teambit/legacy/dist/consumer/component-ops/delete-component-files"));
191
156
  _deleteComponentFiles = function () {
@@ -193,9 +158,9 @@ function _deleteComponentFiles() {
193
158
  };
194
159
  return data;
195
160
  }
196
- function _mergeFiles() {
197
- const data = _interopRequireDefault(require("@teambit/legacy/dist/utils/merge-files"));
198
- _mergeFiles = function () {
161
+ function _configMerger() {
162
+ const data = require("@teambit/config-merger");
163
+ _configMerger = function () {
199
164
  return data;
200
165
  };
201
166
  return data;
@@ -221,13 +186,6 @@ function _merging() {
221
186
  };
222
187
  return data;
223
188
  }
224
- function _configMergeResult() {
225
- const data = require("./config-merge-result");
226
- _configMergeResult = function () {
227
- return data;
228
- };
229
- return data;
230
- }
231
189
  function _mergeStatusProvider() {
232
190
  const data = require("./merge-status-provider");
233
191
  _mergeStatusProvider = function () {
@@ -235,23 +193,18 @@ function _mergeStatusProvider() {
235
193
  };
236
194
  return data;
237
195
  }
196
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
238
197
  function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
239
198
  function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
240
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
241
199
  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; }
242
200
  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; }
243
201
  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; }
244
202
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); }
245
203
  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); }
246
- const WS_DEPS_FIELDS = ['dependencies', 'peerDependencies'];
247
-
248
- // from => to
249
- // the pkg value is in a format of CONFLICT::OURS::THEIRS
250
-
251
204
  // fileName is PathLinux. TS doesn't let anything else in the keys other than string and number
252
205
 
253
206
  class MergingMain {
254
- constructor(workspace, install, snapping, checkout, logger, componentWriter, importer, config, remove) {
207
+ constructor(workspace, install, snapping, checkout, logger, componentWriter, importer, config, remove, configMerger) {
255
208
  this.workspace = workspace;
256
209
  this.install = install;
257
210
  this.snapping = snapping;
@@ -261,6 +214,7 @@ class MergingMain {
261
214
  this.importer = importer;
262
215
  this.config = config;
263
216
  this.remove = remove;
217
+ this.configMerger = configMerger;
264
218
  }
265
219
  async merge(ids, mergeStrategy, abort, resolve, noSnap, message, build, skipDependencyInstallation) {
266
220
  if (!this.workspace) throw new (_workspace().OutsideWorkspaceError)();
@@ -334,12 +288,12 @@ class MergingMain {
334
288
  const {
335
289
  workspaceDepsUpdates,
336
290
  workspaceDepsConflicts
337
- } = await this.updateWorkspaceJsoncWithDepsIfNeeded(allConfigMerge);
291
+ } = await this.configMerger.updateWorkspaceJsoncWithDepsIfNeeded(allConfigMerge);
338
292
  let workspaceConflictError;
339
293
  if (workspaceDepsConflicts) {
340
- workspaceConflictError = await this.writeWorkspaceJsoncWithConflictsGracefully(workspaceDepsConflicts);
294
+ workspaceConflictError = await this.configMerger.writeWorkspaceJsoncWithConflictsGracefully(workspaceDepsConflicts);
341
295
  }
342
- await this.generateConfigMergeConflictFileForAll(allConfigMerge);
296
+ await this.configMerger.generateConfigMergeConflictFileForAll(allConfigMerge);
343
297
  if (currentLane) consumer.scope.objects.add(currentLane);
344
298
  await consumer.scope.objects.persist(); // persist anyway, if currentLane is null it should save all main heads
345
299
 
@@ -408,224 +362,6 @@ class MergingMain {
408
362
  workspaceDepsUpdates
409
363
  };
410
364
  }
411
- async generateConfigMergeConflictFileForAll(allConfigMerge) {
412
- const configMergeFile = this.workspace.getConflictMergeFile();
413
- allConfigMerge.forEach(configMerge => {
414
- const conflict = configMerge.generateMergeConflictFile();
415
- if (!conflict) return;
416
- configMergeFile.addConflict(configMerge.compIdStr, conflict);
417
- });
418
- if (configMergeFile.hasConflict()) {
419
- await configMergeFile.write();
420
- }
421
- }
422
- async writeWorkspaceJsoncWithConflictsGracefully(workspaceDepsConflicts) {
423
- try {
424
- await this.writeWorkspaceJsoncWithConflicts(workspaceDepsConflicts);
425
- return undefined;
426
- } catch (err) {
427
- this.logger.error(`unable to write workspace.jsonc with conflicts`, err);
428
- const errTitle = `unable to write workspace.jsonc with conflicts, due to an error: "${err.message}".
429
- see the conflicts below and edit your workspace.jsonc as you see fit.`;
430
- const conflictsStr = WS_DEPS_FIELDS.map(depField => {
431
- if (!workspaceDepsConflicts[depField]) return [];
432
- return workspaceDepsConflicts[depField].map(({
433
- name,
434
- version
435
- }) => {
436
- const {
437
- currentVal,
438
- otherVal
439
- } = (0, _configMergeResult().parseVersionLineWithConflict)(version);
440
- return `(${depField}) ${name}: ours: ${currentVal}, theirs: ${otherVal}`;
441
- });
442
- }).flat().join('\n');
443
- return new (_bitError().BitError)(`${errTitle}\n${conflictsStr}`);
444
- }
445
- }
446
- async writeWorkspaceJsoncWithConflicts(workspaceDepsConflicts) {
447
- const wsConfig = this.config.workspaceConfig;
448
- if (!wsConfig) throw new Error(`unable to get workspace config`);
449
- const wsJsoncPath = wsConfig.path;
450
- const wsJsoncOriginalContent = await _fsExtra().default.readFile(wsJsoncPath, 'utf8');
451
- let wsJsoncContent = wsJsoncOriginalContent;
452
- WS_DEPS_FIELDS.forEach(depField => {
453
- if (!workspaceDepsConflicts[depField]) return;
454
- workspaceDepsConflicts[depField].forEach(({
455
- name,
456
- version
457
- }) => {
458
- const {
459
- currentVal,
460
- otherVal
461
- } = (0, _configMergeResult().parseVersionLineWithConflict)(version);
462
- // e.g. "@ci/8oypmb6p-remote.bar.foo": "^0.0.3"
463
- const originalDep = `"${name}": "${currentVal}"`;
464
- if (!wsJsoncContent.includes(originalDep)) {
465
- throw new Error(`unable to find the dependency ${originalDep} in the workspace.jsonc`);
466
- }
467
- wsJsoncContent = wsJsoncContent.replace(originalDep, `"${name}": "${otherVal}"`);
468
- });
469
- });
470
- const baseFilePath = await _tempy().default.write('');
471
- const otherFilePath = await _tempy().default.write(wsJsoncContent);
472
- const mergeFilesParams = {
473
- filePath: wsJsoncPath,
474
- currentFile: {
475
- label: 'ours',
476
- path: wsJsoncPath
477
- },
478
- baseFile: {
479
- path: baseFilePath
480
- },
481
- otherFile: {
482
- label: 'theirs',
483
- path: otherFilePath
484
- }
485
- };
486
- const mergeResult = await (0, _mergeFiles().default)(mergeFilesParams);
487
- const conflictFile = mergeResult.conflict;
488
- if (!conflictFile) {
489
- this.logger.debug(`original content:\n${wsJsoncOriginalContent}`);
490
- this.logger.debug(`new content:\n${wsJsoncContent}`);
491
- throw new Error('unable to generate conflict from the workspace.jsonc file. see debug.log for the file content');
492
- }
493
- await wsConfig.backupConfigFile('before writing conflicts');
494
- await _fsExtra().default.writeFile(wsJsoncPath, conflictFile);
495
- }
496
- async updateWorkspaceJsoncWithDepsIfNeeded(allConfigMerge) {
497
- const allResults = allConfigMerge.map(c => c.getDepsResolverResult());
498
-
499
- // aggregate all dependencies that can be updated (not conflicting)
500
- const nonConflictDeps = {};
501
- const nonConflictSources = {}; // for logging/debugging purposes
502
- allConfigMerge.forEach(configMerge => {
503
- const mergedConfig = configMerge.getDepsResolverResult()?.mergedConfig;
504
- if (!mergedConfig || mergedConfig === '-') return;
505
- const mergedConfigPolicy = mergedConfig.policy || {};
506
- _constants().DEPENDENCIES_FIELDS.forEach(depField => {
507
- if (!mergedConfigPolicy[depField]) return;
508
- mergedConfigPolicy[depField].forEach(pkg => {
509
- if (pkg.force) return; // we only care about auto-detected dependencies
510
- if (nonConflictDeps[pkg.name]) {
511
- if (!nonConflictDeps[pkg.name].includes(pkg.version)) nonConflictDeps[pkg.name].push(pkg.version);
512
- nonConflictSources[pkg.name].push(configMerge.compIdStr);
513
- return;
514
- }
515
- nonConflictDeps[pkg.name] = [pkg.version];
516
- nonConflictSources[pkg.name] = [configMerge.compIdStr];
517
- });
518
- });
519
- });
520
-
521
- // aggregate all dependencies that have conflicts
522
- const conflictDeps = {};
523
- const conflictDepsSources = {}; // for logging/debugging purposes
524
- allConfigMerge.forEach(configMerge => {
525
- const mergedConfigConflict = configMerge.getDepsResolverResult()?.conflict;
526
- if (!mergedConfigConflict) return;
527
- _constants().DEPENDENCIES_FIELDS.forEach(depField => {
528
- if (!mergedConfigConflict[depField]) return;
529
- mergedConfigConflict[depField].forEach(pkg => {
530
- if (pkg.force) return; // we only care about auto-detected dependencies
531
- if (conflictDeps[pkg.name]) {
532
- if (!conflictDeps[pkg.name].includes(pkg.version)) conflictDeps[pkg.name].push(pkg.version);
533
- conflictDepsSources[pkg.name].push(configMerge.compIdStr);
534
- return;
535
- }
536
- conflictDeps[pkg.name] = [pkg.version];
537
- conflictDepsSources[pkg.name] = [configMerge.compIdStr];
538
- });
539
- });
540
- });
541
- const notConflictedPackages = Object.keys(nonConflictDeps);
542
- const conflictedPackages = Object.keys(conflictDeps);
543
- if (!notConflictedPackages.length && !conflictedPackages.length) return {};
544
- const workspaceConfig = this.config.workspaceConfig;
545
- if (!workspaceConfig) throw new Error(`updateWorkspaceJsoncWithDepsIfNeeded unable to get workspace config`);
546
- const depResolver = workspaceConfig.extensions.findCoreExtension(_dependencyResolver().DependencyResolverAspect.id);
547
- const policy = depResolver?.config.policy;
548
- if (!policy) {
549
- return {};
550
- }
551
-
552
- // calculate the workspace.json updates
553
- const workspaceJsonUpdates = {};
554
- notConflictedPackages.forEach(pkgName => {
555
- if (nonConflictDeps[pkgName].length > 1) {
556
- // we only want the deps that the other lane has them in the workspace.json and that all comps use the same dep.
557
- return;
558
- }
559
- _constants().DEPENDENCIES_FIELDS.forEach(depField => {
560
- if (!policy[depField]?.[pkgName]) return; // doesn't exists in the workspace.json
561
- const currentVer = policy[depField][pkgName];
562
- const newVer = nonConflictDeps[pkgName][0];
563
- if (currentVer === newVer) return;
564
- workspaceJsonUpdates[pkgName] = [currentVer, newVer];
565
- policy[depField][pkgName] = newVer;
566
- this.logger.debug(`update workspace.jsonc: ${pkgName} from ${currentVer} to ${newVer}. Triggered by: ${nonConflictSources[pkgName].join(', ')}`);
567
- });
568
- });
569
-
570
- // calculate the workspace.json conflicts
571
- const workspaceJsonConflicts = {
572
- dependencies: [],
573
- peerDependencies: []
574
- };
575
- const conflictPackagesToRemoveFromConfigMerge = [];
576
- conflictedPackages.forEach(pkgName => {
577
- if (conflictDeps[pkgName].length > 1) {
578
- // we only want the deps that the other lane has them in the workspace.json and that all comps use the same dep.
579
- return;
580
- }
581
- const conflictRaw = conflictDeps[pkgName][0];
582
- const [, currentVal, otherVal] = conflictRaw.split('::');
583
- WS_DEPS_FIELDS.forEach(depField => {
584
- if (!policy[depField]?.[pkgName]) return;
585
- const currentVerInWsJson = policy[depField][pkgName];
586
- if (!currentVerInWsJson) return;
587
- // the version is coming from the workspace.jsonc
588
- conflictPackagesToRemoveFromConfigMerge.push(pkgName);
589
- if (_semver().default.satisfies(otherVal, currentVerInWsJson)) {
590
- // the other version is compatible with the current version in the workspace.json
591
- return;
592
- }
593
- workspaceJsonConflicts[depField].push({
594
- name: pkgName,
595
- version: conflictRaw.replace(currentVal, currentVerInWsJson),
596
- force: false
597
- });
598
- conflictPackagesToRemoveFromConfigMerge.push(pkgName);
599
- this.logger.debug(`conflict workspace.jsonc: ${pkgName} current: ${currentVerInWsJson}, other: ${otherVal}. Triggered by: ${conflictDepsSources[pkgName].join(', ')}`);
600
- });
601
- });
602
- WS_DEPS_FIELDS.forEach(depField => {
603
- if ((0, _lodash().isEmpty)(workspaceJsonConflicts[depField])) delete workspaceJsonConflicts[depField];
604
- });
605
- if (conflictPackagesToRemoveFromConfigMerge.length) {
606
- allResults.forEach(result => {
607
- if (result?.conflict) {
608
- _constants().DEPENDENCIES_FIELDS.forEach(depField => {
609
- if (!result.conflict?.[depField]) return;
610
- result.conflict[depField] = result.conflict?.[depField].filter(dep => !conflictPackagesToRemoveFromConfigMerge.includes(dep.name));
611
- if (!result.conflict[depField].length) delete result.conflict[depField];
612
- });
613
- if ((0, _lodash().isEmpty)(result.conflict)) result.conflict = undefined;
614
- }
615
- });
616
- }
617
- if (Object.keys(workspaceJsonUpdates).length) {
618
- await workspaceConfig.write({
619
- reasonForChange: 'merge (update dependencies)'
620
- });
621
- }
622
- this.logger.debug('final workspace.jsonc updates', workspaceJsonUpdates);
623
- this.logger.debug('final workspace.jsonc conflicts', workspaceJsonConflicts);
624
- return {
625
- workspaceDepsUpdates: Object.keys(workspaceJsonUpdates).length ? workspaceJsonUpdates : undefined,
626
- workspaceDepsConflicts: Object.keys(workspaceJsonConflicts).length ? workspaceJsonConflicts : undefined
627
- };
628
- }
629
365
 
630
366
  /**
631
367
  * this function gets called from two different commands:
@@ -902,16 +638,16 @@ see the conflicts below and edit your workspace.jsonc as you see fit.`;
902
638
  }
903
639
  return ids.map(id => consumer.getParsedId(id));
904
640
  }
905
- static async provider([cli, workspace, snapping, checkout, install, loggerMain, compWriter, importer, config, remove, globalConfig]) {
641
+ static async provider([cli, workspace, snapping, checkout, install, loggerMain, compWriter, importer, config, remove, globalConfig, configMerger]) {
906
642
  const logger = loggerMain.createLogger(_merging().MergingAspect.id);
907
- const merging = new MergingMain(workspace, install, snapping, checkout, logger, compWriter, importer, config, remove);
643
+ const merging = new MergingMain(workspace, install, snapping, checkout, logger, compWriter, importer, config, remove, configMerger);
908
644
  cli.register(new (_mergeCmd().MergeCmd)(merging, globalConfig));
909
645
  return merging;
910
646
  }
911
647
  }
912
648
  exports.MergingMain = MergingMain;
913
649
  _defineProperty(MergingMain, "slots", []);
914
- _defineProperty(MergingMain, "dependencies", [_cli().CLIAspect, _workspace().default, _snapping().default, _checkout().CheckoutAspect, _install().InstallAspect, _logger().LoggerAspect, _componentWriter().ComponentWriterAspect, _importer().default, _config().ConfigAspect, _remove().default, _globalConfig().default]);
650
+ _defineProperty(MergingMain, "dependencies", [_cli().CLIAspect, _workspace().default, _snapping().default, _checkout().CheckoutAspect, _install().InstallAspect, _logger().LoggerAspect, _componentWriter().ComponentWriterAspect, _importer().default, _config().ConfigAspect, _remove().default, _globalConfig().default, _configMerger().ConfigMergerAspect]);
915
651
  _defineProperty(MergingMain, "runtime", _cli().MainRuntime);
916
652
  _merging().MergingAspect.addRuntime(MergingMain);
917
653