@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,60 @@
1
+ import { getStringifyArgs } from '@teambit/legacy.utils';
2
+ import { BitObject } from '../objects';
3
+ import Ref from '../objects/ref';
4
+
5
+ type ScopeMetaProps = {
6
+ name: string;
7
+ license: string;
8
+ };
9
+
10
+ // TODO: fix parse
11
+ // @ts-ignore
12
+ export default class ScopeMeta extends BitObject {
13
+ license: string | null | undefined;
14
+ name: string;
15
+
16
+ constructor(props: ScopeMetaProps) {
17
+ super();
18
+ this.license = props.license;
19
+ this.name = props.name;
20
+ }
21
+
22
+ toObject(): Record<string, any> {
23
+ return {
24
+ license: this.license,
25
+ name: this.name,
26
+ };
27
+ }
28
+
29
+ // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
30
+ toString(pretty: boolean): string {
31
+ const args = getStringifyArgs(pretty);
32
+ return JSON.stringify(this.toObject(), ...args);
33
+ }
34
+
35
+ id(): string {
36
+ return this.name;
37
+ }
38
+
39
+ toBuffer(pretty): Buffer {
40
+ return Buffer.from(this.toString(pretty));
41
+ }
42
+
43
+ static fromScopeName(name: string): Ref {
44
+ // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
45
+ return ScopeMeta.fromObject({ name }).hash();
46
+ }
47
+
48
+ static parse(propsStr: string | Buffer): ScopeMeta {
49
+ // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
50
+ return this.fromObject(JSON.parse(propsStr));
51
+ }
52
+
53
+ static fromObject(props: ScopeMetaProps): ScopeMeta {
54
+ return new ScopeMeta(props);
55
+ }
56
+
57
+ static from(props: ScopeMetaProps): ScopeMeta {
58
+ return ScopeMeta.fromObject(props);
59
+ }
60
+ }
@@ -0,0 +1,32 @@
1
+ import { BitObject } from '../objects';
2
+
3
+ // TODO: fix .parse
4
+ // @ts-ignore
5
+ export default class Source extends BitObject {
6
+ contents: Buffer;
7
+
8
+ constructor(contents: Buffer) {
9
+ super();
10
+ this.contents = contents;
11
+ }
12
+
13
+ id() {
14
+ return this.contents;
15
+ }
16
+
17
+ toBuffer() {
18
+ return this.contents;
19
+ }
20
+
21
+ toString() {
22
+ return this.contents.toString();
23
+ }
24
+
25
+ static parse(contents: Buffer): Source {
26
+ return new Source(contents);
27
+ }
28
+
29
+ static from(buffer: Buffer): Source {
30
+ return new Source(buffer);
31
+ }
32
+ }
@@ -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,266 @@
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 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 allHashes: string[] = [item.hash.toString()];
120
+ const missing: string[] = [];
121
+ const addHashesRecursively = (ver: VersionParents) => {
122
+ ver.parents.forEach((parent) => {
123
+ if (allHashes.includes(parent.toString())) return;
124
+ const parentVer = this.getVersionData(parent);
125
+ if (!parentVer) {
126
+ missing.push(parent.toString());
127
+ return;
128
+ }
129
+ allHashes.push(parent.toString());
130
+ if (parentVer.parents.length) addHashesRecursively(parentVer);
131
+ });
132
+ };
133
+ addHashesRecursively(item);
134
+ return { found: allHashes, missing };
135
+ }
136
+
137
+ isRefPartOfHistory(startFrom: Ref, searchFor: Ref) {
138
+ const { found } = this.getAllHashesFrom(startFrom);
139
+ return found?.includes(searchFor.toString());
140
+ }
141
+
142
+ isGraphCompleteSince(ref: Ref) {
143
+ if (this.graphCompleteRefs.includes(ref.toString())) return true;
144
+ const { missing } = this.getAllHashesFrom(ref);
145
+ const isComplete = !missing || !missing.length;
146
+ if (isComplete) {
147
+ this.graphCompleteRefs.push(ref.toString());
148
+ this.hasChanged = true;
149
+ }
150
+ return isComplete;
151
+ }
152
+
153
+ getAllHashesAsString(): string[] {
154
+ return Object.keys(this.versionsObj);
155
+ }
156
+
157
+ merge(versionHistory: VersionHistory) {
158
+ const existingHashes = this.getAllHashesAsString();
159
+ const incomingHashes = versionHistory.getAllHashesAsString();
160
+ const hashesInExistingOnly = difference(existingHashes, incomingHashes);
161
+ const versionsDataOnExistingOnly = this.versions.filter((v) => hashesInExistingOnly.includes(v.hash.toString()));
162
+ const newVersions = [...versionHistory.versions, ...versionsDataOnExistingOnly];
163
+ this.versionsObj = this.versionParentsToObject(newVersions);
164
+ }
165
+
166
+ getAncestor(numOfGenerationsToGoBack: number, ref: Ref): Ref {
167
+ const errorMsg = `unable to get an older parent of ${this.compId.toString()}`;
168
+ const versionData = this.getVersionData(ref);
169
+ if (!versionData)
170
+ throw new BitError(`${errorMsg}, version "${ref.toString()}" was not found in the version history`);
171
+ if (numOfGenerationsToGoBack === 0) return versionData.hash;
172
+ if (!versionData.parents.length) throw new BitError(`${errorMsg}, version "${ref.toString()}" has no parents`);
173
+ const parent = versionData.parents[0];
174
+ return this.getAncestor(numOfGenerationsToGoBack - 1, parent);
175
+ }
176
+
177
+ getGraph(
178
+ modelComponent?: ModelComponent,
179
+ laneHeads?: { [hash: string]: string[] },
180
+ shortHash = false
181
+ ): VersionHistoryGraph {
182
+ const refToStr = (ref: Ref) => (shortHash ? ref.toShortString() : ref.toString());
183
+ const graph = new Graph<string | HashMetadata, string>();
184
+ const allHashes = this.versions
185
+ .map((v) => compact([v.hash, ...v.parents, ...(v.squashed || []), v.unrelated]))
186
+ .flat();
187
+
188
+ const getMetadata = (ref: Ref): HashMetadata | undefined => {
189
+ if (!modelComponent || !laneHeads) return undefined;
190
+ const tag = modelComponent.getTagOfRefIfExists(ref);
191
+ const pointers = laneHeads[ref.toString()];
192
+ return { tag, pointers };
193
+ };
194
+ const nodes = allHashes.map((v) => new Node(refToStr(v), getMetadata(v) || refToStr(v)));
195
+ const edges = this.versions
196
+ .map((v) => {
197
+ const verEdges = v.parents.map((p) => new Edge(refToStr(v.hash), refToStr(p), 'parent'));
198
+ if (v.unrelated) verEdges.push(new Edge(refToStr(v.hash), refToStr(v.unrelated), 'unrelated'));
199
+ if (v.squashed) {
200
+ const squashed = v.squashed.filter((s) => !v.parents.find((p) => p.isEqual(s)));
201
+ squashed.map((p) => verEdges.push(new Edge(refToStr(v.hash), refToStr(p), 'squashed')));
202
+ }
203
+ return verEdges;
204
+ })
205
+ .flat();
206
+ graph.setNodes(nodes);
207
+ graph.setEdges(edges);
208
+ return graph;
209
+ }
210
+
211
+ get compId() {
212
+ return ComponentID.fromObject({ scope: this.scope, name: this.name });
213
+ }
214
+
215
+ static create(name: string, scope: string, versions: VersionParents[]) {
216
+ return new VersionHistory({
217
+ name,
218
+ scope,
219
+ versions,
220
+ });
221
+ }
222
+
223
+ static parse(contents: string): VersionHistory {
224
+ const parsed = JSON.parse(contents);
225
+ const props: VersionHistoryProps = {
226
+ name: parsed.name,
227
+ scope: parsed.scope,
228
+ versions: parsed.versions.map((ver) => ({
229
+ hash: Ref.from(ver.hash),
230
+ parents: ver.parents.map((p) => Ref.from(p)),
231
+ unrelated: ver.unrelated ? Ref.from(ver.unrelated) : undefined,
232
+ squashed: ver.squashed ? ver.squashed.map((p) => Ref.from(p)) : undefined,
233
+ })),
234
+ graphCompleteRefs: parsed.graphCompleteRefs,
235
+ };
236
+ return new VersionHistory(props);
237
+ }
238
+ }
239
+
240
+ export function versionParentsToGraph(versions: VersionParents[]): Graph<string, string> {
241
+ const refToStr = (ref: Ref) => ref.toString();
242
+ const graph = new Graph<string, string>();
243
+ const allHashes = uniqBy(
244
+ versions
245
+ .map((v) => {
246
+ return compact([v.hash, ...v.parents, ...(v.squashed || []), v.unrelated]);
247
+ })
248
+ .flat(),
249
+ 'hash'
250
+ );
251
+ const nodes = allHashes.map((v) => new Node(refToStr(v), refToStr(v)));
252
+ const edges = versions
253
+ .map((v) => {
254
+ const verEdges = v.parents.map((p) => new Edge(refToStr(v.hash), refToStr(p), 'parent'));
255
+ if (v.unrelated) verEdges.push(new Edge(refToStr(v.hash), refToStr(v.unrelated), 'unrelated'));
256
+ if (v.squashed) {
257
+ const squashed = v.squashed.filter((s) => !v.parents.find((p) => p.isEqual(s)));
258
+ squashed.map((p) => verEdges.push(new Edge(refToStr(v.hash), refToStr(p), 'squashed')));
259
+ }
260
+ return verEdges;
261
+ })
262
+ .flat();
263
+ graph.setNodes(nodes);
264
+ graph.setEdges(edges);
265
+ return graph;
266
+ }