@teambit/objects 0.0.0-00042bc99514ea12d897f81bcd0181e2b034c615

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 (121) hide show
  1. package/dist/fixtures/version-model-extended.json +48 -0
  2. package/dist/fixtures/version-model-object.json +87 -0
  3. package/dist/index.d.ts +19 -0
  4. package/dist/index.js +371 -0
  5. package/dist/index.js.map +1 -0
  6. package/dist/models/dependencies-graph.d.ts +46 -0
  7. package/dist/models/dependencies-graph.js +105 -0
  8. package/dist/models/dependencies-graph.js.map +1 -0
  9. package/dist/models/detach-heads.d.ts +25 -0
  10. package/dist/models/detach-heads.js +84 -0
  11. package/dist/models/detach-heads.js.map +1 -0
  12. package/dist/models/export-metadata.d.ts +24 -0
  13. package/dist/models/export-metadata.js +74 -0
  14. package/dist/models/export-metadata.js.map +1 -0
  15. package/dist/models/index.d.ts +10 -0
  16. package/dist/models/index.js +125 -0
  17. package/dist/models/index.js.map +1 -0
  18. package/dist/models/lane-history.d.ts +43 -0
  19. package/dist/models/lane-history.js +131 -0
  20. package/dist/models/lane-history.js.map +1 -0
  21. package/dist/models/lane-history.spec.d.ts +1 -0
  22. package/dist/models/lane-history.spec.js +46 -0
  23. package/dist/models/lane-history.spec.js.map +1 -0
  24. package/dist/models/lane.d.ts +128 -0
  25. package/dist/models/lane.js +485 -0
  26. package/dist/models/lane.js.map +1 -0
  27. package/dist/models/model-component.d.ts +338 -0
  28. package/dist/models/model-component.js +1509 -0
  29. package/dist/models/model-component.js.map +1 -0
  30. package/dist/models/model-component.spec.d.ts +1 -0
  31. package/dist/models/model-component.spec.js +71 -0
  32. package/dist/models/model-component.spec.js.map +1 -0
  33. package/dist/models/scopeMeta.d.ts +20 -0
  34. package/dist/models/scopeMeta.js +68 -0
  35. package/dist/models/scopeMeta.js.map +1 -0
  36. package/dist/models/source.d.ts +10 -0
  37. package/dist/models/source.js +42 -0
  38. package/dist/models/source.js.map +1 -0
  39. package/dist/models/symlink.d.ts +30 -0
  40. package/dist/models/symlink.js +91 -0
  41. package/dist/models/symlink.js.map +1 -0
  42. package/dist/models/version-history.d.ts +59 -0
  43. package/dist/models/version-history.js +296 -0
  44. package/dist/models/version-history.js.map +1 -0
  45. package/dist/models/version.d.ts +288 -0
  46. package/dist/models/version.js +780 -0
  47. package/dist/models/version.js.map +1 -0
  48. package/dist/models/version.spec.d.ts +1 -0
  49. package/dist/models/version.spec.js +332 -0
  50. package/dist/models/version.spec.js.map +1 -0
  51. package/dist/objects/bit-object-list.d.ts +24 -0
  52. package/dist/objects/bit-object-list.js +65 -0
  53. package/dist/objects/bit-object-list.js.map +1 -0
  54. package/dist/objects/index.d.ts +5 -0
  55. package/dist/objects/index.js +60 -0
  56. package/dist/objects/index.js.map +1 -0
  57. package/dist/objects/object-list-to-graph.d.ts +13 -0
  58. package/dist/objects/object-list-to-graph.js +93 -0
  59. package/dist/objects/object-list-to-graph.js.map +1 -0
  60. package/dist/objects/object-list.d.ts +58 -0
  61. package/dist/objects/object-list.js +389 -0
  62. package/dist/objects/object-list.js.map +1 -0
  63. package/dist/objects/object.d.ts +35 -0
  64. package/dist/objects/object.js +189 -0
  65. package/dist/objects/object.js.map +1 -0
  66. package/dist/objects/objects-readable-generator.d.ts +31 -0
  67. package/dist/objects/objects-readable-generator.js +203 -0
  68. package/dist/objects/objects-readable-generator.js.map +1 -0
  69. package/dist/objects/raw-object.d.ts +23 -0
  70. package/dist/objects/raw-object.js +145 -0
  71. package/dist/objects/raw-object.js.map +1 -0
  72. package/dist/objects/ref.d.ts +14 -0
  73. package/dist/objects/ref.js +45 -0
  74. package/dist/objects/ref.js.map +1 -0
  75. package/dist/objects/repository.d.ts +165 -0
  76. package/dist/objects/repository.js +866 -0
  77. package/dist/objects/repository.js.map +1 -0
  78. package/dist/objects/scope-index.d.ts +74 -0
  79. package/dist/objects/scope-index.js +255 -0
  80. package/dist/objects/scope-index.js.map +1 -0
  81. package/dist/objects/scope-index.spec.d.ts +1 -0
  82. package/dist/objects/scope-index.spec.js +152 -0
  83. package/dist/objects/scope-index.spec.js.map +1 -0
  84. package/dist/objects.aspect.d.ts +2 -0
  85. package/dist/objects.aspect.js +18 -0
  86. package/dist/objects.aspect.js.map +1 -0
  87. package/dist/objects.main.runtime.d.ts +7 -0
  88. package/dist/objects.main.runtime.js +36 -0
  89. package/dist/objects.main.runtime.js.map +1 -0
  90. package/dist/preview-1781548568469.js +7 -0
  91. package/fixtures/version-model-extended.json +48 -0
  92. package/fixtures/version-model-object.json +87 -0
  93. package/models/dependencies-graph.ts +120 -0
  94. package/models/detach-heads.ts +79 -0
  95. package/models/export-metadata.ts +56 -0
  96. package/models/index.ts +11 -0
  97. package/models/lane-history.spec.ts +35 -0
  98. package/models/lane-history.ts +131 -0
  99. package/models/lane.ts +400 -0
  100. package/models/model-component.spec.ts +55 -0
  101. package/models/model-component.ts +1522 -0
  102. package/models/scopeMeta.ts +58 -0
  103. package/models/source.ts +31 -0
  104. package/models/symlink.ts +66 -0
  105. package/models/version-history.ts +283 -0
  106. package/models/version.spec.ts +280 -0
  107. package/models/version.ts +821 -0
  108. package/objects/bit-object-list.ts +59 -0
  109. package/objects/index.ts +6 -0
  110. package/objects/object-list-to-graph.ts +69 -0
  111. package/objects/object-list.ts +327 -0
  112. package/objects/object.ts +152 -0
  113. package/objects/objects-readable-generator.ts +174 -0
  114. package/objects/raw-object.ts +132 -0
  115. package/objects/ref.ts +45 -0
  116. package/objects/repository.ts +780 -0
  117. package/objects/scope-index.spec.ts +95 -0
  118. package/objects/scope-index.ts +197 -0
  119. package/package.json +97 -0
  120. package/types/asset.d.ts +41 -0
  121. package/types/style.d.ts +42 -0
@@ -0,0 +1,58 @@
1
+ import { getStringifyArgs } from '@teambit/legacy.utils';
2
+ import { BitObject } from '../objects';
3
+ import type Ref from '../objects/ref';
4
+
5
+ type ScopeMetaProps = {
6
+ name: string;
7
+ license: string;
8
+ };
9
+
10
+ // TODO: fix parse
11
+ export default class ScopeMeta extends BitObject {
12
+ license: string | null | undefined;
13
+ name: string;
14
+
15
+ constructor(props: ScopeMetaProps) {
16
+ super();
17
+ this.license = props.license;
18
+ this.name = props.name;
19
+ }
20
+
21
+ toObject(): Record<string, any> {
22
+ return {
23
+ license: this.license,
24
+ name: this.name,
25
+ };
26
+ }
27
+
28
+ toString(pretty: boolean): string {
29
+ const args = getStringifyArgs(pretty);
30
+ return JSON.stringify(this.toObject(), ...args);
31
+ }
32
+
33
+ id(): string {
34
+ return this.name;
35
+ }
36
+
37
+ toBuffer(pretty): Buffer {
38
+ return Buffer.from(this.toString(pretty));
39
+ }
40
+
41
+ static fromScopeName(name: string): Ref {
42
+ // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
43
+ return ScopeMeta.fromObject({ name }).hash();
44
+ }
45
+
46
+ static parse(propsStr: string | Buffer): ScopeMeta {
47
+ // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
48
+ return this.fromObject(JSON.parse(propsStr));
49
+ }
50
+
51
+ static fromObject(props: ScopeMetaProps): ScopeMeta {
52
+ return new ScopeMeta(props);
53
+ }
54
+
55
+ static from(props: ScopeMetaProps): ScopeMeta {
56
+ return ScopeMeta.fromObject(props);
57
+ }
58
+ }
@@ -0,0 +1,31 @@
1
+ import { BitObject } from '../objects';
2
+
3
+ // TODO: fix .parse
4
+ export default class Source extends BitObject {
5
+ contents: Buffer;
6
+
7
+ constructor(contents: Buffer) {
8
+ super();
9
+ this.contents = contents;
10
+ }
11
+
12
+ id() {
13
+ return this.contents;
14
+ }
15
+
16
+ toBuffer() {
17
+ return this.contents;
18
+ }
19
+
20
+ toString() {
21
+ return this.contents.toString();
22
+ }
23
+
24
+ static parse(contents: Buffer): Source {
25
+ return new Source(contents);
26
+ }
27
+
28
+ static from(buffer: Buffer): Source {
29
+ return new Source(buffer);
30
+ }
31
+ }
@@ -0,0 +1,66 @@
1
+ import { ComponentID } from '@teambit/component-id';
2
+ import { BitId } from '@teambit/legacy-bit-id';
3
+ import { getStringifyArgs } from '@teambit/legacy.utils';
4
+ import { BitObject } from '../objects';
5
+
6
+ export type SymlinkProp = {
7
+ scope: string;
8
+ name: string;
9
+ realScope: string;
10
+ };
11
+
12
+ /**
13
+ * @deprecated
14
+ * this is not used since component-schema 2.0.0, where the component-id is always the full id.
15
+ */
16
+ export default class Symlink extends BitObject {
17
+ scope: string;
18
+ name: string;
19
+ realScope: string;
20
+
21
+ constructor(props: SymlinkProp) {
22
+ super();
23
+ this.scope = props.scope;
24
+ this.name = props.name;
25
+ this.realScope = props.realScope;
26
+ }
27
+
28
+ id(): string {
29
+ return this.name;
30
+ }
31
+
32
+ getRealComponentId(): ComponentID {
33
+ return ComponentID.fromObject({ scope: this.realScope, name: this.name });
34
+ }
35
+
36
+ toComponentId(): ComponentID {
37
+ return this.getRealComponentId();
38
+ }
39
+
40
+ static parse(contents: Buffer): Symlink {
41
+ const rawContent = JSON.parse(contents.toString());
42
+ if (rawContent.box) rawContent.name = `${rawContent.box}/${rawContent.name}`;
43
+ return Symlink.from(rawContent);
44
+ }
45
+
46
+ toObject() {
47
+ return {
48
+ scope: this.scope,
49
+ name: this.name,
50
+ realScope: this.realScope,
51
+ };
52
+ }
53
+
54
+ toBitId(): BitId {
55
+ return new BitId({ name: this.name });
56
+ }
57
+
58
+ toBuffer(pretty?: boolean) {
59
+ const args = getStringifyArgs(pretty);
60
+ return Buffer.from(JSON.stringify(this.toObject(), ...args));
61
+ }
62
+
63
+ static from(props: SymlinkProp) {
64
+ return new Symlink(props);
65
+ }
66
+ }
@@ -0,0 +1,283 @@
1
+ import { Graph, Edge, Node } from '@teambit/graph.cleargraph';
2
+ import { ComponentID } from '@teambit/component-id';
3
+ import { BitError } from '@teambit/bit-error';
4
+ import { compact, difference, uniqBy } from 'lodash';
5
+ import { getStringifyArgs } from '@teambit/legacy.utils';
6
+ import Ref from '../objects/ref';
7
+ import { BitObject } from '../objects';
8
+ import type Version from './version';
9
+ import { getVersionParentsFromVersion } from '@teambit/component.snap-distance';
10
+ import type ModelComponent from './model-component';
11
+
12
+ export type VersionParents = {
13
+ hash: Ref;
14
+ parents: Ref[];
15
+ unrelated?: Ref;
16
+ squashed?: Ref[];
17
+ };
18
+
19
+ export type VersionHistoryGraph = Graph<string | HashMetadata, string>;
20
+
21
+ type VersionHistoryProps = {
22
+ name: string;
23
+ scope: string;
24
+ versions: VersionParents[];
25
+ graphCompleteRefs?: string[];
26
+ };
27
+
28
+ type HashMetadata = {
29
+ tag?: string;
30
+ pointers?: string[];
31
+ };
32
+
33
+ export default class VersionHistory extends BitObject {
34
+ name: string;
35
+ scope: string;
36
+ private versionsObj: { [hash: string]: VersionParents };
37
+ graphCompleteRefs: string[];
38
+ hasChanged = false; // whether the version history has changed since the last persist
39
+ constructor(props: VersionHistoryProps) {
40
+ super();
41
+ this.name = props.name;
42
+ this.scope = props.scope;
43
+ this.versionsObj = this.versionParentsToObject(props.versions);
44
+ this.graphCompleteRefs = props.graphCompleteRefs || [];
45
+ }
46
+
47
+ get versions() {
48
+ return Object.values(this.versionsObj);
49
+ }
50
+
51
+ private versionParentsToObject(versions: VersionParents[]) {
52
+ return versions.reduce((acc, version) => {
53
+ acc[version.hash.hash] = version;
54
+ return acc;
55
+ }, {});
56
+ }
57
+
58
+ id() {
59
+ return `${this.scope}/${this.name}:${VersionHistory.name}`;
60
+ }
61
+
62
+ static fromId(name: string, scope: string) {
63
+ return new VersionHistory({ scope, name, versions: [] });
64
+ }
65
+
66
+ toObject(): Record<string, any> {
67
+ return {
68
+ name: this.name,
69
+ scope: this.scope,
70
+ versions: this.versions.map((v) => ({
71
+ hash: v.hash.toString(),
72
+ parents: v.parents.map((p) => p.toString()),
73
+ unrelated: v.unrelated?.toString(),
74
+ squashed: v.squashed ? v.squashed.map((p) => p.toString()) : undefined,
75
+ })),
76
+ graphCompleteRefs: this.graphCompleteRefs,
77
+ };
78
+ }
79
+
80
+ toString(pretty: boolean): string {
81
+ const args = getStringifyArgs(pretty);
82
+ return JSON.stringify(this.toObject(), ...args);
83
+ }
84
+
85
+ toBuffer(pretty): Buffer {
86
+ return Buffer.from(this.toString(pretty));
87
+ }
88
+
89
+ getVersionData(ref: Ref): VersionParents | undefined {
90
+ return this.versionsObj[ref.toString()];
91
+ }
92
+
93
+ hasHash(ref: Ref) {
94
+ return Boolean(this.getVersionData(ref));
95
+ }
96
+
97
+ addFromVersionsObjects(versions: Version[]) {
98
+ versions.forEach((version) => {
99
+ const exists = this.getVersionData(version.hash());
100
+ if (exists) {
101
+ // just in case the parents got updated as a result of a merge/squash
102
+ exists.parents = version.parents;
103
+ exists.unrelated = version.unrelated?.head;
104
+ exists.squashed = version.squashed?.previousParents;
105
+ } else {
106
+ const versionData = getVersionParentsFromVersion(version);
107
+ this.versionsObj[version.hash().hash] = versionData;
108
+ }
109
+ });
110
+ }
111
+
112
+ isEmpty() {
113
+ return !this.versions.length;
114
+ }
115
+
116
+ getAllHashesFrom(start: Ref): { found?: string[]; missing?: string[] } {
117
+ const item = this.getVersionData(start);
118
+ if (!item) return { missing: [start.toString()] };
119
+ const hashSet = new Set<string>();
120
+ const allHashes: string[] = [];
121
+ const missing: string[] = [];
122
+
123
+ // Use iterative approach with a stack to avoid stack overflow on deep histories
124
+ const stack: VersionParents[] = [item];
125
+ hashSet.add(item.hash.toString());
126
+
127
+ while (stack.length > 0) {
128
+ const ver = stack.pop()!;
129
+ const verHash = ver.hash.toString();
130
+ allHashes.push(verHash);
131
+
132
+ for (const parent of ver.parents) {
133
+ const parentHash = parent.toString();
134
+ if (hashSet.has(parentHash)) continue;
135
+ const parentVer = this.getVersionData(parent);
136
+ if (!parentVer) {
137
+ missing.push(parentHash);
138
+ continue;
139
+ }
140
+ // Mark as processed before pushing to prevent duplicate stack entries
141
+ hashSet.add(parentHash);
142
+ stack.push(parentVer);
143
+ }
144
+ }
145
+
146
+ return { found: allHashes, missing };
147
+ }
148
+
149
+ isRefPartOfHistory(startFrom: Ref, searchFor: Ref) {
150
+ const { found } = this.getAllHashesFrom(startFrom);
151
+ return found?.includes(searchFor.toString());
152
+ }
153
+
154
+ isGraphCompleteSince(ref: Ref) {
155
+ if (this.graphCompleteRefs.includes(ref.toString())) return true;
156
+ const { missing } = this.getAllHashesFrom(ref);
157
+ const isComplete = !missing || !missing.length;
158
+ if (isComplete) {
159
+ this.graphCompleteRefs.push(ref.toString());
160
+ this.hasChanged = true;
161
+ }
162
+ return isComplete;
163
+ }
164
+
165
+ getAllHashesAsString(): string[] {
166
+ return Object.keys(this.versionsObj);
167
+ }
168
+
169
+ merge(versionHistory: VersionHistory) {
170
+ const existingHashes = this.getAllHashesAsString();
171
+ const incomingHashes = versionHistory.getAllHashesAsString();
172
+ const hashesInExistingOnly = difference(existingHashes, incomingHashes);
173
+ const versionsDataOnExistingOnly = this.versions.filter((v) => hashesInExistingOnly.includes(v.hash.toString()));
174
+ const newVersions = [...versionHistory.versions, ...versionsDataOnExistingOnly];
175
+ this.versionsObj = this.versionParentsToObject(newVersions);
176
+ }
177
+
178
+ getAncestor(numOfGenerationsToGoBack: number, ref: Ref): Ref {
179
+ const errorMsg = `unable to get an older parent of ${this.compId.toString()}`;
180
+ const versionData = this.getVersionData(ref);
181
+ if (!versionData)
182
+ throw new BitError(`${errorMsg}, version "${ref.toString()}" was not found in the version history`);
183
+ if (numOfGenerationsToGoBack === 0) return versionData.hash;
184
+ if (!versionData.parents.length) throw new BitError(`${errorMsg}, version "${ref.toString()}" has no parents`);
185
+ const parent = versionData.parents[0];
186
+ return this.getAncestor(numOfGenerationsToGoBack - 1, parent);
187
+ }
188
+
189
+ getGraph(
190
+ modelComponent?: ModelComponent,
191
+ laneHeads?: { [hash: string]: string[] },
192
+ shortHash = false,
193
+ limitVersions?: number
194
+ ): VersionHistoryGraph {
195
+ const refToStr = (ref: Ref) => (shortHash ? ref.toShortString() : ref.toString());
196
+ const graph = new Graph<string | HashMetadata, string>();
197
+ const allVersions = limitVersions ? [...this.versions].slice(-limitVersions) : this.versions;
198
+ const allHashes = allVersions
199
+ .map((v) => compact([v.hash, ...v.parents, ...(v.squashed || []), v.unrelated]))
200
+ .flat();
201
+
202
+ const allHashesUniq = uniqBy(allHashes, 'hash');
203
+
204
+ const getMetadata = (ref: Ref): HashMetadata | undefined => {
205
+ if (!modelComponent || !laneHeads) return undefined;
206
+ const tag = modelComponent.getTagOfRefIfExists(ref);
207
+ const pointers = laneHeads[ref.toString()];
208
+ return { tag, pointers };
209
+ };
210
+ const nodes = allHashesUniq.map((v) => new Node(refToStr(v), getMetadata(v) || refToStr(v)));
211
+
212
+ const edges = allVersions
213
+ .map((v) => {
214
+ const verEdges = v.parents.map((p) => new Edge(refToStr(v.hash), refToStr(p), 'parent'));
215
+ if (v.unrelated) verEdges.push(new Edge(refToStr(v.hash), refToStr(v.unrelated), 'unrelated'));
216
+ if (v.squashed) {
217
+ const squashed = v.squashed.filter((s) => !v.parents.find((p) => p.isEqual(s)));
218
+ squashed.map((p) => verEdges.push(new Edge(refToStr(v.hash), refToStr(p), 'squashed')));
219
+ }
220
+ return verEdges;
221
+ })
222
+ .flat();
223
+ graph.setNodes(nodes);
224
+ graph.setEdges(edges);
225
+ return graph;
226
+ }
227
+
228
+ get compId() {
229
+ return ComponentID.fromObject({ scope: this.scope, name: this.name });
230
+ }
231
+
232
+ static create(name: string, scope: string, versions: VersionParents[]) {
233
+ return new VersionHistory({
234
+ name,
235
+ scope,
236
+ versions,
237
+ });
238
+ }
239
+
240
+ static parse(contents: string): VersionHistory {
241
+ const parsed = JSON.parse(contents);
242
+ const props: VersionHistoryProps = {
243
+ name: parsed.name,
244
+ scope: parsed.scope,
245
+ versions: parsed.versions.map((ver) => ({
246
+ hash: Ref.from(ver.hash),
247
+ parents: ver.parents.map((p) => Ref.from(p)),
248
+ unrelated: ver.unrelated ? Ref.from(ver.unrelated) : undefined,
249
+ squashed: ver.squashed ? ver.squashed.map((p) => Ref.from(p)) : undefined,
250
+ })),
251
+ graphCompleteRefs: parsed.graphCompleteRefs,
252
+ };
253
+ return new VersionHistory(props);
254
+ }
255
+ }
256
+
257
+ export function versionParentsToGraph(versions: VersionParents[]): Graph<string, string> {
258
+ const refToStr = (ref: Ref) => ref.toString();
259
+ const graph = new Graph<string, string>();
260
+ const allHashes = uniqBy(
261
+ versions
262
+ .map((v) => {
263
+ return compact([v.hash, ...v.parents, ...(v.squashed || []), v.unrelated]);
264
+ })
265
+ .flat(),
266
+ 'hash'
267
+ );
268
+ const nodes = allHashes.map((v) => new Node(refToStr(v), refToStr(v)));
269
+ const edges = versions
270
+ .map((v) => {
271
+ const verEdges = v.parents.map((p) => new Edge(refToStr(v.hash), refToStr(p), 'parent'));
272
+ if (v.unrelated) verEdges.push(new Edge(refToStr(v.hash), refToStr(v.unrelated), 'unrelated'));
273
+ if (v.squashed) {
274
+ const squashed = v.squashed.filter((s) => !v.parents.find((p) => p.isEqual(s)));
275
+ squashed.map((p) => verEdges.push(new Edge(refToStr(v.hash), refToStr(p), 'squashed')));
276
+ }
277
+ return verEdges;
278
+ })
279
+ .flat();
280
+ graph.setNodes(nodes);
281
+ graph.setEdges(edges);
282
+ return graph;
283
+ }