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