@teambit/config-merger 0.0.1

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