@teambit/merging 0.0.252 → 0.0.253

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.
@@ -0,0 +1,588 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ require("core-js/modules/es.array.iterator.js");
5
+ require("core-js/modules/es.regexp.exec.js");
6
+ Object.defineProperty(exports, "__esModule", {
7
+ value: true
8
+ });
9
+ exports.ConfigMerger = void 0;
10
+ function _defineProperty2() {
11
+ const data = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
12
+ _defineProperty2 = function () {
13
+ return data;
14
+ };
15
+ return data;
16
+ }
17
+ function _semver() {
18
+ const data = _interopRequireDefault(require("semver"));
19
+ _semver = function () {
20
+ return data;
21
+ };
22
+ return data;
23
+ }
24
+ function _dependencyResolver() {
25
+ const data = require("@teambit/dependency-resolver");
26
+ _dependencyResolver = function () {
27
+ return data;
28
+ };
29
+ return data;
30
+ }
31
+ function _envs() {
32
+ const data = require("@teambit/envs");
33
+ _envs = function () {
34
+ return data;
35
+ };
36
+ return data;
37
+ }
38
+ function _lodash() {
39
+ const data = require("lodash");
40
+ _lodash = function () {
41
+ return data;
42
+ };
43
+ return data;
44
+ }
45
+ function _configMergeResult() {
46
+ const data = require("./config-merge-result");
47
+ _configMergeResult = function () {
48
+ return data;
49
+ };
50
+ return data;
51
+ }
52
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
53
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2().default)(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
54
+ /**
55
+ * perform 3-way merge of component configuration (aspects).
56
+ * normally this is needed when merging one lane into another. the component may have different aspects config in each lane.
57
+ * the baseAspects are the aspects of the component in the diversion point (the common ancestor of the two lanes).
58
+ * the currentAspects are the aspects of the component in the current lane.
59
+ * the otherAspects are the aspects of the component in the other lane. this is the lane we merge into the current lane.
60
+ *
61
+ * the basic merging strategy is a simple comparison between the aspect-configs, if they're different, we have a conflict.
62
+ * we have two special cases:
63
+ *
64
+ * 1. dependency-resolver: we do a deeper check for the policy, we compare each dependency separately. also, we take
65
+ * into account not only the config, but also the data. this is needed because some dependencies are automatically
66
+ * added by Bit (from the import statements in the code) and they're not in the config. the final config has the deps
67
+ * from both sources, the config and the data. The way we know to differentiate between them is by the "force" prop.
68
+ * the config has always force: true.
69
+ *
70
+ * 2. envs: if we don't treat it specially, the user will need to make the change not only in the envs aspect, but also
71
+ * in the deps-resolver (because the env is added as a devDependency) and also in the aspect itself (because
72
+ * teambit.envs/env has only the id and not the version). to make it simpler, we ignore the envs in the deps-resolver
73
+ * we ignore the individual aspect that is the env itself. we only show teambit.envs/env and we put the env id and
74
+ * version. later, when the component is loaded, we split the id and the version and put them in the correct places.
75
+ * see workspace.componentExtension / adjustEnvsOnConfigMerge for more details.
76
+ */
77
+ class ConfigMerger {
78
+ constructor(compIdStr, workspaceIds, currentAspects, baseAspects, otherAspects, currentLabel, otherLabel) {
79
+ this.compIdStr = compIdStr;
80
+ this.workspaceIds = workspaceIds;
81
+ this.currentAspects = currentAspects;
82
+ this.baseAspects = baseAspects;
83
+ this.otherAspects = otherAspects;
84
+ this.currentLabel = currentLabel;
85
+ this.otherLabel = otherLabel;
86
+ (0, _defineProperty2().default)(this, "currentEnv", void 0);
87
+ (0, _defineProperty2().default)(this, "otherEnv", void 0);
88
+ (0, _defineProperty2().default)(this, "baseEnv", void 0);
89
+ (0, _defineProperty2().default)(this, "handledExtIds", []);
90
+ }
91
+ merge() {
92
+ this.populateEnvs();
93
+ const results = this.currentAspects.map(currentExt => {
94
+ const id = currentExt.stringId;
95
+ if (this.handledExtIds.includes(id)) return null;
96
+ this.handledExtIds.push(id);
97
+ const baseExt = this.baseAspects.findExtension(id, true);
98
+ const otherExt = this.otherAspects.findExtension(id, true);
99
+ if (otherExt) {
100
+ // try to 3-way-merge
101
+ return this.mergePerStrategy({
102
+ id,
103
+ currentExt,
104
+ otherExt,
105
+ baseExt
106
+ });
107
+ }
108
+ // exist in current but not in other
109
+ if (baseExt) {
110
+ // was removed on other
111
+ return this.basicConflictGenerator({
112
+ id,
113
+ currentConfig: this.getConfig(currentExt),
114
+ otherConfig: '-'
115
+ });
116
+ }
117
+ // exist in current but not in other and base, so it got created on current. nothing to do.
118
+ return null;
119
+ });
120
+ const otherAspectsNotHandledResults = this.otherAspects.map(otherExt => {
121
+ const id = otherExt.stringId;
122
+ if (this.handledExtIds.includes(id)) return null;
123
+ this.handledExtIds.push(id);
124
+ const baseExt = this.baseAspects.findExtension(id, true);
125
+ if (baseExt) {
126
+ // was removed on current
127
+ return this.basicConflictGenerator({
128
+ id,
129
+ currentConfig: '-',
130
+ otherConfig: this.getConfig(otherExt)
131
+ });
132
+ }
133
+ // exist in other but not in current and base, so it got created on other.
134
+ return {
135
+ id,
136
+ mergedConfig: this.getConfig(otherExt)
137
+ };
138
+ });
139
+ const envResult = this.envStrategy();
140
+ return new (_configMergeResult().ConfigMergeResult)(this.compIdStr, (0, _lodash().compact)([...results, ...otherAspectsNotHandledResults, envResult]));
141
+ }
142
+ populateEnvs() {
143
+ // populate ids
144
+ const getEnvId = ext => {
145
+ const envsAspect = ext.findCoreExtension(_envs().EnvsAspect.id);
146
+ if (!envsAspect) throw new Error(`unable to find ${_envs().EnvsAspect.id} aspect for ${this.compIdStr}`);
147
+ const env = envsAspect.config.env || envsAspect.data.id;
148
+ if (!env) throw new Error(`unable to find env for ${this.compIdStr}, the config and data of ${_envs().EnvsAspect.id} are empty}`);
149
+ return env;
150
+ };
151
+ const currentEnv = getEnvId(this.currentAspects);
152
+ this.currentEnv = {
153
+ id: currentEnv
154
+ };
155
+ const otherEnv = getEnvId(this.otherAspects);
156
+ this.otherEnv = {
157
+ id: otherEnv
158
+ };
159
+ const baseEnv = this.baseAspects ? getEnvId(this.baseAspects) : undefined;
160
+ if (baseEnv) this.baseEnv = {
161
+ id: baseEnv
162
+ };
163
+
164
+ // populate version
165
+ const currentEnvAspect = this.currentAspects.findExtension(currentEnv, true);
166
+ if (currentEnvAspect) {
167
+ var _currentEnvAspect$ext;
168
+ this.handledExtIds.push(currentEnvAspect.stringId);
169
+ this.currentEnv.version = (_currentEnvAspect$ext = currentEnvAspect.extensionId) === null || _currentEnvAspect$ext === void 0 ? void 0 : _currentEnvAspect$ext.version;
170
+ this.currentEnv.config = currentEnvAspect.rawConfig;
171
+ }
172
+ const otherEnvAspect = this.otherAspects.findExtension(otherEnv, true);
173
+ if (otherEnvAspect) {
174
+ var _otherEnvAspect$exten;
175
+ this.handledExtIds.push(otherEnvAspect.stringId);
176
+ this.otherEnv.version = (_otherEnvAspect$exten = otherEnvAspect.extensionId) === null || _otherEnvAspect$exten === void 0 ? void 0 : _otherEnvAspect$exten.version;
177
+ this.otherEnv.config = otherEnvAspect.rawConfig;
178
+ }
179
+ if (this.baseEnv) {
180
+ const baseEnvAspect = this.baseAspects.findExtension(baseEnv, true);
181
+ if (baseEnvAspect) {
182
+ var _baseEnvAspect$extens;
183
+ this.baseEnv.version = (_baseEnvAspect$extens = baseEnvAspect.extensionId) === null || _baseEnvAspect$extens === void 0 ? void 0 : _baseEnvAspect$extens.version;
184
+ this.baseEnv.config = baseEnvAspect.rawConfig;
185
+ }
186
+ }
187
+ }
188
+ envStrategy() {
189
+ const mergeStrategyParams = {
190
+ id: _envs().EnvsAspect.id,
191
+ currentConfig: {
192
+ env: this.currentEnv.version ? `${this.currentEnv.id}@${this.currentEnv.version}` : this.currentEnv.id
193
+ },
194
+ otherConfig: {
195
+ env: this.otherEnv.version ? `${this.otherEnv.id}@${this.otherEnv.version}` : this.otherEnv.id
196
+ }
197
+ };
198
+ if (this.baseEnv) {
199
+ var _this$baseEnv, _this$baseEnv2;
200
+ mergeStrategyParams.baseConfig = {
201
+ env: (_this$baseEnv = this.baseEnv) !== null && _this$baseEnv !== void 0 && _this$baseEnv.version ? `${this.baseEnv.id}@${this.baseEnv.version}` : (_this$baseEnv2 = this.baseEnv) === null || _this$baseEnv2 === void 0 ? void 0 : _this$baseEnv2.id
202
+ };
203
+ }
204
+ if (this.currentEnv.id === this.otherEnv.id && this.currentEnv.version === this.otherEnv.version) {
205
+ return null;
206
+ }
207
+ if (this.isIdInWorkspace(this.currentEnv.id)) {
208
+ // the env currently used is part of the workspace, that's what the user needs. don't try to resolve anything.
209
+ return null;
210
+ }
211
+ return this.basicConfigMerge(mergeStrategyParams);
212
+ }
213
+ areConfigsEqual(configA, configB) {
214
+ return JSON.stringify(configA) === JSON.stringify(configB);
215
+ }
216
+ mergePerStrategy(mergeStrategyParams) {
217
+ const {
218
+ id,
219
+ currentExt,
220
+ otherExt,
221
+ baseExt
222
+ } = mergeStrategyParams;
223
+ const depResolverResult = this.depResolverStrategy(mergeStrategyParams);
224
+ if (depResolverResult) {
225
+ return depResolverResult;
226
+ }
227
+ const currentConfig = this.getConfig(currentExt);
228
+ const otherConfig = this.getConfig(otherExt);
229
+ const baseConfig = baseExt ? this.getConfig(baseExt) : undefined;
230
+ return this.basicConfigMerge({
231
+ id,
232
+ currentConfig,
233
+ otherConfig,
234
+ baseConfig
235
+ });
236
+ }
237
+ basicConfigMerge(mergeStrategyParams) {
238
+ const {
239
+ id,
240
+ currentConfig,
241
+ otherConfig,
242
+ baseConfig
243
+ } = mergeStrategyParams;
244
+ if (this.areConfigsEqual(currentConfig, otherConfig)) {
245
+ return null;
246
+ }
247
+ if (baseConfig && this.areConfigsEqual(baseConfig, otherConfig)) {
248
+ // was changed on current
249
+ return null;
250
+ }
251
+ if (baseConfig && this.areConfigsEqual(baseConfig, currentConfig)) {
252
+ // was changed on other
253
+ return {
254
+ id,
255
+ mergedConfig: otherConfig
256
+ };
257
+ }
258
+ // either no baseConfig, or baseConfig is also different from both: other and local. that's a conflict.
259
+ const mergeStrategyConfigParams = {
260
+ id,
261
+ currentConfig,
262
+ otherConfig,
263
+ baseConfig
264
+ };
265
+ return this.basicConflictGenerator(mergeStrategyConfigParams);
266
+ }
267
+ basicConflictGenerator({
268
+ id,
269
+ currentConfig,
270
+ otherConfig
271
+ }) {
272
+ // uncomment to debug
273
+ // console.log('basicConflictGenerator', this.compIdStr, id, 'currentConfig', currentConfig, 'otherConfig', otherConfig);
274
+ let conflict;
275
+ if (currentConfig === '-') {
276
+ conflict = `${'<'.repeat(7)} ${this.currentLabel}
277
+ =======
278
+ "${id}": ${JSON.stringify(otherConfig, undefined, 2)}
279
+ ${'>'.repeat(7)} ${this.otherLabel}`;
280
+ } else if (otherConfig === '-') {
281
+ conflict = `${'<'.repeat(7)} ${this.currentLabel}
282
+ "${id}": ${JSON.stringify(otherConfig, undefined, 2)}
283
+ =======
284
+ ${'>'.repeat(7)} ${this.otherLabel}`;
285
+ } else {
286
+ const formatConfig = conf => {
287
+ const confStr = JSON.stringify(conf, undefined, 2);
288
+ const confStrSplit = confStr.split('\n');
289
+ confStrSplit.shift(); // remove first {
290
+ confStrSplit.pop(); // remove last }
291
+ return confStrSplit.join('\n');
292
+ };
293
+ conflict = `"${id}": {
294
+ ${'<'.repeat(7)} ${this.currentLabel}
295
+ ${formatConfig(currentConfig)}
296
+ =======
297
+ ${formatConfig(otherConfig)}
298
+ ${'>'.repeat(7)} ${this.otherLabel}
299
+ }`;
300
+ }
301
+ return {
302
+ id,
303
+ conflict
304
+ };
305
+ }
306
+ depResolverStrategy(params) {
307
+ if (params.id !== _dependencyResolver().DependencyResolverAspect.id) return undefined;
308
+ const {
309
+ currentExt,
310
+ otherExt,
311
+ baseExt
312
+ } = params;
313
+ const currentConfig = this.getConfig(currentExt);
314
+ const otherConfig = this.getConfig(otherExt);
315
+ const baseConfig = baseExt ? this.getConfig(baseExt) : undefined;
316
+ // const mergeStrategyConfigParams = { id, currentConfig, otherConfig, baseConfig };
317
+
318
+ const mergedPolicy = {
319
+ dependencies: [],
320
+ devDependencies: [],
321
+ peerDependencies: []
322
+ };
323
+ const conflictedPolicy = {
324
+ dependencies: [],
325
+ devDependencies: [],
326
+ peerDependencies: []
327
+ };
328
+ let isMerged = false;
329
+ let hasConflict = false;
330
+ const conflictIndicator = 'CONFLICT::';
331
+ const nonPolicyConfigToMerged = {};
332
+ const nonPolicyConfigConflict = {};
333
+ const handleConfigMerge = () => {
334
+ if (this.areConfigsEqual(currentConfig, otherConfig)) {
335
+ return;
336
+ }
337
+ if (baseConfig && this.areConfigsEqual(baseConfig, otherConfig)) {
338
+ // was changed on current
339
+ return;
340
+ }
341
+ if (currentConfig === '-' || otherConfig === '-') {
342
+ throw new Error('not implemented. Is it possible to have it as minus?');
343
+ }
344
+ const allKeys = (0, _lodash().uniq)([...Object.keys(currentConfig), ...Object.keys(otherConfig)]);
345
+ const otherKeys = allKeys.filter(key => key !== 'policy');
346
+ if (baseConfig && this.areConfigsEqual(baseConfig, currentConfig)) {
347
+ // was changed on other
348
+ otherKeys.forEach(key => {
349
+ if (otherConfig[key]) {
350
+ nonPolicyConfigToMerged[key] = otherConfig[key];
351
+ }
352
+ });
353
+ if (otherConfig.policy) {
354
+ ['dependencies', 'devDependencies', 'peerDependencies'].forEach(depType => {
355
+ if (!otherConfig.policy[depType]) return;
356
+ Object.keys(otherConfig.policy[depType]).forEach(depName => {
357
+ mergedPolicy[depType].push({
358
+ name: depName,
359
+ version: otherConfig.policy[depType][depName],
360
+ force: true
361
+ });
362
+ });
363
+ });
364
+ }
365
+ isMerged = true;
366
+ return;
367
+ }
368
+
369
+ // either no baseConfig, or baseConfig is also different from both: other and local. that's a conflict.
370
+ // we have to differentiate between a conflict in the policy, which might be merged with conflict with dependencies data.
371
+ // and a conflict with other fields of config.
372
+ otherKeys.forEach(key => {
373
+ if (JSON.stringify(currentConfig[key]) === JSON.stringify(otherConfig[key])) {
374
+ return;
375
+ }
376
+ if (baseConfig && JSON.stringify(baseConfig[key]) === JSON.stringify(otherConfig[key])) {
377
+ // was changed on current
378
+ return;
379
+ }
380
+ if (baseConfig && JSON.stringify(baseConfig[key]) === JSON.stringify(currentConfig[key])) {
381
+ // was changed on other
382
+ nonPolicyConfigToMerged[key] = otherConfig[key];
383
+ return;
384
+ }
385
+ // conflict
386
+ nonPolicyConfigConflict[key] = {
387
+ current: currentConfig[key],
388
+ other: otherConfig[key]
389
+ };
390
+ });
391
+ if (!currentConfig.policy && !otherConfig.policy) return;
392
+ ['dependencies', 'devDependencies', 'peerDependencies'].forEach(depType => {
393
+ var _currentConfig$policy, _otherConfig$policy;
394
+ const currentPolicy = (_currentConfig$policy = currentConfig.policy) === null || _currentConfig$policy === void 0 ? void 0 : _currentConfig$policy[depType];
395
+ const otherPolicy = (_otherConfig$policy = otherConfig.policy) === null || _otherConfig$policy === void 0 ? void 0 : _otherConfig$policy[depType];
396
+ const allPolicyKeys = (0, _lodash().uniq)(Object.keys(currentPolicy || {}).concat(Object.keys(otherPolicy || {})));
397
+ if (!allPolicyKeys.length) return;
398
+ allPolicyKeys.forEach(pkgName => {
399
+ var _baseConfig$policy, _baseConfig$policy$de;
400
+ const currentVal = currentPolicy === null || currentPolicy === void 0 ? void 0 : currentPolicy[pkgName];
401
+ const otherVal = otherPolicy === null || otherPolicy === void 0 ? void 0 : otherPolicy[pkgName];
402
+ const baseVal = baseConfig !== '-' && (baseConfig === null || baseConfig === void 0 ? void 0 : (_baseConfig$policy = baseConfig.policy) === null || _baseConfig$policy === void 0 ? void 0 : (_baseConfig$policy$de = _baseConfig$policy[depType]) === null || _baseConfig$policy$de === void 0 ? void 0 : _baseConfig$policy$de[pkgName]);
403
+ if (this.isEnv(pkgName)) {
404
+ // ignore the envs
405
+ return;
406
+ }
407
+ if (currentVal === otherVal) {
408
+ // mergedPolicy[depType][pkgName] = currentVal;
409
+ return;
410
+ }
411
+ if (baseVal === otherVal) {
412
+ // mergedPolicy[depType][pkgName] = currentVal;
413
+ return;
414
+ }
415
+ if (baseVal === currentVal) {
416
+ mergedPolicy[depType].push({
417
+ name: pkgName,
418
+ version: otherVal,
419
+ force: true
420
+ });
421
+ isMerged = true;
422
+ return;
423
+ }
424
+ // either no baseVal, or baseVal is also different from both: other and local. that's a conflict.
425
+ hasConflict = true;
426
+ conflictedPolicy[depType].push({
427
+ name: pkgName,
428
+ version: `${conflictIndicator}${currentVal}::${otherVal}::`,
429
+ force: true
430
+ });
431
+ });
432
+ });
433
+ };
434
+ handleConfigMerge();
435
+ const lifecycleToDepType = {
436
+ runtime: 'dependencies',
437
+ dev: 'devDependencies',
438
+ peer: 'peerDependencies'
439
+ };
440
+ const hasConfigForDep = (depType, depName) => mergedPolicy[depType].find(d => d.name === depName);
441
+ const addSerializedDepToPolicy = dep => {
442
+ const depType = lifecycleToDepType[dep.lifecycle];
443
+ if (hasConfigForDep(depType, dep.id)) {
444
+ return; // there is already config for it.
445
+ }
446
+
447
+ mergedPolicy[depType].push({
448
+ name: dep.id,
449
+ version: dep.policy || dep.version,
450
+ force: false
451
+ });
452
+ isMerged = true;
453
+ };
454
+ const getAutoDeps = ext => {
455
+ var _ext$findCoreExtensio, _ext$findCoreExtensio2;
456
+ const data = (_ext$findCoreExtensio = ext.findCoreExtension(_dependencyResolver().DependencyResolverAspect.id)) === null || _ext$findCoreExtensio === void 0 ? void 0 : _ext$findCoreExtensio.data.dependencies;
457
+ if (!data) return [];
458
+ const policy = ((_ext$findCoreExtensio2 = ext.findCoreExtension(_dependencyResolver().DependencyResolverAspect.id)) === null || _ext$findCoreExtensio2 === void 0 ? void 0 : _ext$findCoreExtensio2.data.policy) || [];
459
+ return data.filter(d => d.source === 'auto').map(d => {
460
+ const idWithoutVersion = d.__type === 'package' ? d.id : d.id.split('@')[0];
461
+ const existingPolicy = policy.find(p => p.dependencyId === idWithoutVersion);
462
+ const getPolicyVer = () => {
463
+ if (d.__type === 'package') return undefined; // for packages, the policy is already the version
464
+ if (existingPolicy) return existingPolicy.value.version; // currently it's missing, will be implemented by @Gilad
465
+ if (!_semver().default.valid(d.version)) return d.version; // could be a hash
466
+ // default to `^` or ~ if starts with zero, until we save the policy from the workspace during tag/snap.
467
+ return d.version.startsWith('0.') ? `~${d.version}` : `^${d.version}`;
468
+ };
469
+ return _objectSpread(_objectSpread({}, d), {}, {
470
+ id: idWithoutVersion,
471
+ policy: getPolicyVer()
472
+ });
473
+ });
474
+ };
475
+ const currentData = getAutoDeps(this.currentAspects);
476
+ const otherData = getAutoDeps(this.otherAspects);
477
+ const currentAndOtherData = (0, _lodash().uniqBy)(currentData.concat(otherData), d => d.id);
478
+ const baseData = getAutoDeps(this.baseAspects);
479
+
480
+ // uncomment to debug
481
+ // console.log('\n\n**************', this.compIdStr, '**************');
482
+ // console.log('currentData', currentData.length, '\n', currentData.map((d) => `${d.__type} ${d.id} ${d.version}`).join('\n'));
483
+ // console.log('otherData', otherData.length, '\n', otherData.map((d) => `${d.__type} ${d.id} ${d.version}`).join('\n'));
484
+ // console.log('otherData', baseData.length, '\n', baseData.map((d) => `${d.__type} ${d.id} ${d.version}`).join('\n'));
485
+ // console.log('** END **\n\n');
486
+
487
+ currentAndOtherData.forEach(depData => {
488
+ if (this.isEnv(depData.id)) {
489
+ // ignore the envs
490
+ return;
491
+ }
492
+ const currentDep = currentData.find(d => d.id === depData.id);
493
+ const otherDep = otherData.find(d => d.id === depData.id);
494
+ if (!otherDep) {
495
+ return;
496
+ }
497
+ if (!currentDep) {
498
+ // only on other
499
+ addSerializedDepToPolicy(otherDep);
500
+ return;
501
+ }
502
+ const currentId = currentDep.id;
503
+ const currentVer = currentDep.policy || currentDep.version;
504
+ const otherVer = otherDep.policy || otherDep.version;
505
+ if (currentVer === otherVer) {
506
+ return;
507
+ }
508
+ const baseDep = baseData.find(d => d.id === currentId);
509
+ const baseVer = (baseDep === null || baseDep === void 0 ? void 0 : baseDep.policy) || (baseDep === null || baseDep === void 0 ? void 0 : baseDep.version);
510
+ if (baseVer && baseVer === otherVer) {
511
+ return;
512
+ }
513
+ if (currentDep.__type === 'component' && this.isIdInWorkspace(currentId)) {
514
+ // dependencies that exist in the workspace, should be ignored. they'll be resolved later to the version in the ws.
515
+ return;
516
+ }
517
+ if (baseVer && baseVer === currentVer) {
518
+ addSerializedDepToPolicy(otherDep);
519
+ return;
520
+ }
521
+ // either no baseVal, or baseVal is also different from both: other and local. that's probably a conflict.
522
+ const depType = lifecycleToDepType[currentDep.lifecycle];
523
+ if (hasConfigForDep(depType, currentDep.id)) {
524
+ return; // there is already config for it.
525
+ }
526
+ // try to resolve according to semver
527
+ if (currentDep.policy && otherDep.policy && (0, _dependencyResolver().isRange)(currentDep.policy, currentDep.id) && (0, _dependencyResolver().isRange)(otherDep.policy, otherDep.id)) {
528
+ if (_semver().default.satisfies(currentDep.version, otherDep.policy)) {
529
+ return;
530
+ }
531
+ if (_semver().default.satisfies(otherDep.version, currentDep.policy)) {
532
+ addSerializedDepToPolicy(otherDep);
533
+ return;
534
+ }
535
+ }
536
+ hasConflict = true;
537
+ conflictedPolicy[depType].push({
538
+ name: currentDep.id,
539
+ version: `${conflictIndicator}${currentVer}::${otherVer}::`,
540
+ force: false
541
+ });
542
+ });
543
+ ['dependencies', 'devDependencies', 'peerDependencies'].forEach(depType => {
544
+ if (!mergedPolicy[depType].length) delete mergedPolicy[depType];
545
+ if (!conflictedPolicy[depType].length) delete conflictedPolicy[depType];
546
+ });
547
+ let conflictStr;
548
+ if (hasConflict) {
549
+ const mergedConfigSplit = JSON.stringify({
550
+ policy: conflictedPolicy
551
+ }, undefined, 2).split('\n');
552
+ const conflictLines = mergedConfigSplit.map(line => {
553
+ if (!line.includes(conflictIndicator)) return line;
554
+ const [, currentVal, otherVal] = line.split('::');
555
+ return `${'<'.repeat(7)} ${this.currentLabel}
556
+ "version": "${currentVal}",
557
+ =======
558
+ "version": "${otherVal}",
559
+ ${'>'.repeat(7)} ${this.otherLabel}`;
560
+ });
561
+ // replace the first line with line with the id
562
+ conflictLines.shift();
563
+ conflictLines.unshift(`"${params.id}": {`);
564
+ conflictStr = conflictLines.join('\n');
565
+ }
566
+ const config = isMerged ? _objectSpread(_objectSpread({}, nonPolicyConfigToMerged), {}, {
567
+ policy: mergedPolicy
568
+ }) : undefined;
569
+ return {
570
+ id: params.id,
571
+ mergedConfig: config,
572
+ conflict: conflictStr
573
+ };
574
+ }
575
+ isIdInWorkspace(id) {
576
+ return Boolean(this.workspaceIds.find(c => c.toStringWithoutVersion() === id));
577
+ }
578
+ isEnv(id) {
579
+ return id === this.currentEnv.id || id === this.otherEnv.id;
580
+ }
581
+ getConfig(ext) {
582
+ if (ext.rawConfig === '-') return ext.rawConfig;
583
+ return (0, _lodash().omit)(ext.rawConfig, ['__specific']);
584
+ }
585
+ }
586
+ exports.ConfigMerger = ConfigMerger;
587
+
588
+ //# sourceMappingURL=config-merger.js.map