@teambit/legacy-bit-id 1.0.4 → 1.1.1
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.
- package/bit-id.spec.ts +225 -0
- package/bit-id.ts +314 -0
- package/bit-ids.spec.ts +96 -0
- package/bit-ids.ts +195 -0
- package/dist/bit-id.js +290 -344
- package/dist/bit-id.js.map +1 -1
- package/dist/bit-id.spec.js +224 -349
- package/dist/bit-id.spec.js.map +1 -1
- package/dist/bit-ids.d.ts +52 -0
- package/dist/bit-ids.js +174 -0
- package/dist/bit-ids.js.map +1 -0
- package/dist/bit-ids.spec.d.ts +1 -0
- package/dist/bit-ids.spec.js +92 -0
- package/dist/bit-ids.spec.js.map +1 -0
- package/dist/exceptions/index.js +15 -71
- package/dist/exceptions/index.js.map +1 -1
- package/dist/exceptions/invalid-bit-id.js +6 -21
- package/dist/exceptions/invalid-bit-id.js.map +1 -1
- package/dist/exceptions/invalid-id-chunk.js +6 -21
- package/dist/exceptions/invalid-id-chunk.js.map +1 -1
- package/dist/exceptions/invalid-name.js +6 -21
- package/dist/exceptions/invalid-name.js.map +1 -1
- package/dist/exceptions/invalid-scope-name-from-remote.js +7 -18
- package/dist/exceptions/invalid-scope-name-from-remote.js.map +1 -1
- package/dist/exceptions/invalid-scope-name.js +7 -23
- package/dist/exceptions/invalid-scope-name.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +41 -90
- package/dist/index.js.map +1 -1
- package/dist/tsconfig.json +32 -0
- package/dist/utils/is-valid-id-chunk.js +8 -10
- package/dist/utils/is-valid-id-chunk.js.map +1 -1
- package/dist/utils/is-valid-id-chunk.spec.js +21 -32
- package/dist/utils/is-valid-id-chunk.spec.js.map +1 -1
- package/dist/utils/is-valid-scope-name.js +5 -8
- package/dist/utils/is-valid-scope-name.js.map +1 -1
- package/index.ts +18 -0
- package/package.json +5 -13
- package/tsconfig.json +2 -10
- /package/dist/{preview-1698204020177.js → preview-1706301986259.js} +0 -0
package/bit-ids.ts
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { forEach, uniqBy } from 'lodash';
|
|
2
|
+
import { LATEST_VERSION } from '@teambit/component-version';
|
|
3
|
+
import BitId, { BitIdStr } from './bit-id';
|
|
4
|
+
|
|
5
|
+
export default class BitIds extends Array<BitId> {
|
|
6
|
+
serialize(): string[] {
|
|
7
|
+
return this.map((bitId) => bitId.toString());
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
has(bitId: BitId): boolean {
|
|
11
|
+
return Boolean(this.search(bitId));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
hasWithoutVersion(bitId: BitId): boolean {
|
|
15
|
+
return Boolean(this.searchWithoutVersion(bitId));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
hasWithoutScope(bitId: BitId): boolean {
|
|
19
|
+
return Boolean(this.searchWithoutScope(bitId));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
hasWithoutScopeAndVersion(bitId: BitId): boolean {
|
|
23
|
+
return Boolean(this.searchWithoutScopeAndVersion(bitId));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
hasWithoutScopeAndVersionAsString(bitIdStr: BitIdStr): boolean {
|
|
27
|
+
return Boolean(this.find((id) => id.name === bitIdStr));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
search(bitId: BitId): BitId | null | undefined {
|
|
31
|
+
return this.find((id) => id.hasSameName(bitId) && id.hasSameScope(bitId) && id.hasSameVersion(bitId));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
searchWithoutVersion(bitId: BitId): BitId | null | undefined {
|
|
35
|
+
return this.find((id) => id.hasSameName(bitId) && id.hasSameScope(bitId));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
searchWithoutScopeAndVersion(bitId: BitId): BitId | undefined {
|
|
39
|
+
return this.find((id) => id.hasSameName(bitId));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
searchWithoutScope(bitId: BitId): BitId | null | undefined {
|
|
43
|
+
return this.find((id) => id.hasSameName(bitId) && id.hasSameVersion(bitId));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
searchStrWithoutVersion(idStr: string): BitId | null | undefined {
|
|
47
|
+
return this.find((id) => id.toStringWithoutVersion() === idStr);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
searchStrWithoutScopeAndVersion(idStr: string): BitId | null | undefined {
|
|
51
|
+
return this.find((id) => id.toStringWithoutScopeAndVersion() === idStr);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
filterExact(bitId: BitId): BitId[] {
|
|
55
|
+
return this.filter((id) => id.hasSameName(bitId) && id.hasSameScope(bitId) && id.hasSameVersion(bitId));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
filterWithoutVersion(bitId: BitId): BitId[] {
|
|
59
|
+
return this.filter((id) => id.hasSameName(bitId) && id.hasSameScope(bitId));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
filterWithoutScopeAndVersion(bitId: BitId): BitId[] {
|
|
63
|
+
return this.filter((id) => id.hasSameName(bitId));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
removeIfExist(bitId: BitId): BitIds {
|
|
67
|
+
return BitIds.fromArray(this.filter((id) => !id.isEqual(bitId)));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Return ids which are on the current instance and not in the passed list
|
|
72
|
+
* @param bitIds
|
|
73
|
+
*/
|
|
74
|
+
difference(bitIds: BitIds): BitIds {
|
|
75
|
+
return BitIds.fromArray(this.filter((id) => !bitIds.search(id)));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
removeIfExistWithoutVersion(bitId: BitId): BitIds {
|
|
79
|
+
return BitIds.fromArray(this.filter((id) => !id.isEqualWithoutVersion(bitId)));
|
|
80
|
+
}
|
|
81
|
+
removeMultipleIfExistWithoutVersion(bitIds: BitIds): BitIds {
|
|
82
|
+
return BitIds.fromArray(this.filter((id) => !bitIds.hasWithoutVersion(id)));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
toObject() {
|
|
86
|
+
return this.reduce((acc, bitId) => {
|
|
87
|
+
acc[bitId.toString()] = bitId;
|
|
88
|
+
return acc;
|
|
89
|
+
}, {});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* make sure to pass only bit ids you know they have scope, otherwise, you'll get invalid bit ids.
|
|
94
|
+
* this is mainly useful for remote commands where it is impossible to have a component without scope.
|
|
95
|
+
*/
|
|
96
|
+
static deserialize(array: string[] = []): BitIds {
|
|
97
|
+
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
|
|
98
|
+
return new BitIds(...array.map((id) => BitId.parse(id, true)));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
static deserializeObsolete(array: string[] = []): BitIds {
|
|
102
|
+
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
|
|
103
|
+
return new BitIds(...array.map((id) => BitId.parseObsolete(id)));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
toString(): string {
|
|
107
|
+
return this.map((id) => id.toString()).join(', ');
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
toGroupByIdWithoutVersion(): { [idStrWithoutVer: string]: BitIds } {
|
|
111
|
+
return this.reduce((acc, current) => {
|
|
112
|
+
const idStrWithoutVer = current.toStringWithoutVersion();
|
|
113
|
+
if (acc[idStrWithoutVer]) acc[idStrWithoutVer].push(current);
|
|
114
|
+
else acc[idStrWithoutVer] = new BitIds(current);
|
|
115
|
+
return acc;
|
|
116
|
+
}, {});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
toGroupByScopeName(idsWithDefaultScope: BitIds): { [scopeName: string]: BitIds } {
|
|
120
|
+
return this.reduce((acc, current) => {
|
|
121
|
+
const getScopeName = () => {
|
|
122
|
+
if (current.scope) return current.scope;
|
|
123
|
+
const idWithDefaultScope = idsWithDefaultScope.searchWithoutScopeAndVersion(current);
|
|
124
|
+
return idWithDefaultScope ? idWithDefaultScope.scope : null;
|
|
125
|
+
};
|
|
126
|
+
const scopeName = getScopeName();
|
|
127
|
+
if (!scopeName) {
|
|
128
|
+
throw new Error(`toGroupByScopeName() expect ids to have a scope name, got ${current.toString()}`);
|
|
129
|
+
}
|
|
130
|
+
if (acc[scopeName]) acc[scopeName].push(current);
|
|
131
|
+
else acc[scopeName] = new BitIds(current);
|
|
132
|
+
return acc;
|
|
133
|
+
}, {});
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
findDuplicationsIgnoreVersion(): { [id: string]: BitId[] } {
|
|
137
|
+
const duplications = {};
|
|
138
|
+
this.forEach((id) => {
|
|
139
|
+
const sameIds = this.filterWithoutVersion(id);
|
|
140
|
+
if (sameIds.length > 1) {
|
|
141
|
+
duplications[id.toStringWithoutVersion()] = sameIds;
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
return duplications;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
add(bitIds: BitId[]) {
|
|
148
|
+
bitIds.forEach((bitId) => {
|
|
149
|
+
if (!this.search(bitId)) this.push(bitId);
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
static fromObject(dependencies: { [key: string]: string }) {
|
|
154
|
+
const array = [];
|
|
155
|
+
|
|
156
|
+
forEach(dependencies, (version, id) => {
|
|
157
|
+
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
|
|
158
|
+
array.push(BitId.parse(id, true, version)); // bit.json has only imported dependencies, they all have scope
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
|
|
162
|
+
return new BitIds(...array);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
static fromArray(ids: BitId[]): BitIds {
|
|
166
|
+
// don't do `new BitIds(...ids);`, it'll throw "Maximum call stack size exceeded" for large number if ids.
|
|
167
|
+
const bitIds = new BitIds();
|
|
168
|
+
ids.forEach((id) => bitIds.push(id));
|
|
169
|
+
return bitIds;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
static uniqFromArray(bitIds: BitId[]): BitIds {
|
|
173
|
+
const uniq = uniqBy(bitIds, (id) => id.toString());
|
|
174
|
+
return BitIds.fromArray(uniq);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
throwForDuplicationIgnoreVersion() {
|
|
178
|
+
this.forEach((bitId) => {
|
|
179
|
+
const found = this.filterWithoutVersion(bitId);
|
|
180
|
+
if (found.length > 1) {
|
|
181
|
+
throw new Error(`bitIds has "${bitId.toStringWithoutVersion()}" duplicated as following:
|
|
182
|
+
${found.map((id) => id.toString()).join('\n')}`);
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
toVersionLatest(): BitIds {
|
|
188
|
+
return BitIds.uniqFromArray(this.map((id) => id.changeVersion(LATEST_VERSION)));
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
clone(): BitIds {
|
|
192
|
+
const cloneIds = this.map((id) => id.clone());
|
|
193
|
+
return new BitIds(...cloneIds);
|
|
194
|
+
}
|
|
195
|
+
}
|