@teambit/importer 0.0.282 → 0.0.284

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,866 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ require("core-js/modules/es.array.flat-map.js");
6
+
7
+ require("core-js/modules/es.array.iterator.js");
8
+
9
+ require("core-js/modules/es.array.unscopables.flat-map.js");
10
+
11
+ require("core-js/modules/es.promise.js");
12
+
13
+ Object.defineProperty(exports, "__esModule", {
14
+ value: true
15
+ });
16
+ exports.default = void 0;
17
+
18
+ function _defineProperty2() {
19
+ const data = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
20
+
21
+ _defineProperty2 = function () {
22
+ return data;
23
+ };
24
+
25
+ return data;
26
+ }
27
+
28
+ function _chalk() {
29
+ const data = _interopRequireDefault(require("chalk"));
30
+
31
+ _chalk = function () {
32
+ return data;
33
+ };
34
+
35
+ return data;
36
+ }
37
+
38
+ function _ramda() {
39
+ const data = _interopRequireDefault(require("ramda"));
40
+
41
+ _ramda = function () {
42
+ return data;
43
+ };
44
+
45
+ return data;
46
+ }
47
+
48
+ function _semver() {
49
+ const data = _interopRequireDefault(require("semver"));
50
+
51
+ _semver = function () {
52
+ return data;
53
+ };
54
+
55
+ return data;
56
+ }
57
+
58
+ function _bitError() {
59
+ const data = require("@teambit/bit-error");
60
+
61
+ _bitError = function () {
62
+ return data;
63
+ };
64
+
65
+ return data;
66
+ }
67
+
68
+ function _pMapSeries() {
69
+ const data = _interopRequireDefault(require("p-map-series"));
70
+
71
+ _pMapSeries = function () {
72
+ return data;
73
+ };
74
+
75
+ return data;
76
+ }
77
+
78
+ function _componentVersion() {
79
+ const data = require("@teambit/component-version");
80
+
81
+ _componentVersion = function () {
82
+ return data;
83
+ };
84
+
85
+ return data;
86
+ }
87
+
88
+ function _listScope() {
89
+ const data = require("@teambit/legacy/dist/api/consumer/lib/list-scope");
90
+
91
+ _listScope = function () {
92
+ return data;
93
+ };
94
+
95
+ return data;
96
+ }
97
+
98
+ function _bitId() {
99
+ const data = require("@teambit/legacy/dist/bit-id");
100
+
101
+ _bitId = function () {
102
+ return data;
103
+ };
104
+
105
+ return data;
106
+ }
107
+
108
+ function _loader() {
109
+ const data = _interopRequireDefault(require("@teambit/legacy/dist/cli/loader"));
110
+
111
+ _loader = function () {
112
+ return data;
113
+ };
114
+
115
+ return data;
116
+ }
117
+
118
+ function _loaderMessages() {
119
+ const data = require("@teambit/legacy/dist/cli/loader/loader-messages");
120
+
121
+ _loaderMessages = function () {
122
+ return data;
123
+ };
124
+
125
+ return data;
126
+ }
127
+
128
+ function _generalError() {
129
+ const data = _interopRequireDefault(require("@teambit/legacy/dist/error/general-error"));
130
+
131
+ _generalError = function () {
132
+ return data;
133
+ };
134
+
135
+ return data;
136
+ }
137
+
138
+ function _showDoctorError() {
139
+ const data = _interopRequireDefault(require("@teambit/legacy/dist/error/show-doctor-error"));
140
+
141
+ _showDoctorError = function () {
142
+ return data;
143
+ };
144
+
145
+ return data;
146
+ }
147
+
148
+ function _logger() {
149
+ const data = _interopRequireDefault(require("@teambit/legacy/dist/logger/logger"));
150
+
151
+ _logger = function () {
152
+ return data;
153
+ };
154
+
155
+ return data;
156
+ }
157
+
158
+ function _scopeRemotes() {
159
+ const data = require("@teambit/legacy/dist/scope/scope-remotes");
160
+
161
+ _scopeRemotes = function () {
162
+ return data;
163
+ };
164
+
165
+ return data;
166
+ }
167
+
168
+ function _utils() {
169
+ const data = require("@teambit/legacy/dist/utils");
170
+
171
+ _utils = function () {
172
+ return data;
173
+ };
174
+
175
+ return data;
176
+ }
177
+
178
+ function _hasWildcard() {
179
+ const data = _interopRequireDefault(require("@teambit/legacy/dist/utils/string/has-wildcard"));
180
+
181
+ _hasWildcard = function () {
182
+ return data;
183
+ };
184
+
185
+ return data;
186
+ }
187
+
188
+ function _exceptions() {
189
+ const data = require("@teambit/legacy/dist/consumer/exceptions");
190
+
191
+ _exceptions = function () {
192
+ return data;
193
+ };
194
+
195
+ return data;
196
+ }
197
+
198
+ function _checkoutVersion() {
199
+ const data = require("@teambit/legacy/dist/consumer/versions-ops/checkout-version");
200
+
201
+ _checkoutVersion = function () {
202
+ return data;
203
+ };
204
+
205
+ return data;
206
+ }
207
+
208
+ function _mergeVersion() {
209
+ const data = require("@teambit/legacy/dist/consumer/versions-ops/merge-version");
210
+
211
+ _mergeVersion = function () {
212
+ return data;
213
+ };
214
+
215
+ return data;
216
+ }
217
+
218
+ function _componentsPendingMerge() {
219
+ const data = _interopRequireDefault(require("@teambit/legacy/dist/consumer/component-ops/exceptions/components-pending-merge"));
220
+
221
+ _componentsPendingMerge = function () {
222
+ return data;
223
+ };
224
+
225
+ return data;
226
+ }
227
+
228
+ function _manyComponentsWriter() {
229
+ const data = _interopRequireDefault(require("@teambit/legacy/dist/consumer/component-ops/many-components-writer"));
230
+
231
+ _manyComponentsWriter = function () {
232
+ return data;
233
+ };
234
+
235
+ return data;
236
+ }
237
+
238
+ class ImportComponents {
239
+ // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
240
+ // @ts-ignore
241
+ constructor(consumer, options) {
242
+ (0, _defineProperty2().default)(this, "consumer", void 0);
243
+ (0, _defineProperty2().default)(this, "scope", void 0);
244
+ (0, _defineProperty2().default)(this, "options", void 0);
245
+ (0, _defineProperty2().default)(this, "mergeStatus", void 0);
246
+ (0, _defineProperty2().default)(this, "laneObjects", void 0);
247
+ (0, _defineProperty2().default)(this, "divergeData", []);
248
+ this.consumer = consumer;
249
+ this.scope = consumer.scope;
250
+ this.options = options;
251
+ this.laneObjects = this.options.lanes ? this.options.lanes.lanes : [];
252
+ }
253
+
254
+ importComponents() {
255
+ _loader().default.start(_loaderMessages().BEFORE_IMPORT_ACTION);
256
+
257
+ this.options.saveDependenciesAsComponents = this.consumer.config._saveDependenciesAsComponents;
258
+
259
+ if (this.options.lanes && !this.options.ids.length) {
260
+ return this.importObjectsOnLane();
261
+ }
262
+
263
+ if (this.options.ids.length) {
264
+ return this.importSpecificComponents();
265
+ }
266
+
267
+ return this.importAccordingToBitMap();
268
+ }
269
+
270
+ async importObjectsOnLane() {
271
+ if (!this.options.objectsOnly) {
272
+ throw new Error(`importObjectsOnLane should have objectsOnly=true`);
273
+ }
274
+
275
+ if (this.laneObjects.length > 1) {
276
+ throw new Error(`importObjectsOnLane does not support more than one lane`);
277
+ }
278
+
279
+ const lane = this.laneObjects.length ? this.laneObjects[0] : undefined;
280
+ const bitIds = await this.getBitIds();
281
+ lane ? _logger().default.debug(`importObjectsOnLane, Lane: ${lane.id()}, Ids: ${bitIds.toString()}`) : _logger().default.debug(`importObjectsOnLane, the lane does not exist on the remote. importing only the main components`);
282
+ const beforeImportVersions = await this._getCurrentVersions(bitIds);
283
+ const componentsWithDependencies = await this.consumer.importComponentsObjects(bitIds, {
284
+ allHistory: this.options.allHistory,
285
+ lane
286
+ }); // import lane components from their original scope, this way, it's possible to run diff/merge on them
287
+
288
+ if (lane) {
289
+ const mainIds = await this.scope.getDefaultLaneIdsFromLane(lane);
290
+
291
+ const mainIdsLatest = _bitId().BitIds.fromArray(mainIds.map(m => m.changeVersion(undefined))); // @todo: optimize this maybe. currently, it imports twice.
292
+ // try to make the previous `importComponentsObjectsHarmony` import the same component once from the original
293
+ // scope and once from the lane-scope.
294
+
295
+
296
+ await this.consumer.importComponentsObjects(mainIdsLatest, {
297
+ allHistory: this.options.allHistory,
298
+ ignoreMissingHead: true
299
+ });
300
+ } // merge the lane objects
301
+
302
+
303
+ const mergeAllLanesResults = await (0, _pMapSeries().default)(this.laneObjects, laneObject => this.scope.sources.mergeLane(laneObject, true));
304
+ const mergedLanes = mergeAllLanesResults.map(result => result.mergeLane);
305
+ await Promise.all(mergedLanes.map(mergedLane => this.scope.lanes.saveLane(mergedLane)));
306
+
307
+ const componentsWithDependenciesFiltered = this._filterComponentsWithLowerVersions(componentsWithDependencies);
308
+
309
+ await this._fetchDivergeData(componentsWithDependenciesFiltered);
310
+
311
+ this._throwForDivergedHistory();
312
+
313
+ await this._writeToFileSystem(componentsWithDependenciesFiltered);
314
+ await this._saveLaneDataIfNeeded(componentsWithDependenciesFiltered);
315
+ const importDetails = await this._getImportDetails(beforeImportVersions, componentsWithDependencies);
316
+ return {
317
+ dependencies: componentsWithDependenciesFiltered,
318
+ importDetails
319
+ };
320
+ }
321
+
322
+ async importSpecificComponents() {
323
+ _logger().default.debug(`importSpecificComponents, Ids: ${this.options.ids.join(', ')}`);
324
+
325
+ const bitIds = await this.getBitIds();
326
+ const beforeImportVersions = await this._getCurrentVersions(bitIds);
327
+ await this._throwForPotentialIssues(bitIds);
328
+ const componentsWithDependencies = await this.consumer.importComponentsHarmony(bitIds, true, this.laneObjects);
329
+
330
+ if (this.laneObjects && this.options.objectsOnly) {
331
+ // merge the lane objects
332
+ const mergeAllLanesResults = await (0, _pMapSeries().default)(this.laneObjects, laneObject => this.scope.sources.mergeLane(laneObject, true));
333
+ const mergedLanes = mergeAllLanesResults.map(result => result.mergeLane);
334
+ await Promise.all(mergedLanes.map(mergedLane => this.scope.lanes.saveLane(mergedLane)));
335
+ }
336
+
337
+ const componentsWithDependenciesFiltered = this._filterComponentsWithLowerVersions(componentsWithDependencies);
338
+
339
+ await this._fetchDivergeData(componentsWithDependenciesFiltered);
340
+
341
+ this._throwForDivergedHistory();
342
+
343
+ await this._writeToFileSystem(componentsWithDependenciesFiltered);
344
+ await this._saveLaneDataIfNeeded(componentsWithDependenciesFiltered);
345
+ const importDetails = await this._getImportDetails(beforeImportVersions, componentsWithDependencies);
346
+ return {
347
+ dependencies: componentsWithDependenciesFiltered,
348
+ importDetails
349
+ };
350
+ }
351
+
352
+ async _fetchDivergeData(componentsWithDependencies) {
353
+ if (this.options.objectsOnly) {
354
+ // no need for it when importing objects only. if it's enabled, in case when on a lane and a non-lane
355
+ // component is in bitmap using an older version, it throws "getDivergeData: unable to find Version X of Y"
356
+ return;
357
+ }
358
+
359
+ await Promise.all(componentsWithDependencies.map(async ({
360
+ component
361
+ }) => {
362
+ const modelComponent = await this.scope.getModelComponent(component.id);
363
+ await modelComponent.setDivergeData(this.scope.objects, undefined, false);
364
+ this.divergeData.push(modelComponent);
365
+ }));
366
+ }
367
+
368
+ _throwForDivergedHistory() {
369
+ if (this.options.merge || this.options.objectsOnly) return;
370
+ const divergedComponents = this.divergeData.filter(modelComponent => modelComponent.getDivergeData().isDiverged());
371
+
372
+ if (divergedComponents.length) {
373
+ const divergeData = divergedComponents.map(modelComponent => ({
374
+ id: modelComponent.id(),
375
+ snapsLocal: modelComponent.getDivergeData().snapsOnLocalOnly.length,
376
+ snapsRemote: modelComponent.getDivergeData().snapsOnRemoteOnly.length
377
+ }));
378
+ throw new (_componentsPendingMerge().default)(divergeData);
379
+ }
380
+ }
381
+ /**
382
+ * it can happen for example when importing a component with `--dependent` flag and the component has
383
+ * the same dependent with different versions. we only want the one with the higher version
384
+ */
385
+
386
+
387
+ _filterComponentsWithLowerVersions(componentsWithDependencies) {
388
+ return componentsWithDependencies.filter(comp => {
389
+ const sameIdHigherVersion = componentsWithDependencies.find(c => !c.component.id.isEqual(comp.component.id) && c.component.id.isEqualWithoutVersion(comp.component.id) && (0, _componentVersion().isTag)(c.component.id.version) && (0, _componentVersion().isTag)(comp.component.id.version) && _semver().default.gt(c.component.id.version, comp.component.id.version));
390
+ return !sameIdHigherVersion;
391
+ });
392
+ }
393
+ /**
394
+ * consider the following use cases:
395
+ * 1) no ids were provided. it should import all the lanes components objects AND main components objects
396
+ * (otherwise, if main components are not imported and are missing, then bit-status complains about it)
397
+ * 2) ids are provided with wildcards. we assume the user wants only the ids that are available on the lane.
398
+ * because a user may entered "bit import scope/*" and this scope has many component on the lane and many not on the lane.
399
+ * we want to bring only the components on the lane.
400
+ * 3) ids are provided without wildcards. here, the user knows exactly what's needed and it's ok to get the ids from
401
+ * main if not found on the lane.
402
+ */
403
+
404
+
405
+ async getBitIdsForLanes() {
406
+ if (!this.options.lanes) {
407
+ throw new Error(`getBitIdsForLanes: this.options.lanes must be set`);
408
+ }
409
+
410
+ const bitIdsFromLane = _bitId().BitIds.fromArray(this.laneObjects.flatMap(lane => lane.toBitIds()));
411
+
412
+ if (!this.options.ids.length) {
413
+ const mainIds = this.consumer.bitMap.getAuthoredAndImportedBitIdsOfDefaultLane();
414
+ const mainIdsToImport = mainIds.filter(id => id.hasScope() && !bitIdsFromLane.hasWithoutVersion(id));
415
+ bitIdsFromLane.push(...mainIdsToImport);
416
+ return bitIdsFromLane;
417
+ }
418
+
419
+ const idsWithWildcard = this.options.ids.filter(id => (0, _hasWildcard().default)(id));
420
+ const idsWithoutWildcard = this.options.ids.filter(id => !(0, _hasWildcard().default)(id));
421
+ const idsWithoutWildcardPreferFromLane = idsWithoutWildcard.map(idStr => {
422
+ const id = _bitId().BitId.parse(idStr, true);
423
+
424
+ const fromLane = bitIdsFromLane.searchWithoutVersion(id);
425
+ return fromLane || id;
426
+ });
427
+ const bitIds = [...idsWithoutWildcardPreferFromLane];
428
+
429
+ if (!idsWithWildcard) {
430
+ return bitIds;
431
+ }
432
+
433
+ await (0, _pMapSeries().default)(idsWithWildcard, async idStr => {
434
+ const idsFromRemote = await (0, _listScope().getRemoteBitIdsByWildcards)(idStr);
435
+ const existingOnLanes = idsFromRemote.filter(id => bitIdsFromLane.hasWithoutVersion(id));
436
+
437
+ if (!existingOnLanes.length) {
438
+ throw new (_bitError().BitError)(`the id with the the wildcard "${idStr}" has been parsed to multiple component ids.
439
+ however, none of them existing on the lane "${this.laneObjects.map(l => l.name).join(', ')}"
440
+ in case you intend to import these components from main, please run the following:
441
+ bit import ${idsFromRemote.map(id => id.toStringWithoutVersion()).join(' ')}`);
442
+ }
443
+
444
+ bitIds.push(...existingOnLanes);
445
+ });
446
+ return bitIds;
447
+ }
448
+
449
+ async getBitIdsForNonLanes() {
450
+ const bitIds = [];
451
+ await Promise.all(this.options.ids.map(async idStr => {
452
+ if ((0, _hasWildcard().default)(idStr)) {
453
+ const ids = await (0, _listScope().getRemoteBitIdsByWildcards)(idStr);
454
+
455
+ _loader().default.start(_loaderMessages().BEFORE_IMPORT_ACTION); // it stops the previous loader of BEFORE_REMOTE_LIST
456
+
457
+
458
+ bitIds.push(...ids);
459
+ } else {
460
+ bitIds.push(_bitId().BitId.parse(idStr, true)); // we don't support importing without a scope name
461
+ }
462
+ }));
463
+ return bitIds;
464
+ }
465
+
466
+ async getBitIds() {
467
+ const bitIds = this.options.lanes ? await this.getBitIdsForLanes() : await this.getBitIdsForNonLanes();
468
+
469
+ if (this.options.importDependenciesDirectly || this.options.importDependents) {
470
+ const graphs = await this._getComponentsGraphs(bitIds);
471
+
472
+ if (this.options.importDependenciesDirectly) {
473
+ const dependenciesIds = this._getDependenciesFromGraph(bitIds, graphs);
474
+
475
+ bitIds.push(...dependenciesIds);
476
+ }
477
+
478
+ if (this.options.importDependents) {
479
+ const dependentsIds = this._getDependentsFromGraph(bitIds, graphs);
480
+
481
+ bitIds.push(...dependentsIds);
482
+ }
483
+ }
484
+
485
+ return _bitId().BitIds.uniqFromArray(bitIds);
486
+ }
487
+
488
+ _getDependenciesFromGraph(bitIds, graphs) {
489
+ const dependencies = bitIds.map(bitId => {
490
+ const componentGraph = graphs.find(graph => graph.scopeName === bitId.scope);
491
+
492
+ if (!componentGraph) {
493
+ throw new Error(`unable to find a graph for ${bitId.toString()}`);
494
+ }
495
+
496
+ const dependenciesInfo = componentGraph.getDependenciesInfo(bitId);
497
+ return dependenciesInfo.map(d => d.id);
498
+ });
499
+ return _ramda().default.flatten(dependencies);
500
+ }
501
+
502
+ _getDependentsFromGraph(bitIds, graphs) {
503
+ const dependents = bitIds.map(bitId => {
504
+ const componentGraph = graphs.find(graph => graph.scopeName === bitId.scope);
505
+
506
+ if (!componentGraph) {
507
+ throw new Error(`unable to find a graph for ${bitId.toString()}`);
508
+ }
509
+
510
+ const dependentsInfo = componentGraph.getDependentsInfo(bitId);
511
+ return dependentsInfo.map(d => d.id);
512
+ });
513
+ return _ramda().default.flatten(dependents);
514
+ }
515
+
516
+ async _getComponentsGraphs(bitIds) {
517
+ const remotes = await (0, _scopeRemotes().getScopeRemotes)(this.consumer.scope);
518
+ return remotes.scopeGraphs(bitIds, this.consumer.scope);
519
+ }
520
+
521
+ async importAccordingToBitMap() {
522
+ this.options.objectsOnly = !this.options.merge && !this.options.override;
523
+ const componentsIdsToImport = this.getIdsToImportFromBitmap();
524
+
525
+ if (_ramda().default.isEmpty(componentsIdsToImport)) {
526
+ if (!this.options.withEnvironments) {
527
+ throw new (_exceptions().NothingToImport)();
528
+ }
529
+ }
530
+
531
+ await this._throwForModifiedOrNewComponents(componentsIdsToImport);
532
+ const beforeImportVersions = await this._getCurrentVersions(componentsIdsToImport);
533
+ let componentsAndDependencies = [];
534
+
535
+ if (componentsIdsToImport.length) {
536
+ // change all ids version to 'latest'. otherwise, it tries to import local tags/snaps from a remote
537
+ // const idsWithLatestVersion = componentsIdsToImport.toVersionLatest();
538
+ if (!this.options.objectsOnly) {
539
+ throw new Error(`bit import with no ids and --merge flag was not implemented yet`);
540
+ }
541
+
542
+ componentsAndDependencies = await this.consumer.importComponentsObjects(componentsIdsToImport, {
543
+ fromOriginalScope: this.options.fromOriginalScope,
544
+ allHistory: this.options.allHistory
545
+ });
546
+ await this._writeToFileSystem(componentsAndDependencies);
547
+ }
548
+
549
+ const importDetails = await this._getImportDetails(beforeImportVersions, componentsAndDependencies);
550
+ return {
551
+ dependencies: componentsAndDependencies,
552
+ importDetails
553
+ };
554
+ }
555
+
556
+ getIdsToImportFromBitmap() {
557
+ const authoredExportedComponents = this.consumer.bitMap.getExportedComponents();
558
+ return _bitId().BitIds.fromArray(authoredExportedComponents);
559
+ }
560
+ /**
561
+ * author might require bit-components that were installed via a package-manager. in that case,
562
+ * the objects are not imported until bit build or bit tag was running. this makes sure to get
563
+ * the objects on 'bit import', so then in the UI, they'll be shown nicely.
564
+ */
565
+
566
+
567
+ async getIdsOfDepsInstalledAsPackages() {
568
+ if (!this.options.objectsOnly) {
569
+ // this is needed only when importing objects. we don't want these components to be written to the fs
570
+ return [];
571
+ }
572
+
573
+ const authoredNonExportedComponentsIds = this.consumer.bitMap.getAuthoredNonExportedComponents();
574
+ const {
575
+ components: authoredNonExportedComponents
576
+ } = await this.consumer.loadComponents(_bitId().BitIds.fromArray(authoredNonExportedComponentsIds), false);
577
+
578
+ const dependencies = _ramda().default.flatten(authoredNonExportedComponents.map(c => c.getAllDependenciesIds()));
579
+
580
+ const missingDeps = [];
581
+ await Promise.all(dependencies.map(async dep => {
582
+ if (!dep.hasScope()) return;
583
+ const isInScope = await this.scope.isComponentInScope(dep);
584
+ if (!isInScope) missingDeps.push(dep);
585
+ }));
586
+ return missingDeps;
587
+ }
588
+
589
+ async _getCurrentVersions(ids) {
590
+ const versionsP = ids.map(async id => {
591
+ const modelComponent = await this.consumer.scope.getModelComponentIfExist(id.changeVersion(undefined));
592
+ const idStr = id.toStringWithoutVersion();
593
+ if (!modelComponent) return [idStr, []];
594
+ return [idStr, modelComponent.listVersions()];
595
+ });
596
+ const versions = await Promise.all(versionsP);
597
+ return _ramda().default.fromPairs(versions);
598
+ }
599
+ /**
600
+ * get import details, includes the diff between the versions array before import and after import
601
+ */
602
+
603
+
604
+ async _getImportDetails(currentVersions, components) {
605
+ const detailsP = components.map(async component => {
606
+ const id = component.component.id;
607
+ const idStr = id.toStringWithoutVersion();
608
+ const beforeImportVersions = currentVersions[idStr];
609
+
610
+ if (!beforeImportVersions) {
611
+ throw new Error(`_getImportDetails failed finding ${idStr} in currentVersions, which has ${Object.keys(currentVersions).join(', ')}`);
612
+ }
613
+
614
+ const modelComponent = await this.consumer.scope.getModelComponentIfExist(id);
615
+ if (!modelComponent) throw new (_showDoctorError().default)(`imported component ${idStr} was not found in the model`);
616
+ const afterImportVersions = modelComponent.listVersions();
617
+
618
+ const versionDifference = _ramda().default.difference(afterImportVersions, beforeImportVersions);
619
+
620
+ const getStatus = () => {
621
+ if (!versionDifference.length) return 'up to date';
622
+ if (!beforeImportVersions.length) return 'added';
623
+ return 'updated';
624
+ };
625
+
626
+ const filesStatus = this.mergeStatus && this.mergeStatus[idStr] ? this.mergeStatus[idStr] : null;
627
+ const deprecated = await modelComponent.isDeprecated(this.scope.objects);
628
+ const removed = component.component.removed;
629
+ const latestVersion = modelComponent.latest();
630
+ return {
631
+ id: idStr,
632
+ versions: versionDifference,
633
+ latestVersion: versionDifference.includes(latestVersion) ? latestVersion : null,
634
+ status: getStatus(),
635
+ filesStatus,
636
+ missingDeps: component.missingDependencies,
637
+ deprecated,
638
+ removed
639
+ };
640
+ });
641
+ return Promise.all(detailsP);
642
+ }
643
+
644
+ async _throwForPotentialIssues(ids) {
645
+ await this._throwForModifiedOrNewComponents(ids);
646
+
647
+ this._throwForDifferentComponentWithSameName(ids);
648
+ }
649
+
650
+ async _throwForModifiedOrNewComponents(ids) {
651
+ // the typical objectsOnly option is when a user cloned a project with components tagged to the source code, but
652
+ // doesn't have the model objects. in that case, calling getComponentStatusById() may return an error as it relies
653
+ // on the model objects when there are dependencies
654
+ if (this.options.override || this.options.objectsOnly || this.options.merge) return;
655
+ const componentsStatuses = await this.consumer.getManyComponentsStatuses(ids);
656
+ const modifiedComponents = componentsStatuses.filter(({
657
+ status
658
+ }) => status.modified || status.newlyCreated).map(c => c.id);
659
+
660
+ if (modifiedComponents.length) {
661
+ throw new (_generalError().default)(_chalk().default.yellow(`unable to import the following components due to local changes, use --merge flag to merge your local changes or --override to override them\n${modifiedComponents.join('\n')} `));
662
+ }
663
+ }
664
+ /**
665
+ * Model Component id() calculation uses id.toString() for the hash.
666
+ * If an imported component has scopereadonly name equals to a local name, both will have the exact same
667
+ * hash and they'll override each other.
668
+ */
669
+
670
+
671
+ _throwForDifferentComponentWithSameName(ids) {
672
+ ids.forEach(id => {
673
+ const existingId = this.consumer.getParsedIdIfExist(id.toStringWithoutVersion());
674
+
675
+ if (existingId && !existingId.hasScope()) {
676
+ throw new (_generalError().default)(`unable to import ${id.toString()}. the component name conflicted with your local component with the same name.
677
+ it's fine to have components with the same name as long as their scope names are different.
678
+ Make sure to export your component first to get a scope and then try importing again`);
679
+ }
680
+ });
681
+ }
682
+
683
+ async _getMergeStatus(componentWithDependencies) {
684
+ const component = componentWithDependencies.component;
685
+ const componentStatus = await this.consumer.getComponentStatusById(component.id);
686
+ const mergeStatus = {
687
+ componentWithDependencies,
688
+ mergeResults: null
689
+ };
690
+ if (!componentStatus.modified) return mergeStatus;
691
+ const componentModel = await this.consumer.scope.getModelComponent(component.id);
692
+ const existingBitMapBitId = this.consumer.bitMap.getBitId(component.id, {
693
+ ignoreVersion: true
694
+ });
695
+ const fsComponent = await this.consumer.loadComponent(existingBitMapBitId);
696
+ const currentlyUsedVersion = existingBitMapBitId.version; // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
697
+
698
+ const baseComponent = await componentModel.loadVersion(currentlyUsedVersion, this.consumer.scope.objects);
699
+ const otherComponent = await componentModel.loadVersion( // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
700
+ component.id.version, this.consumer.scope.objects);
701
+ const mergeResults = await (0, _mergeVersion().threeWayMerge)({
702
+ consumer: this.consumer,
703
+ otherComponent,
704
+ otherLabel: component.id.version,
705
+ currentComponent: fsComponent,
706
+ currentLabel: `${currentlyUsedVersion} modified`,
707
+ baseComponent
708
+ });
709
+ mergeStatus.mergeResults = mergeResults;
710
+ return mergeStatus;
711
+ }
712
+ /**
713
+ * 1) when there are conflicts and the strategy is "ours", don't write the imported component to
714
+ * the filesystem, only update bitmap.
715
+ *
716
+ * 2) when there are conflicts and the strategy is "theirs", override the local changes by the
717
+ * imported component. (similar to --override)
718
+ *
719
+ * 3) when there is no conflict or there are conflicts and the strategy is manual, write the files
720
+ * according to the merge result. (done by applyModifiedVersion())
721
+ */
722
+
723
+
724
+ _updateComponentFilesPerMergeStrategy(componentMergeStatus) {
725
+ const mergeResults = componentMergeStatus.mergeResults;
726
+ if (!mergeResults) return null;
727
+ const component = componentMergeStatus.componentWithDependencies.component;
728
+ const files = component.files;
729
+
730
+ if (mergeResults.hasConflicts && this.options.mergeStrategy === _mergeVersion().MergeOptions.ours) {
731
+ const filesStatus = {}; // don't write the files to the filesystem, only bump the bitmap version.
732
+
733
+ files.forEach(file => {
734
+ // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
735
+ filesStatus[(0, _utils().pathNormalizeToLinux)(file.relative)] = _mergeVersion().FileStatus.unchanged;
736
+ });
737
+ this.consumer.bitMap.updateComponentId(component.id);
738
+ this.consumer.bitMap.hasChanged = true;
739
+ return filesStatus;
740
+ }
741
+
742
+ if (mergeResults.hasConflicts && this.options.mergeStrategy === _mergeVersion().MergeOptions.theirs) {
743
+ const filesStatus = {}; // the local changes will be overridden (as if the user entered --override flag for this component)
744
+
745
+ files.forEach(file => {
746
+ // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
747
+ filesStatus[(0, _utils().pathNormalizeToLinux)(file.relative)] = _mergeVersion().FileStatus.updated;
748
+ });
749
+ return filesStatus;
750
+ }
751
+
752
+ const {
753
+ filesStatus,
754
+ modifiedFiles
755
+ } = (0, _checkoutVersion().applyModifiedVersion)(component.files, mergeResults, this.options.mergeStrategy);
756
+ component.files = modifiedFiles;
757
+ return filesStatus;
758
+ }
759
+ /**
760
+ * update the component files if they are modified and there is a merge strategy.
761
+ * returns only the components that need to be written to the filesystem
762
+ */
763
+
764
+
765
+ async updateAllComponentsAccordingToMergeStrategy(componentsWithDependencies) {
766
+ if (!this.options.merge) return componentsWithDependencies;
767
+ const componentsStatusP = componentsWithDependencies.map(componentWithDependencies => {
768
+ return this._getMergeStatus(componentWithDependencies);
769
+ });
770
+ const componentsStatus = await Promise.all(componentsStatusP);
771
+ const componentWithConflict = componentsStatus.find(component => component.mergeResults && component.mergeResults.hasConflicts);
772
+
773
+ if (componentWithConflict && !this.options.mergeStrategy) {
774
+ this.options.mergeStrategy = await (0, _mergeVersion().getMergeStrategyInteractive)();
775
+ }
776
+
777
+ this.mergeStatus = {};
778
+ const componentsToWrite = componentsStatus.map(componentStatus => {
779
+ const filesStatus = this._updateComponentFilesPerMergeStrategy(componentStatus);
780
+
781
+ const componentWithDependencies = componentStatus.componentWithDependencies;
782
+ if (!filesStatus) return componentWithDependencies;
783
+ this.mergeStatus[componentWithDependencies.component.id.toStringWithoutVersion()] = filesStatus;
784
+ const unchangedFiles = Object.keys(filesStatus).filter(file => filesStatus[file] === _mergeVersion().FileStatus.unchanged);
785
+
786
+ if (unchangedFiles.length === Object.keys(filesStatus).length) {
787
+ // all files are unchanged
788
+ return null;
789
+ }
790
+
791
+ return componentWithDependencies;
792
+ });
793
+
794
+ const removeNulls = _ramda().default.reject(_ramda().default.isNil);
795
+
796
+ return removeNulls(componentsToWrite);
797
+ }
798
+
799
+ _shouldSaveLaneData() {
800
+ if (this.options.objectsOnly) {
801
+ return false;
802
+ }
803
+
804
+ return this.consumer.isOnLane();
805
+ }
806
+
807
+ async _saveLaneDataIfNeeded(componentsWithDependencies) {
808
+ if (!this._shouldSaveLaneData()) {
809
+ return;
810
+ }
811
+
812
+ const currentLane = await this.consumer.getCurrentLaneObject();
813
+
814
+ if (!currentLane) {
815
+ return; // user on main
816
+ }
817
+
818
+ const idsFromRemoteLanes = _bitId().BitIds.fromArray(this.laneObjects.flatMap(lane => lane.toBitIds()));
819
+
820
+ const components = componentsWithDependencies.map(c => c.component);
821
+ await Promise.all(components.map(async comp => {
822
+ const existOnRemoteLane = idsFromRemoteLanes.has(comp.id);
823
+
824
+ if (!existOnRemoteLane && !this.options.saveInLane) {
825
+ this.consumer.bitMap.setComponentProp(comp.id, 'onLanesOnly', false);
826
+ return;
827
+ }
828
+
829
+ const modelComponent = await this.scope.getModelComponent(comp.id);
830
+ const ref = modelComponent.getRef(comp.id.version);
831
+ if (!ref) throw new Error(`_saveLaneDataIfNeeded unable to get ref for ${comp.id.toString()}`);
832
+ currentLane.addComponent({
833
+ id: comp.id,
834
+ head: ref
835
+ });
836
+ }));
837
+ await this.scope.lanes.saveLane(currentLane);
838
+ }
839
+
840
+ async _writeToFileSystem(componentsWithDependencies) {
841
+ if (this.options.objectsOnly) {
842
+ return;
843
+ }
844
+
845
+ const componentsToWrite = await this.updateAllComponentsAccordingToMergeStrategy(componentsWithDependencies);
846
+ const manyComponentsWriter = new (_manyComponentsWriter().default)({
847
+ consumer: this.consumer,
848
+ componentsWithDependencies: componentsToWrite,
849
+ writeToPath: this.options.writeToPath,
850
+ writePackageJson: this.options.writePackageJson,
851
+ addToRootPackageJson: this.options.writePackageJson,
852
+ // no point to add to root if it doesn't have package.json
853
+ writeConfig: this.options.writeConfig,
854
+ writeDists: this.options.writeDists,
855
+ installNpmPackages: this.options.installNpmPackages,
856
+ verbose: this.options.verbose,
857
+ override: this.options.override
858
+ });
859
+ await manyComponentsWriter.writeAll();
860
+ }
861
+
862
+ }
863
+
864
+ exports.default = ImportComponents;
865
+
866
+ //# sourceMappingURL=import-components.js.map