@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.
Files changed (40) hide show
  1. package/bit-id.spec.ts +225 -0
  2. package/bit-id.ts +314 -0
  3. package/bit-ids.spec.ts +96 -0
  4. package/bit-ids.ts +195 -0
  5. package/dist/bit-id.js +290 -344
  6. package/dist/bit-id.js.map +1 -1
  7. package/dist/bit-id.spec.js +224 -349
  8. package/dist/bit-id.spec.js.map +1 -1
  9. package/dist/bit-ids.d.ts +52 -0
  10. package/dist/bit-ids.js +174 -0
  11. package/dist/bit-ids.js.map +1 -0
  12. package/dist/bit-ids.spec.d.ts +1 -0
  13. package/dist/bit-ids.spec.js +92 -0
  14. package/dist/bit-ids.spec.js.map +1 -0
  15. package/dist/exceptions/index.js +15 -71
  16. package/dist/exceptions/index.js.map +1 -1
  17. package/dist/exceptions/invalid-bit-id.js +6 -21
  18. package/dist/exceptions/invalid-bit-id.js.map +1 -1
  19. package/dist/exceptions/invalid-id-chunk.js +6 -21
  20. package/dist/exceptions/invalid-id-chunk.js.map +1 -1
  21. package/dist/exceptions/invalid-name.js +6 -21
  22. package/dist/exceptions/invalid-name.js.map +1 -1
  23. package/dist/exceptions/invalid-scope-name-from-remote.js +7 -18
  24. package/dist/exceptions/invalid-scope-name-from-remote.js.map +1 -1
  25. package/dist/exceptions/invalid-scope-name.js +7 -23
  26. package/dist/exceptions/invalid-scope-name.js.map +1 -1
  27. package/dist/index.d.ts +2 -1
  28. package/dist/index.js +41 -90
  29. package/dist/index.js.map +1 -1
  30. package/dist/tsconfig.json +32 -0
  31. package/dist/utils/is-valid-id-chunk.js +8 -10
  32. package/dist/utils/is-valid-id-chunk.js.map +1 -1
  33. package/dist/utils/is-valid-id-chunk.spec.js +21 -32
  34. package/dist/utils/is-valid-id-chunk.spec.js.map +1 -1
  35. package/dist/utils/is-valid-scope-name.js +5 -8
  36. package/dist/utils/is-valid-scope-name.js.map +1 -1
  37. package/index.ts +18 -0
  38. package/package.json +5 -13
  39. package/tsconfig.json +2 -10
  40. /package/dist/{preview-1698204020177.js → preview-1706301986259.js} +0 -0
package/dist/bit-id.js CHANGED
@@ -1,350 +1,296 @@
1
1
  "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
5
17
  });
6
- exports.default = exports.VERSION_DELIMITER = void 0;
7
- function _decamelize() {
8
- const data = _interopRequireDefault(require("decamelize"));
9
- _decamelize = function () {
10
- return data;
11
- };
12
- return data;
13
- }
14
- function path() {
15
- const data = _interopRequireWildcard(require("path"));
16
- path = function () {
17
- return data;
18
- };
19
- return data;
20
- }
21
- function semver() {
22
- const data = _interopRequireWildcard(require("semver"));
23
- semver = function () {
24
- return data;
25
- };
26
- return data;
27
- }
28
- function _lodash() {
29
- const data = require("lodash");
30
- _lodash = function () {
31
- return data;
32
- };
33
- return data;
34
- }
35
- function _componentVersion() {
36
- const data = require("@teambit/component-version");
37
- _componentVersion = function () {
38
- return data;
39
- };
40
- return data;
41
- }
42
- function _isValidIdChunk() {
43
- const data = _interopRequireDefault(require("./utils/is-valid-id-chunk"));
44
- _isValidIdChunk = function () {
45
- return data;
46
- };
47
- return data;
48
- }
49
- function _isValidScopeName() {
50
- const data = _interopRequireDefault(require("./utils/is-valid-scope-name"));
51
- _isValidScopeName = function () {
52
- return data;
53
- };
54
- return data;
55
- }
56
- function _exceptions() {
57
- const data = require("./exceptions");
58
- _exceptions = function () {
59
- return data;
60
- };
61
- return data;
62
- }
63
- function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
64
- function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
65
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
66
- function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
67
- function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
68
- function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
69
- const VERSION_DELIMITER = '@';
70
- exports.VERSION_DELIMITER = VERSION_DELIMITER;
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.VERSION_DELIMITER = void 0;
30
+ const decamelize_1 = __importDefault(require("decamelize"));
31
+ const path = __importStar(require("path"));
32
+ const semver = __importStar(require("semver"));
33
+ const lodash_1 = require("lodash");
34
+ const component_version_1 = require("@teambit/component-version");
35
+ const is_valid_id_chunk_1 = __importDefault(require("./utils/is-valid-id-chunk"));
36
+ const is_valid_scope_name_1 = __importDefault(require("./utils/is-valid-scope-name"));
37
+ const exceptions_1 = require("./exceptions");
38
+ exports.VERSION_DELIMITER = '@';
71
39
  class BitId {
72
- constructor({
73
- scope,
74
- box,
75
- name,
76
- version
77
- }) {
78
- _defineProperty(this, "scope", void 0);
79
- _defineProperty(this, "box", void 0);
80
- _defineProperty(this, "name", void 0);
81
- _defineProperty(this, "version", void 0);
82
- // don't validate the id parts using isValidIdChunk here. we instance this class tons of times
83
- // and running regex so many times impact the performance
84
- if (!name) throw new (_exceptions().InvalidName)(name);
85
- this.scope = scope || null;
86
- this.box = undefined;
87
- this.name = box ? `${box}/${name}` : name;
88
- this.version = version || undefined;
89
- Object.freeze(this);
90
- }
91
- clone() {
92
- return new BitId(this);
93
- }
94
- changeScope(newScope) {
95
- return new BitId({
96
- scope: newScope,
97
- name: this.name,
98
- version: this.version
99
- });
100
- }
101
- changeVersion(newVersion) {
102
- return new BitId({
103
- scope: this.scope,
104
- name: this.name,
105
- version: newVersion
106
- });
107
- }
108
- isLocal(scopeName) {
109
- return !this.scope || Boolean(scopeName && scopeName === this.scope);
110
- }
111
- getVersion() {
112
- return (0, _componentVersion().versionParser)(this.version);
113
- }
114
- hasVersion() {
115
- return Boolean(this.version && this.version !== _componentVersion().LATEST_VERSION);
116
- }
117
- hasScope() {
118
- return Boolean(this.scope);
119
- }
120
- hasSameName(id) {
121
- return this.name === id.name;
122
- }
123
- hasSameScope(id) {
124
- if (this.hasScope() && id.hasScope()) return this.scope === id.scope;
125
- if (!this.hasScope() && !id.hasScope()) return true;
126
- return false; // one has scope but not the other
127
- }
128
-
129
- hasSameVersion(id) {
130
- if (this.hasVersion() && id.hasVersion()) return this.version === id.version;
131
- if (!this.hasVersion() && !id.hasVersion()) return true;
132
- return false; // one has version but not the other
133
- }
134
-
135
- toString(ignoreScope = false, ignoreVersion = false) {
136
- const {
137
- name,
138
- version
139
- } = this;
140
- const scope = this.scope;
141
- const componentStr = ignoreScope || !scope ? name : [scope, name].join('/');
142
- // when there is no scope and the version is latest, omit the version.
143
- if (ignoreVersion || !this.hasVersion()) return componentStr;
144
- return componentStr.concat(`${VERSION_DELIMITER}${version}`);
145
- }
146
- toStringWithoutScope() {
147
- return this.toString(true);
148
- }
149
- toStringWithoutVersion() {
150
- return this.toString(false, true);
151
- }
152
- toStringWithoutScopeAndVersion() {
153
- return this.toString(true, true);
154
- }
155
- isEqual(bitId) {
156
- return this.hasSameName(bitId) && this.hasSameScope(bitId) && this.hasSameVersion(bitId);
157
- }
158
- isEqualWithoutVersion(bitId) {
159
- return this.hasSameName(bitId) && this.hasSameScope(bitId);
160
- }
161
- isEqualWithoutScopeAndVersion(bitId) {
162
- return this.hasSameName(bitId);
163
- }
164
- serialize() {
165
- const obj = {
166
- scope: this.scope,
167
- name: this.name,
168
- version: this.version
169
- };
170
- if (!this.hasVersion()) delete obj.version;
171
- if (!this.hasScope()) delete obj.scope;
172
- return obj;
173
- }
174
- toFullPath() {
175
- if (!this.scope || !this.version) {
176
- throw new Error('BitId.toFullPath is unable to generate a path without a scope or a version');
177
- }
178
- return path().join(this.name, this.scope, this.version);
179
- }
180
- isVersionSnap() {
181
- return !(0, _componentVersion().isTag)(this.version);
182
- }
183
-
184
- /**
185
- * examples:
186
- * 1.0.0 => null
187
- * 1.0.0-dev.1 => ['dev', 1]
188
- * 1.0.0-dev.1.alpha.2 => ['dev', 1, 'alpha', 2]
189
- * 1.0.0-0 => [0]
190
- */
191
- getVersionPreReleaseData() {
192
- if (!this.version) return null;
193
- return semver().prerelease(this.version);
194
- }
195
-
196
- /**
197
- * Get a string id and return a string without the version part
198
- * @param {string} id
199
- * @return {string} id - id without version
200
- */
201
- static getStringWithoutVersion(id) {
202
- const splitted = id.split(VERSION_DELIMITER);
203
- let res = splitted[0];
204
- // the delimiter is @. now with the new owner prefix
205
- // many times the id starts with the @ sign as part of the @owner prefix
206
- // do not treat this @ at the beginning as the version delimiter
207
- if (id.startsWith(VERSION_DELIMITER)) {
208
- res = `${VERSION_DELIMITER}${splitted[1]}`;
209
- }
210
- return res;
211
- }
212
- static getVersionOnlyFromString(id) {
213
- return id.split(VERSION_DELIMITER)[1];
214
- }
215
- static parse(id, hasScope = true, version = _componentVersion().LATEST_VERSION) {
216
- if (typeof id !== 'string') {
217
- throw new TypeError(`BitId.parse expects to get "id" as a string, instead, got ${typeof id}`);
218
- }
219
- if (id.includes(VERSION_DELIMITER) && id.lastIndexOf(VERSION_DELIMITER) > 0) {
220
- const [newId, newVersion] = id.split(VERSION_DELIMITER);
221
- id = newId;
222
- version = newVersion;
223
- }
224
- const getScopeAndName = () => {
225
- if (hasScope) {
226
- return BitId.getScopeAndName(id);
227
- }
228
- return {
229
- scope: undefined,
230
- name: id
231
- };
232
- };
233
- const {
234
- scope,
235
- name
236
- } = getScopeAndName();
237
- if (!(0, _isValidIdChunk().default)(name)) throw new (_exceptions().InvalidName)(name);
238
- if (scope && !(0, _isValidScopeName().default)(scope)) {
239
- throw new (_exceptions().InvalidScopeName)(scope, id);
240
- }
241
- return new BitId({
242
- scope,
243
- name,
244
- version
245
- });
246
- }
247
- static getScopeAndName(id) {
248
- const delimiterIndex = id.indexOf('/');
249
- if (delimiterIndex < 0) throw new (_exceptions().InvalidBitId)(id);
250
- const scope = id.substring(0, delimiterIndex);
251
- const name = id.substring(delimiterIndex + 1);
252
- return {
253
- scope,
254
- name
255
- };
256
- }
257
- static parseObsolete(id, version = _componentVersion().LATEST_VERSION) {
258
- if (id.includes(VERSION_DELIMITER)) {
259
- const [newId, newVersion] = id.split(VERSION_DELIMITER);
260
- id = newId;
261
- version = newVersion;
262
- }
263
- const idSplit = id.split('/');
264
- if (idSplit.length === 3) {
265
- const [scope, box, name] = idSplit;
266
- if (!(0, _isValidIdChunk().default)(name, false) || !(0, _isValidIdChunk().default)(box, false) || !(0, _isValidScopeName().default)(scope)) {
267
- throw new (_exceptions().InvalidIdChunk)(`${scope}/${box}/${name}`);
268
- }
269
- // $FlowFixMe (in this case the realScopeName is not undefined)
270
- return new BitId({
271
- scope,
272
- box,
273
- name,
274
- version
275
- });
276
- }
277
- if (idSplit.length === 2) {
278
- const [box, name] = idSplit;
279
- if (!(0, _isValidIdChunk().default)(name, false) || !(0, _isValidIdChunk().default)(box, false)) {
280
- throw new (_exceptions().InvalidIdChunk)(`${box}/${name}`);
281
- }
282
- return new BitId({
283
- box,
284
- name,
285
- version
286
- });
287
- }
288
- if (idSplit.length === 1) {
289
- const [name] = idSplit;
290
- if (!(0, _isValidIdChunk().default)(name)) {
291
- throw new (_exceptions().InvalidIdChunk)(name);
292
- }
293
- return new BitId({
294
- name,
295
- version
296
- });
297
- }
298
- throw new (_exceptions().InvalidBitId)(id);
299
- }
300
-
301
- /**
302
- * before version 13.0.3 bitmap and component-dependencies ids were written as strings (e.g. scope/box/name@version)
303
- * since that version the ids are written as objects ({ scope: scopeName, name: compName, version: 0.0.1 })
304
- */
305
- static parseBackwardCompatible(id) {
306
- return typeof id === 'string' ? BitId.parseObsolete(id) : new BitId(id);
307
- }
308
- static getValidScopeName(scope) {
309
- const suggestedName = scope.toLowerCase();
310
- let cleanName = suggestedName.split('').map(char => {
311
- if (/^[$\-_!.a-z0-9]+$/.test(char)) return char;
312
- return '';
313
- }).join('');
314
-
315
- // allow only one dot
316
- const nameSplitByDot = cleanName.split('.');
317
- if (nameSplitByDot.length > 1) {
318
- cleanName = `${(0, _lodash().head)(nameSplitByDot)}.${(0, _lodash().tail)(nameSplitByDot).join('')}`;
319
- }
320
- if (!cleanName) {
321
- // @todo: change it to BitError
322
- throw new Error('scope name created by directory name have to contains at least one character or number');
323
- }
324
- return cleanName;
325
- }
326
- static getValidIdChunk(chunk) {
327
- if (!(0, _isValidIdChunk().default)(chunk)) {
328
- chunk = chunk.replace(/\./g, ''); // remove "."
329
- chunk = chunk.replace(/ /g, '-'); // replace a space with a dash.
330
- chunk = (0, _decamelize().default)(chunk, '-');
331
- }
332
- return chunk;
333
- }
334
- static getValidBitId(box, name) {
335
- return new BitId({
336
- name: BitId.getValidIdChunk(name),
337
- box: box ? BitId.getValidIdChunk(box) : undefined
338
- });
339
- }
340
- static isValidVersion(version) {
341
- // a version can be a tag (semver) or a snap (hash)
342
- return (0, _componentVersion().isTag)(version) || (0, _componentVersion().isHash)(version);
343
- }
344
- static isValidSemver(version) {
345
- return Boolean(semver().valid(version));
346
- }
40
+ constructor({ scope, box, name, version }) {
41
+ // don't validate the id parts using isValidIdChunk here. we instance this class tons of times
42
+ // and running regex so many times impact the performance
43
+ if (!name)
44
+ throw new exceptions_1.InvalidName(name);
45
+ this.scope = scope || null;
46
+ this.box = undefined;
47
+ this.name = box ? `${box}/${name}` : name;
48
+ this.version = version || undefined;
49
+ Object.freeze(this);
50
+ }
51
+ clone() {
52
+ return new BitId(this);
53
+ }
54
+ changeScope(newScope) {
55
+ return new BitId({ scope: newScope, name: this.name, version: this.version });
56
+ }
57
+ changeVersion(newVersion) {
58
+ return new BitId({ scope: this.scope, name: this.name, version: newVersion });
59
+ }
60
+ isLocal(scopeName) {
61
+ return !this.scope || Boolean(scopeName && scopeName === this.scope);
62
+ }
63
+ getVersion() {
64
+ return (0, component_version_1.versionParser)(this.version);
65
+ }
66
+ hasVersion() {
67
+ return Boolean(this.version && this.version !== component_version_1.LATEST_VERSION);
68
+ }
69
+ hasScope() {
70
+ return Boolean(this.scope);
71
+ }
72
+ hasSameName(id) {
73
+ return this.name === id.name;
74
+ }
75
+ hasSameScope(id) {
76
+ if (this.hasScope() && id.hasScope())
77
+ return this.scope === id.scope;
78
+ if (!this.hasScope() && !id.hasScope())
79
+ return true;
80
+ return false; // one has scope but not the other
81
+ }
82
+ hasSameVersion(id) {
83
+ if (this.hasVersion() && id.hasVersion())
84
+ return this.version === id.version;
85
+ if (!this.hasVersion() && !id.hasVersion())
86
+ return true;
87
+ return false; // one has version but not the other
88
+ }
89
+ toString(ignoreScope = false, ignoreVersion = false) {
90
+ const { name, version } = this;
91
+ const scope = this.scope;
92
+ const componentStr = ignoreScope || !scope ? name : [scope, name].join('/');
93
+ // when there is no scope and the version is latest, omit the version.
94
+ if (ignoreVersion || !this.hasVersion())
95
+ return componentStr;
96
+ return componentStr.concat(`${exports.VERSION_DELIMITER}${version}`);
97
+ }
98
+ toStringWithoutScope() {
99
+ return this.toString(true);
100
+ }
101
+ toStringWithoutVersion() {
102
+ return this.toString(false, true);
103
+ }
104
+ toStringWithoutScopeAndVersion() {
105
+ return this.toString(true, true);
106
+ }
107
+ isEqual(bitId) {
108
+ return this.hasSameName(bitId) && this.hasSameScope(bitId) && this.hasSameVersion(bitId);
109
+ }
110
+ isEqualWithoutVersion(bitId) {
111
+ return this.hasSameName(bitId) && this.hasSameScope(bitId);
112
+ }
113
+ isEqualWithoutScopeAndVersion(bitId) {
114
+ return this.hasSameName(bitId);
115
+ }
116
+ serialize() {
117
+ const obj = { scope: this.scope, name: this.name, version: this.version };
118
+ if (!this.hasVersion())
119
+ delete obj.version;
120
+ if (!this.hasScope())
121
+ delete obj.scope;
122
+ return obj;
123
+ }
124
+ toFullPath() {
125
+ if (!this.scope || !this.version) {
126
+ throw new Error('BitId.toFullPath is unable to generate a path without a scope or a version');
127
+ }
128
+ return path.join(this.name, this.scope, this.version);
129
+ }
130
+ isVersionSnap() {
131
+ return !(0, component_version_1.isTag)(this.version);
132
+ }
133
+ /**
134
+ * examples:
135
+ * 1.0.0 => null
136
+ * 1.0.0-dev.1 => ['dev', 1]
137
+ * 1.0.0-dev.1.alpha.2 => ['dev', 1, 'alpha', 2]
138
+ * 1.0.0-0 => [0]
139
+ */
140
+ getVersionPreReleaseData() {
141
+ if (!this.version)
142
+ return null;
143
+ return semver.prerelease(this.version);
144
+ }
145
+ /**
146
+ * Get a string id and return a string without the version part
147
+ * @param {string} id
148
+ * @return {string} id - id without version
149
+ */
150
+ static getStringWithoutVersion(id) {
151
+ const splitted = id.split(exports.VERSION_DELIMITER);
152
+ let res = splitted[0];
153
+ // the delimiter is @. now with the new owner prefix
154
+ // many times the id starts with the @ sign as part of the @owner prefix
155
+ // do not treat this @ at the beginning as the version delimiter
156
+ if (id.startsWith(exports.VERSION_DELIMITER)) {
157
+ res = `${exports.VERSION_DELIMITER}${splitted[1]}`;
158
+ }
159
+ return res;
160
+ }
161
+ static getVersionOnlyFromString(id) {
162
+ return id.split(exports.VERSION_DELIMITER)[1];
163
+ }
164
+ static parse(id, hasScope = true, version = component_version_1.LATEST_VERSION) {
165
+ if (typeof id !== 'string') {
166
+ throw new TypeError(`BitId.parse expects to get "id" as a string, instead, got ${typeof id}`);
167
+ }
168
+ if (id.includes(exports.VERSION_DELIMITER) && id.lastIndexOf(exports.VERSION_DELIMITER) > 0) {
169
+ const [newId, newVersion] = id.split(exports.VERSION_DELIMITER);
170
+ id = newId;
171
+ version = newVersion;
172
+ }
173
+ const getScopeAndName = () => {
174
+ if (hasScope) {
175
+ return BitId.getScopeAndName(id);
176
+ }
177
+ return {
178
+ scope: undefined,
179
+ name: id,
180
+ };
181
+ };
182
+ const { scope, name } = getScopeAndName();
183
+ if (!(0, is_valid_id_chunk_1.default)(name))
184
+ throw new exceptions_1.InvalidName(name);
185
+ if (scope && !(0, is_valid_scope_name_1.default)(scope)) {
186
+ throw new exceptions_1.InvalidScopeName(scope, id);
187
+ }
188
+ return new BitId({
189
+ scope,
190
+ name,
191
+ version,
192
+ });
193
+ }
194
+ static getScopeAndName(id) {
195
+ const delimiterIndex = id.indexOf('/');
196
+ if (delimiterIndex < 0)
197
+ throw new exceptions_1.InvalidBitId(id);
198
+ const scope = id.substring(0, delimiterIndex);
199
+ const name = id.substring(delimiterIndex + 1);
200
+ return {
201
+ scope,
202
+ name,
203
+ };
204
+ }
205
+ static parseObsolete(id, version = component_version_1.LATEST_VERSION) {
206
+ if (id.includes(exports.VERSION_DELIMITER)) {
207
+ const [newId, newVersion] = id.split(exports.VERSION_DELIMITER);
208
+ id = newId;
209
+ version = newVersion;
210
+ }
211
+ const idSplit = id.split('/');
212
+ if (idSplit.length === 3) {
213
+ const [scope, box, name] = idSplit;
214
+ if (!(0, is_valid_id_chunk_1.default)(name, false) || !(0, is_valid_id_chunk_1.default)(box, false) || !(0, is_valid_scope_name_1.default)(scope)) {
215
+ throw new exceptions_1.InvalidIdChunk(`${scope}/${box}/${name}`);
216
+ }
217
+ // $FlowFixMe (in this case the realScopeName is not undefined)
218
+ return new BitId({
219
+ scope,
220
+ box,
221
+ name,
222
+ version,
223
+ });
224
+ }
225
+ if (idSplit.length === 2) {
226
+ const [box, name] = idSplit;
227
+ if (!(0, is_valid_id_chunk_1.default)(name, false) || !(0, is_valid_id_chunk_1.default)(box, false)) {
228
+ throw new exceptions_1.InvalidIdChunk(`${box}/${name}`);
229
+ }
230
+ return new BitId({
231
+ box,
232
+ name,
233
+ version,
234
+ });
235
+ }
236
+ if (idSplit.length === 1) {
237
+ const [name] = idSplit;
238
+ if (!(0, is_valid_id_chunk_1.default)(name)) {
239
+ throw new exceptions_1.InvalidIdChunk(name);
240
+ }
241
+ return new BitId({
242
+ name,
243
+ version,
244
+ });
245
+ }
246
+ throw new exceptions_1.InvalidBitId(id);
247
+ }
248
+ /**
249
+ * before version 13.0.3 bitmap and component-dependencies ids were written as strings (e.g. scope/box/name@version)
250
+ * since that version the ids are written as objects ({ scope: scopeName, name: compName, version: 0.0.1 })
251
+ */
252
+ static parseBackwardCompatible(id) {
253
+ return typeof id === 'string' ? BitId.parseObsolete(id) : new BitId(id);
254
+ }
255
+ static getValidScopeName(scope) {
256
+ const suggestedName = scope.toLowerCase();
257
+ let cleanName = suggestedName
258
+ .split('')
259
+ .map((char) => {
260
+ if (/^[$\-_!.a-z0-9]+$/.test(char))
261
+ return char;
262
+ return '';
263
+ })
264
+ .join('');
265
+ // allow only one dot
266
+ const nameSplitByDot = cleanName.split('.');
267
+ if (nameSplitByDot.length > 1) {
268
+ cleanName = `${(0, lodash_1.head)(nameSplitByDot)}.${(0, lodash_1.tail)(nameSplitByDot).join('')}`;
269
+ }
270
+ if (!cleanName) {
271
+ // @todo: change it to BitError
272
+ throw new Error('scope name created by directory name have to contains at least one character or number');
273
+ }
274
+ return cleanName;
275
+ }
276
+ static getValidIdChunk(chunk) {
277
+ if (!(0, is_valid_id_chunk_1.default)(chunk)) {
278
+ chunk = chunk.replace(/\./g, ''); // remove "."
279
+ chunk = chunk.replace(/ /g, '-'); // replace a space with a dash.
280
+ chunk = (0, decamelize_1.default)(chunk, '-');
281
+ }
282
+ return chunk;
283
+ }
284
+ static getValidBitId(box, name) {
285
+ return new BitId({ name: BitId.getValidIdChunk(name), box: box ? BitId.getValidIdChunk(box) : undefined });
286
+ }
287
+ static isValidVersion(version) {
288
+ // a version can be a tag (semver) or a snap (hash)
289
+ return (0, component_version_1.isTag)(version) || (0, component_version_1.isHash)(version);
290
+ }
291
+ static isValidSemver(version) {
292
+ return Boolean(semver.valid(version));
293
+ }
347
294
  }
348
295
  exports.default = BitId;
349
-
350
296
  //# sourceMappingURL=bit-id.js.map