@teambit/objects 0.0.19

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.
Files changed (123) hide show
  1. package/artifacts/__bit_junit.xml +68 -0
  2. package/artifacts/preview/teambit_scope_objects-preview.js +1 -0
  3. package/dist/fixtures/version-model-extended.json +48 -0
  4. package/dist/fixtures/version-model-object.json +87 -0
  5. package/dist/index.d.ts +19 -0
  6. package/dist/index.js +371 -0
  7. package/dist/index.js.map +1 -0
  8. package/dist/models/dependencies-graph.d.ts +45 -0
  9. package/dist/models/dependencies-graph.js +106 -0
  10. package/dist/models/dependencies-graph.js.map +1 -0
  11. package/dist/models/detach-heads.d.ts +25 -0
  12. package/dist/models/detach-heads.js +84 -0
  13. package/dist/models/detach-heads.js.map +1 -0
  14. package/dist/models/export-metadata.d.ts +24 -0
  15. package/dist/models/export-metadata.js +76 -0
  16. package/dist/models/export-metadata.js.map +1 -0
  17. package/dist/models/index.d.ts +10 -0
  18. package/dist/models/index.js +125 -0
  19. package/dist/models/index.js.map +1 -0
  20. package/dist/models/lane-history.d.ts +40 -0
  21. package/dist/models/lane-history.js +117 -0
  22. package/dist/models/lane-history.js.map +1 -0
  23. package/dist/models/lane.d.ts +124 -0
  24. package/dist/models/lane.js +463 -0
  25. package/dist/models/lane.js.map +1 -0
  26. package/dist/models/model-component.d.ts +317 -0
  27. package/dist/models/model-component.js +1365 -0
  28. package/dist/models/model-component.js.map +1 -0
  29. package/dist/models/model-component.spec.d.ts +1 -0
  30. package/dist/models/model-component.spec.js +71 -0
  31. package/dist/models/model-component.spec.js.map +1 -0
  32. package/dist/models/scopeMeta.d.ts +20 -0
  33. package/dist/models/scopeMeta.js +71 -0
  34. package/dist/models/scopeMeta.js.map +1 -0
  35. package/dist/models/source.d.ts +10 -0
  36. package/dist/models/source.js +43 -0
  37. package/dist/models/source.js.map +1 -0
  38. package/dist/models/symlink.d.ts +30 -0
  39. package/dist/models/symlink.js +91 -0
  40. package/dist/models/symlink.js.map +1 -0
  41. package/dist/models/version-history.d.ts +59 -0
  42. package/dist/models/version-history.js +285 -0
  43. package/dist/models/version-history.js.map +1 -0
  44. package/dist/models/version.d.ts +279 -0
  45. package/dist/models/version.js +777 -0
  46. package/dist/models/version.js.map +1 -0
  47. package/dist/models/version.spec.d.ts +1 -0
  48. package/dist/models/version.spec.js +340 -0
  49. package/dist/models/version.spec.js.map +1 -0
  50. package/dist/objects/bit-object-list.d.ts +24 -0
  51. package/dist/objects/bit-object-list.js +65 -0
  52. package/dist/objects/bit-object-list.js.map +1 -0
  53. package/dist/objects/index.d.ts +5 -0
  54. package/dist/objects/index.js +60 -0
  55. package/dist/objects/index.js.map +1 -0
  56. package/dist/objects/object-list-to-graph.d.ts +13 -0
  57. package/dist/objects/object-list-to-graph.js +93 -0
  58. package/dist/objects/object-list-to-graph.js.map +1 -0
  59. package/dist/objects/object-list.d.ts +52 -0
  60. package/dist/objects/object-list.js +369 -0
  61. package/dist/objects/object-list.js.map +1 -0
  62. package/dist/objects/object.d.ts +35 -0
  63. package/dist/objects/object.js +190 -0
  64. package/dist/objects/object.js.map +1 -0
  65. package/dist/objects/objects-readable-generator.d.ts +31 -0
  66. package/dist/objects/objects-readable-generator.js +192 -0
  67. package/dist/objects/objects-readable-generator.js.map +1 -0
  68. package/dist/objects/raw-object.d.ts +23 -0
  69. package/dist/objects/raw-object.js +155 -0
  70. package/dist/objects/raw-object.js.map +1 -0
  71. package/dist/objects/ref.d.ts +14 -0
  72. package/dist/objects/ref.js +45 -0
  73. package/dist/objects/ref.js.map +1 -0
  74. package/dist/objects/repository-hooks.d.ts +4 -0
  75. package/dist/objects/repository-hooks.js +56 -0
  76. package/dist/objects/repository-hooks.js.map +1 -0
  77. package/dist/objects/repository.d.ts +148 -0
  78. package/dist/objects/repository.js +842 -0
  79. package/dist/objects/repository.js.map +1 -0
  80. package/dist/objects/scope-index.d.ts +73 -0
  81. package/dist/objects/scope-index.js +251 -0
  82. package/dist/objects/scope-index.js.map +1 -0
  83. package/dist/objects/scope-index.spec.d.ts +1 -0
  84. package/dist/objects/scope-index.spec.js +152 -0
  85. package/dist/objects/scope-index.spec.js.map +1 -0
  86. package/dist/objects.aspect.d.ts +2 -0
  87. package/dist/objects.aspect.js +18 -0
  88. package/dist/objects.aspect.js.map +1 -0
  89. package/dist/objects.main.runtime.d.ts +7 -0
  90. package/dist/objects.main.runtime.js +36 -0
  91. package/dist/objects.main.runtime.js.map +1 -0
  92. package/dist/preview-1736824735631.js +7 -0
  93. package/fixtures/version-model-extended.json +48 -0
  94. package/fixtures/version-model-object.json +87 -0
  95. package/models/dependencies-graph.ts +119 -0
  96. package/models/detach-heads.ts +79 -0
  97. package/models/export-metadata.ts +57 -0
  98. package/models/index.ts +11 -0
  99. package/models/lane-history.ts +106 -0
  100. package/models/lane.ts +367 -0
  101. package/models/model-component.spec.ts +55 -0
  102. package/models/model-component.ts +1367 -0
  103. package/models/scopeMeta.ts +60 -0
  104. package/models/source.ts +32 -0
  105. package/models/symlink.ts +66 -0
  106. package/models/version-history.ts +266 -0
  107. package/models/version.spec.ts +288 -0
  108. package/models/version.ts +818 -0
  109. package/objects/bit-object-list.ts +59 -0
  110. package/objects/index.ts +6 -0
  111. package/objects/object-list-to-graph.ts +69 -0
  112. package/objects/object-list.ts +313 -0
  113. package/objects/object.ts +153 -0
  114. package/objects/objects-readable-generator.ts +167 -0
  115. package/objects/raw-object.ts +142 -0
  116. package/objects/ref.ts +45 -0
  117. package/objects/repository-hooks.ts +42 -0
  118. package/objects/repository.ts +753 -0
  119. package/objects/scope-index.spec.ts +95 -0
  120. package/objects/scope-index.ts +192 -0
  121. package/package.json +98 -0
  122. package/types/asset.d.ts +41 -0
  123. package/types/style.d.ts +42 -0
@@ -0,0 +1,1365 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = exports.VERSION_ZERO = void 0;
7
+ function _lodash() {
8
+ const data = require("lodash");
9
+ _lodash = function () {
10
+ return data;
11
+ };
12
+ return data;
13
+ }
14
+ function _asyncMutex() {
15
+ const data = require("async-mutex");
16
+ _asyncMutex = function () {
17
+ return data;
18
+ };
19
+ return data;
20
+ }
21
+ function semver() {
22
+ const data = _interopRequireWildcard(require("semver"));
23
+ semver = function () {
24
+ return data;
25
+ };
26
+ return data;
27
+ }
28
+ function _componentVersion() {
29
+ const data = require("@teambit/component-version");
30
+ _componentVersion = function () {
31
+ return data;
32
+ };
33
+ return data;
34
+ }
35
+ function _bitError() {
36
+ const data = require("@teambit/bit-error");
37
+ _bitError = function () {
38
+ return data;
39
+ };
40
+ return data;
41
+ }
42
+ function _laneId() {
43
+ const data = require("@teambit/lane-id");
44
+ _laneId = function () {
45
+ return data;
46
+ };
47
+ return data;
48
+ }
49
+ function _componentId() {
50
+ const data = require("@teambit/component-id");
51
+ _componentId = function () {
52
+ return data;
53
+ };
54
+ return data;
55
+ }
56
+ function _pMapSeries() {
57
+ const data = _interopRequireDefault(require("p-map-series"));
58
+ _pMapSeries = function () {
59
+ return data;
60
+ };
61
+ return data;
62
+ }
63
+ function _toolboxArray() {
64
+ const data = require("@teambit/toolbox.array.duplications-finder");
65
+ _toolboxArray = function () {
66
+ return data;
67
+ };
68
+ return data;
69
+ }
70
+ function _legacyBitId() {
71
+ const data = require("@teambit/legacy-bit-id");
72
+ _legacyBitId = function () {
73
+ return data;
74
+ };
75
+ return data;
76
+ }
77
+ function _legacy() {
78
+ const data = require("@teambit/legacy.constants");
79
+ _legacy = function () {
80
+ return data;
81
+ };
82
+ return data;
83
+ }
84
+ function _legacy2() {
85
+ const data = require("@teambit/legacy.consumer-component");
86
+ _legacy2 = function () {
87
+ return data;
88
+ };
89
+ return data;
90
+ }
91
+ function _component() {
92
+ const data = require("@teambit/component.sources");
93
+ _component = function () {
94
+ return data;
95
+ };
96
+ return data;
97
+ }
98
+ function _legacy3() {
99
+ const data = require("@teambit/legacy.consumer-config");
100
+ _legacy3 = function () {
101
+ return data;
102
+ };
103
+ return data;
104
+ }
105
+ function _legacyCli() {
106
+ const data = require("@teambit/legacy.cli.error");
107
+ _legacyCli = function () {
108
+ return data;
109
+ };
110
+ return data;
111
+ }
112
+ function _legacy4() {
113
+ const data = require("@teambit/legacy.logger");
114
+ _legacy4 = function () {
115
+ return data;
116
+ };
117
+ return data;
118
+ }
119
+ function _legacy5() {
120
+ const data = require("@teambit/legacy.utils");
121
+ _legacy5 = function () {
122
+ return data;
123
+ };
124
+ return data;
125
+ }
126
+ function _pkgModules() {
127
+ const data = require("@teambit/pkg.modules.semver-helper");
128
+ _pkgModules = function () {
129
+ return data;
130
+ };
131
+ return data;
132
+ }
133
+ function _component2() {
134
+ const data = require("@teambit/component.snap-distance");
135
+ _component2 = function () {
136
+ return data;
137
+ };
138
+ return data;
139
+ }
140
+ function _legacy6() {
141
+ const data = require("@teambit/legacy.scope");
142
+ _legacy6 = function () {
143
+ return data;
144
+ };
145
+ return data;
146
+ }
147
+ function _objects() {
148
+ const data = require("../objects");
149
+ _objects = function () {
150
+ return data;
151
+ };
152
+ return data;
153
+ }
154
+ function _scopeMeta() {
155
+ const data = _interopRequireDefault(require("./scopeMeta"));
156
+ _scopeMeta = function () {
157
+ return data;
158
+ };
159
+ return data;
160
+ }
161
+ function _versionHistory() {
162
+ const data = _interopRequireDefault(require("./version-history"));
163
+ _versionHistory = function () {
164
+ return data;
165
+ };
166
+ return data;
167
+ }
168
+ function _detachHeads() {
169
+ const data = require("./detach-heads");
170
+ _detachHeads = function () {
171
+ return data;
172
+ };
173
+ return data;
174
+ }
175
+ function _harmonyModules() {
176
+ const data = require("@teambit/harmony.modules.feature-toggle");
177
+ _harmonyModules = function () {
178
+ return data;
179
+ };
180
+ return data;
181
+ }
182
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
183
+ 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); }
184
+ 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 && {}.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; }
185
+ 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; }
186
+ 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; }
187
+ function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
188
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
189
+ 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); }
190
+ const VERSION_ZERO = exports.VERSION_ZERO = '0.0.0';
191
+
192
+ /**
193
+ * we can't rename the class as ModelComponent because old components are already saved in the model
194
+ * with 'Component' in their headers. see object-registrar.types()
195
+ */
196
+ // TODO: FIX me .parser
197
+ // @ts-ignore
198
+ class Component extends _objects().BitObject {
199
+ constructor(props) {
200
+ super();
201
+ _defineProperty(this, "scope", void 0);
202
+ _defineProperty(this, "name", void 0);
203
+ _defineProperty(this, "versions", void 0);
204
+ _defineProperty(this, "orphanedVersions", void 0);
205
+ _defineProperty(this, "lang", void 0);
206
+ /**
207
+ * @deprecated moved to the Version object inside teambit/deprecation aspect
208
+ */
209
+ _defineProperty(this, "deprecated", void 0);
210
+ _defineProperty(this, "bindingPrefix", void 0);
211
+ /**
212
+ * @deprecated since 0.12.6 (long long ago :) probably can be removed)
213
+ */
214
+ _defineProperty(this, "local", void 0);
215
+ _defineProperty(this, "state", void 0);
216
+ _defineProperty(this, "scopesList", void 0);
217
+ _defineProperty(this, "head", void 0);
218
+ _defineProperty(this, "remoteHead", void 0);
219
+ // doesn't get saved in the scope, used to easier access the remote main head
220
+ /**
221
+ * doesn't get saved in the scope, used to easier access the local snap head data
222
+ * when checked out to a lane, this prop is either Ref or null. otherwise (when on main), this prop is undefined.
223
+ */
224
+ _defineProperty(this, "laneHeadLocal", void 0);
225
+ /**
226
+ * doesn't get saved in the scope, used to easier access the remote snap head data
227
+ * when checked out to a lane, this prop is either Ref or null. otherwise (when on main), this prop is undefined.
228
+ */
229
+ _defineProperty(this, "laneHeadRemote", void 0);
230
+ /**
231
+ * when checked out to a lane, calculate what should be the head on the remote.
232
+ * if the laneHeadRemote is null, for example, when the lane is new, then used the the lane it was forked from.
233
+ * it no head is found on the lane/forked, then use the component.head.
234
+ */
235
+ _defineProperty(this, "calculatedRemoteHeadWhenOnLane", void 0);
236
+ _defineProperty(this, "laneId", void 0);
237
+ // doesn't get saved in the scope.
238
+ _defineProperty(this, "laneDataIsPopulated", false);
239
+ // doesn't get saved in the scope, used to improve performance of loading the lane data
240
+ _defineProperty(this, "schema", void 0);
241
+ _defineProperty(this, "detachedHeads", void 0);
242
+ _defineProperty(this, "divergeData", void 0);
243
+ _defineProperty(this, "populateVersionHistoryMutex", new (_asyncMutex().Mutex)());
244
+ if (!props.name) throw new TypeError('Model Component constructor expects to get a name parameter');
245
+ if (!props.scope) {
246
+ throw new (_legacy6().BitIdCompIdError)(props.name);
247
+ }
248
+ this.scope = props.scope;
249
+ this.name = props.name;
250
+ this.versions = props.versions || {};
251
+ this.orphanedVersions = props.orphanedVersions || {};
252
+ this.lang = props.lang || _legacy().DEFAULT_LANGUAGE;
253
+ this.deprecated = props.deprecated || false;
254
+ this.bindingPrefix = props.bindingPrefix || (0, _legacy3().getBindingPrefixByDefaultScope)(props.scope);
255
+ this.state = props.state || {};
256
+ this.scopesList = props.scopesList || [];
257
+ this.head = props.head;
258
+ this.schema = props.schema;
259
+ this.detachedHeads = props.detachedHeads || new (_detachHeads().DetachedHeads)();
260
+ }
261
+ get versionArray() {
262
+ return Object.values(this.versions);
263
+ }
264
+ setVersion(tag, ref) {
265
+ this.versions[tag] = ref;
266
+ delete this.orphanedVersions[tag]; // just in case it's there.
267
+ }
268
+ setOrphanedVersion(tag, ref) {
269
+ if (this.versions[tag]) {
270
+ throw new Error(`unable to save orphanedVersion "${tag}" for "${this.id()}" because this tag is already part of the versions prop`);
271
+ }
272
+ this.orphanedVersions[tag] = ref;
273
+ }
274
+ getRef(version) {
275
+ if ((0, _componentVersion().isTag)(version)) {
276
+ return this.versionsIncludeOrphaned[version];
277
+ }
278
+ if ((0, _componentVersion().isHash)(version)) {
279
+ return new (_objects().Ref)(version);
280
+ }
281
+ return null;
282
+ }
283
+ getHeadStr() {
284
+ return this.head ? this.head.toString() : null;
285
+ }
286
+ getHead() {
287
+ return this.head;
288
+ }
289
+
290
+ /**
291
+ * returns the head hash. regardless of whether current lane is the default or not.
292
+ * if on a lane, it returns the head of the component on the lane.
293
+ */
294
+ getHeadRegardlessOfLane() {
295
+ return this.laneHeadLocal || this.getHead();
296
+ }
297
+ getHeadAsTagIfExist() {
298
+ if (!this.head) return undefined;
299
+ return this.getTagOfRefIfExists(this.head) || this.head.toString();
300
+ }
301
+ hasHead() {
302
+ return Boolean(this.head);
303
+ }
304
+ setHead(head) {
305
+ this.head = head;
306
+ }
307
+ listVersions(sort) {
308
+ const versions = Object.keys(this.versions);
309
+ if (!sort) return versions;
310
+ if (sort === 'ASC') {
311
+ return versions.sort(semver().compare);
312
+ }
313
+ return versions.sort(semver().compare).reverse();
314
+ }
315
+ listVersionsIncludeOrphaned(sort) {
316
+ const versions = Object.keys(this.versionsIncludeOrphaned);
317
+ if (!sort) return versions;
318
+ if (sort === 'ASC') {
319
+ return versions.sort(semver().compare);
320
+ }
321
+ return versions.sort(semver().compare).reverse();
322
+ }
323
+ async hasVersion(version, repo, includeOrphaned = true) {
324
+ if ((0, _componentVersion().isTag)(version)) {
325
+ return includeOrphaned ? this.hasTagIncludeOrphaned(version) : this.hasTag(version);
326
+ }
327
+ const head = this.getHeadRegardlessOfLane();
328
+ if (!head) {
329
+ return false;
330
+ }
331
+ const versionParents = await (0, _component2().getAllVersionParents)({
332
+ repo,
333
+ modelComponent: this,
334
+ heads: [head]
335
+ });
336
+ // we use "startsWith" because it can be a short hash
337
+ return versionParents.map(v => v.hash).some(hash => hash.toString().startsWith(version));
338
+ }
339
+ hasTag(version) {
340
+ return Boolean(this.versions[version]);
341
+ }
342
+ get versionsIncludeOrphaned() {
343
+ // for bit-bin with 266 components, it takes about 1,700ms. don't use lodash.merge, it's much faster
344
+ // but mutates `this.versions`.
345
+ return _objectSpread(_objectSpread({}, this.versions), this.orphanedVersions);
346
+ }
347
+ hasTagIncludeOrphaned(version) {
348
+ return Boolean(this.versions[version] || this.orphanedVersions[version]);
349
+ }
350
+
351
+ /**
352
+ * whether the head is a snap (not a tag)
353
+ */
354
+ isHeadSnap() {
355
+ const tagsHashes = this.versionArray.map(ref => ref.toString());
356
+ return this.head && !tagsHashes.includes(this.head.toString());
357
+ }
358
+
359
+ /**
360
+ * add a new remote if it is not there already
361
+ */
362
+ addScopeListItem(scopeListItem) {
363
+ if (!scopeListItem.name || !scopeListItem.url || !scopeListItem.date) {
364
+ throw new TypeError(`model-component.addRemote get an invalid remote. name: ${scopeListItem.name}, url: ${scopeListItem.url}, date: ${scopeListItem.date}`);
365
+ }
366
+ if (!this.scopesList.find(r => r.url === scopeListItem.url)) {
367
+ this.scopesList.push(scopeListItem);
368
+ }
369
+ }
370
+
371
+ /**
372
+ * on main - it checks local-head (or .bitmap version if given) vs remote-head.
373
+ * on lane - it checks local-head on lane vs remote-head on lane.
374
+ * however, to get an accurate `divergeData.snapsOnSourceOnly`, the above is not enough.
375
+ * for example, comp-a@snap-x from lane-a is merged into lane-b. we don't want this snap-x to be "local", because
376
+ * then, bit-status will show it as "staged" and bit-reset will remove it unexpectedly.
377
+ * if we only check by the local-head and remote-head on lane, it'll be local because the remote-head of lane-b is empty.
378
+ * to address this, we search all remote-refs files for this bit-id and during the local history traversal, if a hash
379
+ * is found there, it'll stop the traversal and not mark it as remote.
380
+ * in this example, during the merge, lane-a was fetched, and the remote-ref of this lane has snap-x as the head.
381
+ */
382
+ async setDivergeData(repo, throws = true, fromCache = true, workspaceId) {
383
+ if (!this.divergeData || !fromCache) {
384
+ const remoteHead = (this.laneId ? this.calculatedRemoteHeadWhenOnLane : this.remoteHead) || null;
385
+ // this is for detach-head scenario. it can happen on main only. we want to compare against the .bitmap
386
+ // version (which is the detached head) and not the actual head.
387
+ const workspaceVersion = !this.isOnLane() && workspaceId?.hasVersion() ? workspaceId.version : null;
388
+ this.divergeData = await (0, _component2().getDivergeData)({
389
+ repo,
390
+ modelComponent: this,
391
+ targetHead: remoteHead,
392
+ sourceHead: workspaceVersion ? this.getRef(workspaceVersion) : undefined,
393
+ throws
394
+ });
395
+ }
396
+ }
397
+ isOnLane() {
398
+ return Boolean(this.laneHeadLocal || this.laneHeadLocal === null);
399
+ }
400
+
401
+ /**
402
+ * this is used (among others) by `bit status` to check whether snaps are local (staged), for `bit reset` to remove them
403
+ * and for `bit export` to push them. for "merge pending" status, use `this.getDivergeDataForMergePending()`.
404
+ */
405
+ getDivergeData() {
406
+ if (!this.divergeData) throw new Error(`getDivergeData() expects divergeData to be populate, please use this.setDivergeData() for id: ${this.id()}`);
407
+ return this.divergeData;
408
+ }
409
+
410
+ /**
411
+ * don't use modelComponent.getDivergeData() because in some scenarios when on a lane, it compares the head
412
+ * on the lane against the head on the main, which could show the component as diverged incorrectly.
413
+ */
414
+ async getDivergeDataForMergePending(repo) {
415
+ return (0, _component2().getDivergeData)({
416
+ repo,
417
+ modelComponent: this,
418
+ targetHead: (this.laneId ? this.laneHeadRemote : this.remoteHead) || null,
419
+ throws: false
420
+ });
421
+ }
422
+ async populateLocalAndRemoteHeads(repo, lane) {
423
+ this.setLaneHeadLocal(lane);
424
+ if (lane) this.laneId = lane.toLaneId();
425
+ if (!this.scope) {
426
+ return; // no remote to update. it's local.
427
+ }
428
+ this.remoteHead = await repo.remoteLanes.getRef(_laneId().LaneId.from(_laneId().DEFAULT_LANE, this.scope), this.toComponentId());
429
+ if (!lane) {
430
+ return;
431
+ }
432
+ this.laneHeadRemote = lane.isNew ? null : await repo.remoteLanes.getRef(lane.toLaneId(), this.toComponentId());
433
+ const calculateRemote = async () => {
434
+ if (this.laneHeadRemote) return this.laneHeadRemote;
435
+ if (lane.isNew && lane.forkedFrom && lane.forkedFrom.scope === lane.scope) {
436
+ // the last check is to make sure that if this lane will be exported to a different scope than the original
437
+ // lane, all snaps of the original lane will be considered as local and will be exported later on.
438
+ const headFromFork = await repo.remoteLanes.getRef(lane.forkedFrom, this.toComponentId());
439
+ if (headFromFork) return headFromFork;
440
+ }
441
+ // if no remote-ref was found, because it's checked out to a lane, it's safe to assume that
442
+ // this.head should be on the original-remote. hence, FetchMissingHistory will retrieve it on lane-remote
443
+ return this.remoteHead || this.head;
444
+ };
445
+ this.calculatedRemoteHeadWhenOnLane = await calculateRemote();
446
+ }
447
+ setLaneHeadLocal(lane) {
448
+ if (lane) {
449
+ this.laneHeadLocal = lane.getComponentHead(this.toComponentId());
450
+ }
451
+ }
452
+
453
+ /**
454
+ * returns only the versions that exist in both components (regardless whether the hash are the same)
455
+ * e.g. this.component = [0.0.1, 0.0.2, 0.0.3], other component = [0.0.3, 0.0.4]. it returns only [0.0.3].
456
+ * also, in case it is coming from 'bit import', the version must be locally changed.
457
+ * otherwise, it doesn't matter whether the hashes are different.
458
+ */
459
+ _getComparableVersionsObjects(otherComponent,
460
+ // in case of merging, the otherComponent is the existing component, and "this" is the incoming component
461
+ local) {
462
+ const otherLocalVersion = otherComponent.getLocalVersions();
463
+ const otherComponentVersions = (0, _lodash().pickBy)(otherComponent.versions, (val, key) => Object.keys(this.versions).includes(key) && (!local || otherLocalVersion.includes(key)));
464
+ const thisComponentVersions = (0, _lodash().pickBy)(this.versions, (val, key) => Object.keys(otherComponentVersions).includes(key) && (!local || otherLocalVersion.includes(key)));
465
+ return {
466
+ thisComponentVersions,
467
+ otherComponentVersions
468
+ };
469
+ }
470
+ compatibleWith(component, local) {
471
+ const {
472
+ thisComponentVersions,
473
+ otherComponentVersions
474
+ } = this._getComparableVersionsObjects(component, local);
475
+ return (0, _lodash().isEqual)(thisComponentVersions, otherComponentVersions);
476
+ }
477
+ diffWith(component, local) {
478
+ const {
479
+ thisComponentVersions,
480
+ otherComponentVersions
481
+ } = this._getComparableVersionsObjects(component, local);
482
+ return Object.keys(thisComponentVersions).filter(version => thisComponentVersions[version].hash !== otherComponentVersions[version].hash);
483
+ }
484
+ isEmpty() {
485
+ return (0, _lodash().isEmpty)(this.versions) && !this.hasHead();
486
+ }
487
+
488
+ /**
489
+ * on main return main head, on lane, return lane head.
490
+ * if the head is also a tag, return the tag, otherwise, return the hash.
491
+ */
492
+ getHeadRegardlessOfLaneAsTagOrHash(returnVersionZeroForNoHead = false) {
493
+ const head = this.getHeadRegardlessOfLane();
494
+ if (!head) {
495
+ if (!(0, _lodash().isEmpty)(this.versions)) throw new Error(`error: ${this.id()} has tags but no head, it might be originated from legacy`);
496
+ if (returnVersionZeroForNoHead) return VERSION_ZERO;
497
+ throw new Error(`getHeadRegardlessOfLaneAsTagOrHash() failed finding a head for ${this.id()}`);
498
+ }
499
+ return this.getTagOfRefIfExists(head) || head.toString();
500
+ }
501
+
502
+ /**
503
+ * get the recent head. if locally is ahead, return the local head. otherwise, return the remote head.
504
+ *
505
+ * a user can be checked out to a lane, in which case, `this.laneHeadLocal` and `this.laneHeadRemote`
506
+ * may be populated.
507
+ * `this.head` may not be populated, e.g. when a component was created on
508
+ * this lane and never got snapped on main.
509
+ * it's impossible that `this.head.isEqual(this.laneHeadLocal)`, because when snapping it's either
510
+ * on main, which goes to this.head OR on a lane, which goes to this.laneHeadLocal.
511
+ */
512
+ async headIncludeRemote(repo) {
513
+ const latestLocally = this.getHeadRegardlessOfLaneAsTagOrHash(true);
514
+ const remoteHead = this.laneHeadRemote || this.remoteHead;
515
+ if (!remoteHead) return latestLocally;
516
+ if (!this.getHeadRegardlessOfLane()) {
517
+ return remoteHead.toString(); // in case a snap was created on another lane
518
+ }
519
+
520
+ // either a user is on main or a lane, check whether the remote is ahead of the local
521
+ if (this.laneId && !this.laneHeadRemote) {
522
+ // when on a lane, setDivergeData is using the `this.calculatedRemoteHeadWhenOnLane`,
523
+ // which takes into account main-head and forked-head. here, we don't want this. we care only about the
524
+ // remote-lane head.
525
+ return latestLocally;
526
+ }
527
+ await this.setDivergeData(repo, false);
528
+ const divergeData = this.getDivergeData();
529
+ if (divergeData.isTargetAhead()) {
530
+ return this.getTagOfRefIfExists(remoteHead) || remoteHead.toString();
531
+ }
532
+ return latestLocally;
533
+ }
534
+ async getRefOfAncestor(repo, generationsToGoBack) {
535
+ const head = this.getHeadRegardlessOfLane();
536
+ if (!head) throw new (_bitError().BitError)(`getRefOfAncestor failed to find the head of ${this.id()}`);
537
+ const versionHistory = await this.getAndPopulateVersionHistory(repo, head);
538
+ return versionHistory.getAncestor(generationsToGoBack, head);
539
+ }
540
+ latestVersion() {
541
+ if ((0, _lodash().isEmpty)(this.versions)) return VERSION_ZERO;
542
+ return (0, _pkgModules().getLatestVersion)(this.listVersions());
543
+ }
544
+ latestVersionIfExist() {
545
+ if ((0, _lodash().isEmpty)(this.versions)) return undefined;
546
+ return (0, _pkgModules().getLatestVersion)(this.listVersions());
547
+ }
548
+
549
+ // @todo: make it readable, it's a mess
550
+ isLatestGreaterThan(version) {
551
+ if (!version) throw TypeError('isLatestGreaterThan expect to get a Version');
552
+ const latest = this.getHeadRegardlessOfLaneAsTagOrHash(true);
553
+ if (this.isEmpty() && !this.calculatedRemoteHeadWhenOnLane) {
554
+ return false; // in case a snap was created on another lane
555
+ }
556
+ if ((0, _componentVersion().isTag)(latest) && (0, _componentVersion().isTag)(version)) {
557
+ return semver().gt(latest, version);
558
+ }
559
+ if (latest === version) return false;
560
+ const latestRef = this.getRef(latest);
561
+ if (!latestRef) throw new Error('isLatestGreaterThan, latestRef was not found');
562
+ const latestHash = latestRef.toString();
563
+ const versionRef = this.getRef(version);
564
+ if (!versionRef) return true; // probably a child
565
+ const versionHash = versionRef.toString();
566
+ if (latestHash === versionHash) return false;
567
+ return true;
568
+ }
569
+
570
+ /**
571
+ * Return the lateset version which actuall exists in the scope
572
+ * (exists means the object itself exists)
573
+ * This relevant for cases when the component version array has few versions
574
+ * but we don't have all the refs in the object
575
+ *
576
+ * @returns {number}
577
+ * @memberof Component
578
+ */
579
+ latestExisting(repository) {
580
+ if ((0, _lodash().isEmpty)(this.versions)) return VERSION_ZERO;
581
+ const versions = this.listVersions('ASC');
582
+ let version = null;
583
+ let versionStr = null;
584
+ while (!version && versions && versions.length) {
585
+ // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
586
+ versionStr = versions.pop();
587
+ // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
588
+ version = this.loadVersionSync(versionStr, repository, false);
589
+ }
590
+ return versionStr || VERSION_ZERO;
591
+ }
592
+
593
+ /**
594
+ * get component log and sort by the timestamp in ascending order (from the earliest to the latest)
595
+ */
596
+ async collectLogs(scope, shortHash = false, startFrom) {
597
+ const repo = scope.objects;
598
+ let versionsInfo = await (0, _component2().getAllVersionsInfo)({
599
+ modelComponent: this,
600
+ repo,
601
+ throws: false,
602
+ startFrom
603
+ });
604
+
605
+ // due to recent changes of getting version-history object rather than fetching the entire history, some version
606
+ // objects might be missing. import the component from the remote
607
+ if (versionsInfo.some(v => v.error && (0, _legacy6().errorIsTypeOfMissingObject)(v.error)) && this.scope !== repo.scopeJson.name) {
608
+ _legacy4().logger.info(`collectLogs is unable to find some objects for ${this.id()}. will try to import them`);
609
+ try {
610
+ const lane = await scope.getCurrentLaneObject();
611
+ await scope.scopeImporter.importWithoutDeps(_componentId().ComponentIdList.fromArray([this.toComponentId()]).toVersionLatest(), {
612
+ cache: false,
613
+ includeVersionHistory: true,
614
+ collectParents: true,
615
+ lane: lane || undefined,
616
+ reason: 'to collect logs (including parents)'
617
+ });
618
+ versionsInfo = await (0, _component2().getAllVersionsInfo)({
619
+ modelComponent: this,
620
+ repo,
621
+ throws: false,
622
+ startFrom
623
+ });
624
+ } catch (err) {
625
+ _legacy4().logger.error(`collectLogs failed to import ${this.id()} history`, err);
626
+ }
627
+ }
628
+ const head = this.getHeadRegardlessOfLane();
629
+ const headVersion = head ? await repo.load(head) : undefined;
630
+ const removeAspect = headVersion?.extensions.findCoreExtension(_legacy().Extensions.remove);
631
+ const removeRange = removeAspect?.config.range;
632
+ const deprecationAspect = headVersion?.extensions.findCoreExtension(_legacy().Extensions.deprecation);
633
+ const deprecationRange = deprecationAspect?.config.range;
634
+ const getRef = ref => shortHash ? ref.toShortString() : ref.toString();
635
+ const results = versionsInfo.map(versionInfo => {
636
+ const log = versionInfo.version ? versionInfo.version.log : {
637
+ message: '<no-data-available>'
638
+ };
639
+ return _objectSpread(_objectSpread({}, log), {}, {
640
+ // @ts-ignore
641
+ username: log?.username || 'unknown',
642
+ // @ts-ignore
643
+ email: log?.email || 'unknown',
644
+ tag: versionInfo.tag,
645
+ hash: getRef(versionInfo.ref),
646
+ parents: versionInfo.parents.map(parent => getRef(parent)),
647
+ onLane: versionInfo.onLane,
648
+ deleted: versionInfo.tag && removeRange && semver().satisfies(versionInfo.tag, removeRange),
649
+ deprecated: versionInfo.tag && deprecationRange && semver().satisfies(versionInfo.tag, deprecationRange),
650
+ hidden: versionInfo.version?.hidden
651
+ });
652
+ });
653
+ // sort from earliest to latest
654
+ const sorted = results.sort((a, b) => {
655
+ // @ts-ignore
656
+ if (a.date && b.date) return a.date - b.date;
657
+ return 0;
658
+ });
659
+ return sorted;
660
+ }
661
+ collectVersions(repo) {
662
+ return Promise.all(this.listVersions().map(versionNum => {
663
+ // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
664
+ return this.toConsumerComponent(versionNum, this.scope, repo);
665
+ }));
666
+ }
667
+ getTagOfRefIfExists(ref, allTags = this.versionsIncludeOrphaned) {
668
+ return Object.keys(allTags).find(versionRef => allTags[versionRef].isEqual(ref));
669
+ }
670
+ getTag(version) {
671
+ if ((0, _componentVersion().isTag)(version)) return version;
672
+ const ref = _objects().Ref.from(version);
673
+ return this.getTagOfRefIfExists(ref);
674
+ }
675
+ switchHashesWithTagsIfExist(refs) {
676
+ // cache the this.versionsIncludeOrphaned results into "allTags", looks strange but it improved
677
+ // the performance on bit-bin with 188 components during source.merge in 4 seconds.
678
+ const allTags = this.versionsIncludeOrphaned;
679
+ return refs.map(ref => this.getTagOfRefIfExists(ref, allTags) || ref.toString());
680
+ }
681
+
682
+ /**
683
+ * if exactVersion is defined, add exact version instead of using the semver mechanism
684
+ */
685
+ getVersionToAdd(releaseType = _legacy().DEFAULT_BIT_RELEASE_TYPE, exactVersion, incrementBy, preReleaseId) {
686
+ if (exactVersion && this.versions[exactVersion]) {
687
+ throw new (_legacy6().VersionAlreadyExists)(exactVersion, this.id());
688
+ }
689
+ const version = exactVersion || this.version(releaseType, incrementBy, preReleaseId);
690
+ (0, _pkgModules().validateVersion)(version);
691
+ return version;
692
+ }
693
+ isEqual(component, considerOrphanedVersions = true) {
694
+ if (this.hasHead() && !component.hasHead() || !this.hasHead() && component.hasHead()) {
695
+ return false; // only one of them has head
696
+ }
697
+ if (this.head && component.head && !this.head.isEqual(component.head)) {
698
+ return false; // the head is not equal.
699
+ }
700
+ // the head is equal or they both don't have head. check the versions
701
+ if (this.versionArray.length !== component.versionArray.length) {
702
+ return false;
703
+ }
704
+ const hasSameVersions = Object.keys(this.versions).every(tag => component.versions[tag] && component.versions[tag].isEqual(this.versions[tag]));
705
+ if (considerOrphanedVersions) {
706
+ if (Object.keys(this.orphanedVersions).length !== Object.keys(component.orphanedVersions).length) {
707
+ return false;
708
+ }
709
+ const hasSameOrphanedVersions = Object.keys(this.orphanedVersions).every(tag => component.orphanedVersions[tag] && component.orphanedVersions[tag].isEqual(this.orphanedVersions[tag]));
710
+ if (!hasSameOrphanedVersions) {
711
+ return false;
712
+ }
713
+ }
714
+ return hasSameVersions;
715
+ }
716
+
717
+ // eslint-disable-next-line complexity
718
+ addVersion(version, versionToAdd, lane, previouslyUsedVersion, {
719
+ addToUpdateDependentsInLane,
720
+ setHeadAsParent,
721
+ detachHead,
722
+ overrideHead
723
+ } = {}) {
724
+ if (detachHead && overrideHead) {
725
+ throw new Error(`addVersion expects either detachHead or overrideHead to be true, not both`);
726
+ }
727
+ if (lane) {
728
+ if ((0, _componentVersion().isTag)(versionToAdd)) {
729
+ throw new (_bitError().BitError)('unable to tag when checked out to a lane, please switch to main, merge the lane and then tag again');
730
+ }
731
+ const currentBitId = this.toComponentId();
732
+ const versionToAddRef = _objects().Ref.from(versionToAdd);
733
+ const parent = previouslyUsedVersion ? this.getRef(previouslyUsedVersion) : null;
734
+ if (!parent) {
735
+ const existingComponentInLane = lane.getComponent(currentBitId);
736
+ const currentHead = existingComponentInLane && existingComponentInLane.head || this.getHead();
737
+ if (currentHead) {
738
+ throw new Error(`component ${currentBitId.toString()} has a head (${currentHead.toString()}) but previouslyUsedVersion is empty`);
739
+ }
740
+ }
741
+ if (parent && !parent.isEqual(versionToAddRef)) {
742
+ version.addAsOnlyParent(parent);
743
+ }
744
+ if (addToUpdateDependentsInLane) {
745
+ lane.addComponentToUpdateDependents(currentBitId.changeVersion(versionToAddRef.toString()));
746
+ lane.setOverrideUpdateDependents(true);
747
+ } else {
748
+ lane.addComponent({
749
+ id: currentBitId,
750
+ head: versionToAddRef,
751
+ isDeleted: version.isRemoved()
752
+ });
753
+ }
754
+ if (lane.readmeComponent && lane.readmeComponent.id.fullName === currentBitId.fullName) {
755
+ lane.setReadmeComponent(currentBitId);
756
+ }
757
+ this.laneHeadLocal = versionToAddRef;
758
+ return versionToAdd;
759
+ }
760
+ // user on main
761
+ const head = this.getHead();
762
+ const parent = previouslyUsedVersion ? this.getRef(previouslyUsedVersion) : null;
763
+ if (head && head.toString() !== versionToAdd && !this.hasTag(versionToAdd) // happens with auto-snap
764
+ ) {
765
+ // if this is a tag and this tag exists, the same version was added before with a different hash.
766
+ // adding the current head into the parent will result in a non-exist hash in the parent.
767
+ // if this is a hash and it's the same hash as the current head, adding it as a parent
768
+ // results in a parent and a version has the same hash.
769
+ // @todo: fix it in a more elegant way
770
+ const parentToSet = setHeadAsParent ? head : parent;
771
+ version.addAsOnlyParent(parentToSet || head);
772
+ }
773
+ if (parent && head && !parent.isEqual(head) && !overrideHead && (0, _harmonyModules().isFeatureEnabled)(_harmonyModules().DETACH_HEAD)) {
774
+ if (detachHead) this.detachedHeads.setHead(version.hash());else throw new Error(`unable to add a new version for "${this.id()}" on main.
775
+ this version started from an older version (${previouslyUsedVersion}), and not from the head (${head}).
776
+ if this is done intentionally, please re-run with --detach-head (or --override-head if available).
777
+ otherwise, please run "bit checkout head" to be up to date, then snap/tag your changes.`);
778
+ } else {
779
+ this.setHead(version.hash());
780
+ this.detachedHeads.clearCurrent();
781
+ }
782
+ if ((0, _componentVersion().isTag)(versionToAdd)) {
783
+ this.setVersion(versionToAdd, version.hash());
784
+ }
785
+ this.markVersionAsLocal(versionToAdd);
786
+ return versionToAdd;
787
+ }
788
+ version(releaseType = _legacy().DEFAULT_BIT_RELEASE_TYPE, incrementBy = 1, preReleaseId) {
789
+ // if (preRelease) releaseType = 'prerelease';
790
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
791
+ const increment = ver => semver().inc(ver, releaseType, undefined, preReleaseId);
792
+ const latest = this.latestVersion();
793
+ if (!latest) {
794
+ const isPreReleaseLike = ['prerelease', 'premajor', 'preminor', 'prepatch'].includes(releaseType);
795
+ return isPreReleaseLike ? increment(_legacy().DEFAULT_BIT_VERSION) : _legacy().DEFAULT_BIT_VERSION;
796
+ }
797
+ let result = increment(latest);
798
+ if (incrementBy === 1) return result;
799
+ for (let i = 1; i < incrementBy; i += 1) {
800
+ result = increment(result);
801
+ }
802
+ return result;
803
+ }
804
+ id() {
805
+ return this.scope ? [this.scope, this.name].join('/') : this.name;
806
+ }
807
+
808
+ /**
809
+ * @deprecated use toComponentId() instead
810
+ */
811
+ toBitId() {
812
+ return new (_legacyBitId().BitId)({
813
+ scope: this.scope,
814
+ name: this.name
815
+ });
816
+ }
817
+ toComponentId() {
818
+ if (!this.scope) throw new Error(`ModelComponent: scope is missing from "${this.name}"`);
819
+ return new (_componentId().ComponentID)(this.toBitId());
820
+ }
821
+
822
+ /**
823
+ * @deprecated use toComponentIdWithLatestVersion() instead
824
+ */
825
+ toBitIdWithLatestVersion() {
826
+ return new (_legacyBitId().BitId)({
827
+ scope: this.scope,
828
+ name: this.name,
829
+ version: this.getHeadRegardlessOfLaneAsTagOrHash(true)
830
+ });
831
+ }
832
+ toComponentIdWithLatestVersion() {
833
+ return _componentId().ComponentID.fromObject({
834
+ scope: this.scope,
835
+ name: this.name,
836
+ version: this.getHeadRegardlessOfLaneAsTagOrHash(true)
837
+ });
838
+ }
839
+ toComponentIdWithHead() {
840
+ return _componentId().ComponentID.fromObject({
841
+ scope: this.scope,
842
+ name: this.name,
843
+ version: this.head?.toString()
844
+ });
845
+ }
846
+ toBitIdWithLatestVersionAllowNull() {
847
+ const id = this.toComponentIdWithLatestVersion();
848
+ return id.version === VERSION_ZERO ? id.changeVersion(undefined) : id;
849
+ }
850
+ toObject() {
851
+ function versions(vers) {
852
+ const obj = {};
853
+ (0, _lodash().forEach)(vers, (ref, version) => {
854
+ obj[version] = ref.toString();
855
+ });
856
+ return obj;
857
+ }
858
+ const componentObject = {
859
+ name: this.name,
860
+ scope: this.scope,
861
+ versions: versions(this.versions),
862
+ lang: this.lang,
863
+ deprecated: this.deprecated,
864
+ bindingPrefix: this.bindingPrefix,
865
+ remotes: this.scopesList,
866
+ schema: this.schema,
867
+ detachedHeads: this.detachedHeads.toObject()
868
+ };
869
+ // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
870
+ if (this.local) componentObject.local = this.local;
871
+ // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
872
+ if (!(0, _lodash().isEmpty)(this.state)) componentObject.state = this.state;
873
+ // @ts-ignore
874
+ if (!(0, _lodash().isEmpty)(this.orphanedVersions)) componentObject.orphanedVersions = versions(this.orphanedVersions);
875
+ const headStr = this.getHeadStr();
876
+ // @ts-ignore
877
+ if (headStr) componentObject.head = headStr;
878
+ return componentObject;
879
+ }
880
+ async loadVersion(versionStr, repository, throws = true) {
881
+ const versionRef = this.getRef(versionStr);
882
+ if (!versionRef) throw new (_legacy6().VersionNotFound)(versionStr, this.id());
883
+ const version = await repository.load(versionRef, false);
884
+ if (!version && throws) throw new (_legacy6().VersionNotFoundOnFS)(versionStr, this.id());
885
+ return version;
886
+ }
887
+ loadVersionSync(version, repository, throws = true) {
888
+ const versionRef = this.getRef(version);
889
+ if (!versionRef) throw new (_legacy6().VersionNotFound)(version, this.id());
890
+ // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
891
+ return versionRef.loadSync(repository, throws);
892
+ }
893
+ async collectVersionsObjects(repo, versions, throwForMissingLocalArtifacts = false, workspaceId) {
894
+ const refsWithoutArtifacts = [];
895
+ const artifactsRefs = [];
896
+ const artifactsRefsFromExportedVersions = [];
897
+ const locallyChangedVersions = await this.getLocalTagsOrHashes(repo, workspaceId);
898
+ const locallyChangedHashes = locallyChangedVersions.map(v => (0, _componentVersion().isTag)(v) ? this.versionsIncludeOrphaned[v].hash : v);
899
+ const versionsRefs = versions.map(version => this.getRef(version));
900
+ refsWithoutArtifacts.push(...versionsRefs);
901
+ const versionsObjects = await Promise.all(versionsRefs.map(versionRef => this.loadVersion(versionRef.toString(), repo)));
902
+ versionsObjects.forEach(versionObject => {
903
+ const refs = versionObject.refsWithOptions(false, false);
904
+ refsWithoutArtifacts.push(...refs);
905
+ const refsFromExtensions = (0, _component().getRefsFromExtensions)(versionObject.extensions);
906
+ locallyChangedHashes.includes(versionObject.hash().toString()) ? artifactsRefs.push(...refsFromExtensions) : artifactsRefsFromExportedVersions.push(...refsFromExtensions);
907
+ });
908
+ const loadedRefs = [];
909
+ try {
910
+ const loaded = await repo.loadManyRaw(refsWithoutArtifacts);
911
+ loadedRefs.push(...loaded);
912
+ } catch (err) {
913
+ if (err.code === 'ENOENT') {
914
+ throw new Error(`unable to find an object file "${err.path}"
915
+ for a component "${this.id()}", versions: ${versions.join(', ')}`);
916
+ }
917
+ throw err;
918
+ }
919
+ try {
920
+ const loaded = throwForMissingLocalArtifacts ? await repo.loadManyRaw(artifactsRefs) : await repo.loadManyRawIgnoreMissing(artifactsRefs);
921
+ loadedRefs.push(...loaded);
922
+ // ignore missing artifacts when exporting old versions that were exported in the past and are now exported to a
923
+ // different scope. this is happening for example when exporting a lane that has components from different
924
+ // remotes. it's ok to not have all artifacts from the other remotes to this remote.
925
+ const loadedExportedArtifacts = await repo.loadManyRawIgnoreMissing(artifactsRefsFromExportedVersions);
926
+ loadedRefs.push(...loadedExportedArtifacts);
927
+ } catch (err) {
928
+ if (err.code === 'ENOENT') {
929
+ throw new Error(`unable to find an artifact object file "${err.path}"
930
+ for a component "${this.id()}", versions: ${versions.join(', ')}
931
+ consider using --ignore-missing-artifacts flag if you're sure the artifacts are in the remote`);
932
+ }
933
+ throw err;
934
+ }
935
+ return loadedRefs;
936
+ }
937
+ async collectObjects(repo) {
938
+ try {
939
+ const [rawComponent, objects] = await Promise.all([this.asRaw(repo), this.collectRaw(repo)]);
940
+ return new (_legacy6().ComponentObjects)(rawComponent, objects.map(o => o.buffer));
941
+ } catch (err) {
942
+ if (err.code === 'ENOENT') {
943
+ throw new Error(`fatal: an object of "${this.id()}" was not found at ${err.path}\nplease try to re-import the component`);
944
+ }
945
+ throw err;
946
+ }
947
+ }
948
+
949
+ /**
950
+ * to delete a version from a component, don't call this method directly. Instead, use sources.removeVersion()
951
+ */
952
+ removeVersion(version) {
953
+ const objectRef = this.getRef(version);
954
+ if (!objectRef) throw new Error(`removeVersion failed finding version ${version}`);
955
+ delete this.versions[version];
956
+ if (this.state.versions && this.state.versions[version]) delete this.state.versions[version];
957
+ return objectRef;
958
+ }
959
+ toComponentVersion(versionStr) {
960
+ const versionParsed = (0, _componentVersion().versionParser)(versionStr);
961
+ const versionNum = versionParsed.latest ? this.getHeadRegardlessOfLaneAsTagOrHash(true) : versionParsed.versionNum;
962
+ if (versionNum === VERSION_ZERO) {
963
+ throw new (_legacy6().NoHeadNoVersion)(this.id());
964
+ }
965
+ if ((0, _componentVersion().isTag)(versionNum) && !this.hasTagIncludeOrphaned(versionNum)) {
966
+ throw new (_bitError().BitError)(`the version ${versionNum} of "${this.id()}" does not exist in ${this.listVersionsIncludeOrphaned().join('\n')}, versions array.`);
967
+ }
968
+ return new (_legacy6().ComponentVersion)(this, versionNum);
969
+ }
970
+
971
+ /**
972
+ * if no "specificVersion" is given, it returns according to the head
973
+ */
974
+ async isDeprecated(repo, specificVersion) {
975
+ // backward compatibility
976
+ if (this.deprecated) {
977
+ return true;
978
+ }
979
+ const head = this.getHeadRegardlessOfLane();
980
+ if (!head) {
981
+ // it's legacy, or new. If legacy, the "deprecated" prop should do. if it's new, the workspace should
982
+ // have the answer.
983
+ return false;
984
+ }
985
+ const headVersion = await repo.load(head);
986
+ if (!headVersion) {
987
+ // the head Version doesn't exist locally, there is no way to know whether it's deprecated
988
+ return null;
989
+ }
990
+ const deprecationAspect = headVersion.extensions.findCoreExtension(_legacy().Extensions.deprecation);
991
+ if (!deprecationAspect) {
992
+ return false;
993
+ }
994
+ if (deprecationAspect.config.deprecate) {
995
+ return true;
996
+ }
997
+ if (specificVersion && deprecationAspect.config.range) {
998
+ const tag = this.getTag(specificVersion);
999
+ if (!tag) return false; // it's a snap. "range" doesn't support deprecating snaps. only semver.
1000
+ return semver().satisfies(tag, deprecationAspect.config.range);
1001
+ }
1002
+ return false;
1003
+ }
1004
+ async isRemoved(repo, specificVersion) {
1005
+ const getHead = () => {
1006
+ if (!this.laneHeadLocal) return this.getHead();
1007
+ // you're checked out to a lane.
1008
+ if (!specificVersion) return this.laneHeadLocal;
1009
+ // it's possible that this specificVersion is from main.
1010
+ if (specificVersion === this.laneHeadLocal.toString()) return this.laneHeadLocal;
1011
+ return this.getHead();
1012
+ };
1013
+ const head = getHead();
1014
+ if (!head) {
1015
+ // it's new or only on lane
1016
+ return false;
1017
+ }
1018
+ const headVersion = await repo.load(head);
1019
+ if (!headVersion) {
1020
+ // the head Version doesn't exist locally, there is no way to know whether it's removed
1021
+ return null;
1022
+ }
1023
+ const removeAspect = headVersion.extensions.findCoreExtension(_legacy().Extensions.remove);
1024
+ if (!removeAspect) {
1025
+ return false;
1026
+ }
1027
+ if (removeAspect.config.removed) {
1028
+ return true;
1029
+ }
1030
+ if (specificVersion && removeAspect.config.range) {
1031
+ const tag = this.getTag(specificVersion);
1032
+ if (!tag) return false; // it's a snap. "range" doesn't support snaps. only semver.
1033
+ return semver().satisfies(tag, removeAspect.config.range);
1034
+ }
1035
+ return false;
1036
+ }
1037
+ async isLaneReadmeOf(repo) {
1038
+ const head = this.getHeadRegardlessOfLane();
1039
+ if (!head) {
1040
+ // we dont support lanes in legacy
1041
+ return [];
1042
+ }
1043
+ const version = await repo.load(head);
1044
+ if (!version) {
1045
+ // the head Version doesn't exist locally, there is no way to know whether it is a lane readme component
1046
+ return [];
1047
+ }
1048
+ const lanesAspect = version.extensions.findCoreExtension(_legacy().Extensions.lanes);
1049
+ if (!lanesAspect || !lanesAspect.config.readme) {
1050
+ return [];
1051
+ }
1052
+ return Object.keys(lanesAspect.config.readme);
1053
+ }
1054
+ /**
1055
+ * convert a ModelComponent of a specific version to ConsumerComponent
1056
+ * @see sources.consumerComponentToVersion() for the opposite action.
1057
+ */
1058
+ async toConsumerComponent(versionStr, scopeName, repository) {
1059
+ _legacy4().logger.trace(`model-component, converting ${this.id()}, version: ${versionStr} to ConsumerComponent`);
1060
+ let componentVersion = this.toComponentVersion(versionStr);
1061
+ const version = await componentVersion.getVersion(repository);
1062
+ // in case the the version is a short-hash, it should be converted to a full hash.
1063
+ if (versionStr !== _componentVersion().LATEST_VERSION && !(0, _componentVersion().isTag)(versionStr) && !(0, _componentVersion().isSnap)(versionStr) && version.hash().toString() !== versionStr) {
1064
+ componentVersion = new (_legacy6().ComponentVersion)(this, version.hash().toString());
1065
+ }
1066
+ const loadFileInstance = ClassName => async file => {
1067
+ const loadP = file.file.load(repository);
1068
+ const content = await loadP;
1069
+ if (!content) throw new (_bitError().BitError)(`failed loading file ${file.relativePath} from the model of ${this.id()}@${versionStr}`);
1070
+ return new ClassName({
1071
+ base: '.',
1072
+ path: file.relativePath,
1073
+ contents: content.contents,
1074
+ test: file.test
1075
+ });
1076
+ };
1077
+ const filesP = version.files ? Promise.all(version.files.map(loadFileInstance(_component().SourceFile))) : null;
1078
+ // @todo: this is weird. why the scopeMeta would be taken from the current scope and not he component scope?
1079
+ const scopeMetaP = scopeName ? _scopeMeta().default.fromScopeName(scopeName).load(repository) : Promise.resolve();
1080
+ const log = version.log || null;
1081
+ // @ts-ignore
1082
+ const [files, scopeMeta] = await Promise.all([filesP, scopeMetaP]);
1083
+ const extensions = version.extensions.clone();
1084
+ // when generating a new ConsumerComponent out of Version, it is critical to make sure that
1085
+ // all objects are cloned and not copied by reference. Otherwise, every time the
1086
+ // ConsumerComponent instance is changed, the Version will be changed as well, and since
1087
+ // the Version instance is saved in the Repository._cache, the next time a Version instance
1088
+ // is retrieved, it'll be different than the first time.
1089
+ const consumerComponent = new (_legacy2().ConsumerComponent)({
1090
+ name: this.name,
1091
+ version: componentVersion.version,
1092
+ scope: this.toComponentId()._legacy.scope,
1093
+ defaultScope: this.scope,
1094
+ lang: this.lang,
1095
+ bindingPrefix: this.bindingPrefix,
1096
+ mainFile: version.mainFile,
1097
+ dependencies: this.addDepsInfoFromDepsResolver(version.dependencies, extensions),
1098
+ devDependencies: this.addDepsInfoFromDepsResolver(version.devDependencies, extensions),
1099
+ flattenedDependencies: version.flattenedDependencies.clone(),
1100
+ packageDependencies: (0, _lodash().clone)(version.packageDependencies),
1101
+ devPackageDependencies: (0, _lodash().clone)(version.devPackageDependencies),
1102
+ peerPackageDependencies: (0, _lodash().clone)(version.peerPackageDependencies),
1103
+ // @ts-ignore
1104
+ files,
1105
+ docs: version.docs,
1106
+ // @ts-ignore
1107
+ license: scopeMeta ? _component().License.deserialize(scopeMeta.license) : undefined,
1108
+ // todo: make sure we have license in case of local scope
1109
+ log,
1110
+ overrides: _legacy3().ComponentOverrides.loadFromScope(version.overrides),
1111
+ packageJsonChangedProps: (0, _lodash().clone)(version.packageJsonChangedProps),
1112
+ deprecated: this.deprecated,
1113
+ removed: version.isRemoved(),
1114
+ scopesList: (0, _lodash().clone)(this.scopesList),
1115
+ schema: version.schema,
1116
+ extensions,
1117
+ buildStatus: version.buildStatus
1118
+ });
1119
+ return consumerComponent;
1120
+ }
1121
+ addDepsInfoFromDepsResolver(dependencies, extensions) {
1122
+ const cloned = dependencies.getClone();
1123
+ const depsResolverData = extensions.find(ext => ext.name === 'teambit.dependencies/dependency-resolver');
1124
+ if (!depsResolverData) return cloned;
1125
+ cloned.forEach(dependency => {
1126
+ if (dependency.packageName) return;
1127
+ const matchedEntry = depsResolverData.data?.dependencies?.find(entry => {
1128
+ return dependency.id.toString() === entry.id;
1129
+ });
1130
+ if (matchedEntry) {
1131
+ dependency.packageName = matchedEntry.packageName;
1132
+ }
1133
+ });
1134
+ return cloned;
1135
+ }
1136
+
1137
+ // @todo: make sure it doesn't have the same ref twice, once as a version and once as a head
1138
+ refs() {
1139
+ const versions = Object.values(this.versionsIncludeOrphaned);
1140
+ if (this.head) versions.push(this.head);
1141
+ return versions;
1142
+ }
1143
+ validateBeforePersisting(componentStr) {
1144
+ _legacy4().logger.trace(`validating component object: ${this.hash().hash} ${this.id()}`);
1145
+ const component = Component.parse(componentStr);
1146
+ component.validate();
1147
+ }
1148
+ toBuffer(pretty) {
1149
+ const args = (0, _legacy5().getStringifyArgs)(pretty);
1150
+ const obj = this.toObject();
1151
+ const str = JSON.stringify(obj, ...args);
1152
+ if (this.validateBeforePersist) this.validateBeforePersisting(str);
1153
+ return Buffer.from(str);
1154
+ }
1155
+
1156
+ /**
1157
+ * Clear data that is relevant only for the local scope and should not be moved to the remote scope
1158
+ */
1159
+ clearStateData() {
1160
+ this.local = false; // backward compatibility for components created before 0.12.6
1161
+ this.state = {};
1162
+ }
1163
+ markVersionAsLocal(version) {
1164
+ if (!this.state.versions) this.state = {
1165
+ versions: {}
1166
+ };
1167
+ // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
1168
+ if (!this.state.versions[version]) this.state.versions[version] = {};
1169
+ // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
1170
+ this.state.versions[version].local = true;
1171
+ }
1172
+
1173
+ /**
1174
+ * local versions that are not exported on the main lane.
1175
+ * @see `this.getLocalTagsOrHashes()`, to get local snaps on the current lane
1176
+ */
1177
+ getLocalVersions() {
1178
+ if ((0, _lodash().isEmpty)(this.state) || (0, _lodash().isEmpty)(this.state.versions)) return [];
1179
+ // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
1180
+ return Object.keys(this.state.versions).filter(version => this.state.versions[version].local);
1181
+ }
1182
+ hasLocalTag(tag) {
1183
+ const localVersions = this.getLocalVersions();
1184
+ return localVersions.includes(tag);
1185
+ }
1186
+ async getLocalTagsOrHashes(repo, workspaceId) {
1187
+ const localHashes = await this.getLocalHashes(repo, workspaceId);
1188
+ if (!localHashes.length) return [];
1189
+ return this.switchHashesWithTagsIfExist(localHashes).reverse(); // reverse to get the older first
1190
+ }
1191
+ async getLocalHashes(repo, workspaceId) {
1192
+ await this.setDivergeData(repo, undefined, undefined, workspaceId);
1193
+ const divergeData = this.getDivergeData();
1194
+ const localHashes = divergeData.snapsOnSourceOnly;
1195
+ if (!localHashes.length) return [];
1196
+ return localHashes.reverse(); // reverse to get the older first
1197
+ }
1198
+
1199
+ /**
1200
+ * for most cases, use `isLocallyChanged`, which takes into account lanes.
1201
+ * this is for cases when we only care about the versions exist in the `state` prop.
1202
+ */
1203
+ isLocallyChangedRegardlessOfLanes() {
1204
+ return Boolean(this.getLocalVersions().length);
1205
+ }
1206
+
1207
+ /**
1208
+ * whether the component was locally changed, either by adding a new snap/tag or by merging
1209
+ * components from different lanes.
1210
+ */
1211
+ async isLocallyChanged(repo, lane, workspaceId) {
1212
+ if (lane) await this.populateLocalAndRemoteHeads(repo, lane);
1213
+ await this.setDivergeData(repo, undefined, undefined, workspaceId);
1214
+ const divergeData = this.getDivergeData();
1215
+ return divergeData.isSourceAhead();
1216
+ }
1217
+ async getVersionHistory(repo) {
1218
+ const emptyVersionHistory = _versionHistory().default.fromId(this.name, this.scope);
1219
+ const versionHistory = await repo.load(emptyVersionHistory.hash());
1220
+ return versionHistory || emptyVersionHistory;
1221
+ }
1222
+ async getAndPopulateVersionHistory(repo, head) {
1223
+ const versionHistory = await this.getVersionHistory(repo);
1224
+ const {
1225
+ err
1226
+ } = await this.populateVersionHistoryIfMissingGracefully(repo, versionHistory, head);
1227
+ if (err) {
1228
+ _legacy4().logger.error(`rethrowing an error ${err.message}, current stuck`, new Error(err.message));
1229
+ throw err;
1230
+ }
1231
+ return versionHistory;
1232
+ }
1233
+
1234
+ /**
1235
+ * careful! the `versions` passed here can belong to other components, not necessarily to this one.
1236
+ * that's why it checks whether the version-hash exists in the VersionHistory, and if it's not,
1237
+ * it won't update it.
1238
+ */
1239
+ async updateRebasedVersionHistory(repo, versions) {
1240
+ const versionHistory = await this.getVersionHistory(repo);
1241
+ const hasUpdated = versions.some(version => {
1242
+ const versionData = versionHistory.getVersionData(version.hash());
1243
+ if (!versionData) return false;
1244
+ versionHistory.addFromVersionsObjects([version]);
1245
+ return true;
1246
+ });
1247
+ return hasUpdated ? versionHistory : undefined;
1248
+ }
1249
+ async updateVersionHistory(repo, versions) {
1250
+ const versionHistory = await this.getVersionHistory(repo);
1251
+ versionHistory.addFromVersionsObjects(versions);
1252
+ _legacy4().logger.debug(`updating version history of ${this.id()} with ${versions.length} versions`);
1253
+ return versionHistory;
1254
+ }
1255
+ async populateVersionHistoryIfMissingGracefully(repo, versionHistory, head,
1256
+ /**
1257
+ * during traversal, if a hash is found in the VersionHistory it probably means that it has all history until this
1258
+ * point, so we can stop there for better performance. In some rare cases (e.g. the export was interrupted), we
1259
+ * need the ability of full traversal to repair the VersionHistory.
1260
+ */
1261
+ exitWhenFind = true) {
1262
+ const headExists = versionHistory.hasHash(head);
1263
+ if (exitWhenFind && headExists) return {};
1264
+ const getVersionObj = async ref => await ref.load(repo);
1265
+ const versionsToAdd = [];
1266
+ let err;
1267
+ const addParentsRecursively = async version => {
1268
+ await (0, _pMapSeries().default)(version.parents, async parent => {
1269
+ const foundParent = versionHistory.hasHash(parent) || versionsToAdd.find(v => v.hash().isEqual(parent));
1270
+ if (exitWhenFind && foundParent) {
1271
+ return;
1272
+ }
1273
+ const parentVersion = await getVersionObj(parent);
1274
+ if (!parentVersion) {
1275
+ const tag = this.getTagOfRefIfExists(parent);
1276
+ err = tag ? new (_legacy6().VersionNotFound)(tag, this.id()) : new (_legacy6().ParentNotFound)(this.id(), version.hash().toString(), parent.toString());
1277
+ return;
1278
+ }
1279
+ versionsToAdd.push(parentVersion);
1280
+ await addParentsRecursively(parentVersion);
1281
+ });
1282
+ };
1283
+ const headVer = await getVersionObj(head);
1284
+ if (!headVer) {
1285
+ return {
1286
+ err: new (_legacy6().HeadNotFound)(this.id(), head.toString())
1287
+ };
1288
+ }
1289
+ return this.populateVersionHistoryMutex.runExclusive(async () => {
1290
+ if (!headExists) versionsToAdd.push(headVer);
1291
+ await addParentsRecursively(headVer);
1292
+ const added = versionsToAdd.map(v => (0, _component2().getVersionParentsFromVersion)(v));
1293
+ if (err) {
1294
+ return {
1295
+ err,
1296
+ added
1297
+ };
1298
+ }
1299
+ versionHistory.addFromVersionsObjects(versionsToAdd);
1300
+ _legacy4().logger.debug(`populateVersionHistoryIfMissingGracefully, updating ${this.id()} with ${versionsToAdd.length} versions`);
1301
+ await repo.writeObjectsToTheFS([versionHistory]);
1302
+ return {
1303
+ added
1304
+ };
1305
+ });
1306
+ }
1307
+ static parse(contents) {
1308
+ const rawComponent = JSON.parse(contents);
1309
+ return Component.from({
1310
+ name: rawComponent.box ? `${rawComponent.box}/${rawComponent.name}` : rawComponent.name,
1311
+ scope: rawComponent.scope,
1312
+ versions: (0, _lodash().mapValues)(rawComponent.versions, val => _objects().Ref.from(val)),
1313
+ lang: rawComponent.lang,
1314
+ deprecated: rawComponent.deprecated,
1315
+ bindingPrefix: rawComponent.bindingPrefix,
1316
+ state: rawComponent.state,
1317
+ orphanedVersions: (0, _lodash().mapValues)(rawComponent.orphanedVersions || {}, val => _objects().Ref.from(val)),
1318
+ scopesList: rawComponent.remotes,
1319
+ head: rawComponent.head ? _objects().Ref.from(rawComponent.head) : undefined,
1320
+ schema: rawComponent.schema || (rawComponent.head ? _legacy2().SchemaName.Harmony : _legacy2().SchemaName.Legacy),
1321
+ detachedHeads: _detachHeads().DetachedHeads.fromObject(rawComponent.detachedHeads)
1322
+ });
1323
+ }
1324
+ static from(props) {
1325
+ return new Component(props);
1326
+ }
1327
+ static fromBitId(bitId) {
1328
+ // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
1329
+ return new Component({
1330
+ name: bitId.fullName,
1331
+ scope: bitId.scope
1332
+ });
1333
+ }
1334
+ get isLegacy() {
1335
+ return !this.schema || this.schema === _legacy2().SchemaName.Legacy;
1336
+ }
1337
+ validate() {
1338
+ const message = `unable to save Component object "${this.id()}"`;
1339
+ if (!this.name) throw new (_bitError().BitError)(`${message} the name is missing`);
1340
+ if (this.state && this.state.versions) {
1341
+ Object.keys(this.state.versions).forEach(version => {
1342
+ if ((0, _componentVersion().isTag)(version) && !this.hasTag(version)) {
1343
+ throw new (_legacyCli().ValidationError)(`${message}, the version ${version} is marked as staged but is not available`);
1344
+ }
1345
+ });
1346
+ }
1347
+ const hashDuplications = (0, _toolboxArray().findDuplications)(this.versionArray.map(v => v.toString()));
1348
+ if (hashDuplications.length) {
1349
+ throw new (_legacyCli().ValidationError)(`${message}, the following hash(es) are duplicated ${hashDuplications.join(', ')}`);
1350
+ }
1351
+ Object.keys(this.orphanedVersions).forEach(version => {
1352
+ if (this.versions[version]) {
1353
+ throw new (_legacyCli().ValidationError)(`${message}, the version "${version}" exists in orphanedVersions but it exits also in "versions" prop`);
1354
+ }
1355
+ });
1356
+ if (!this.isLegacy && !this.head && this.versionArray.length) {
1357
+ // legacy don't have head. also, when snapping on a lane the first time, there is no head.
1358
+ // tags are done on default lane only, so if there are versions (tag), it must have head
1359
+ throw new (_legacyCli().ValidationError)(`${message}, the "head" prop is missing`);
1360
+ }
1361
+ }
1362
+ }
1363
+ exports.default = Component;
1364
+
1365
+ //# sourceMappingURL=model-component.js.map