@teambit/aspect-loader 0.0.880 → 0.0.882

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.
@@ -1,228 +1,160 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
4
  require("core-js/modules/es.array.iterator.js");
6
-
7
5
  require("core-js/modules/es.promise.js");
8
-
9
6
  require("core-js/modules/es.regexp.exec.js");
10
-
11
7
  Object.defineProperty(exports, "__esModule", {
12
8
  value: true
13
9
  });
14
10
  exports.default = exports.AspectLoaderMain = void 0;
15
-
16
11
  function _defineProperty2() {
17
12
  const data = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
18
-
19
13
  _defineProperty2 = function () {
20
14
  return data;
21
15
  };
22
-
23
16
  return data;
24
17
  }
25
-
26
18
  function _path() {
27
19
  const data = require("path");
28
-
29
20
  _path = function () {
30
21
  return data;
31
22
  };
32
-
33
23
  return data;
34
24
  }
35
-
36
25
  function _legacyBitId() {
37
26
  const data = require("@teambit/legacy-bit-id");
38
-
39
27
  _legacyBitId = function () {
40
28
  return data;
41
29
  };
42
-
43
30
  return data;
44
31
  }
45
-
46
32
  function _scope() {
47
33
  const data = _interopRequireDefault(require("@teambit/legacy/dist/scope/scope"));
48
-
49
34
  _scope = function () {
50
35
  return data;
51
36
  };
52
-
53
37
  return data;
54
38
  }
55
-
56
39
  function _constants() {
57
40
  const data = require("@teambit/legacy/dist/constants");
58
-
59
41
  _constants = function () {
60
42
  return data;
61
43
  };
62
-
63
44
  return data;
64
45
  }
65
-
66
46
  function _cli() {
67
47
  const data = require("@teambit/cli");
68
-
69
48
  _cli = function () {
70
49
  return data;
71
50
  };
72
-
73
51
  return data;
74
52
  }
75
-
76
53
  function _harmony() {
77
54
  const data = require("@teambit/harmony");
78
-
79
55
  _harmony = function () {
80
56
  return data;
81
57
  };
82
-
83
58
  return data;
84
59
  }
85
-
86
60
  function _component() {
87
61
  const data = require("@teambit/component");
88
-
89
62
  _component = function () {
90
63
  return data;
91
64
  };
92
-
93
65
  return data;
94
66
  }
95
-
96
67
  function _logger() {
97
68
  const data = require("@teambit/logger");
98
-
99
69
  _logger = function () {
100
70
  return data;
101
71
  };
102
-
103
72
  return data;
104
73
  }
105
-
106
74
  function _compilationModules() {
107
75
  const data = require("@teambit/compilation.modules.babel-compiler");
108
-
109
76
  _compilationModules = function () {
110
77
  return data;
111
78
  };
112
-
113
79
  return data;
114
80
  }
115
-
116
81
  function _envs() {
117
82
  const data = require("@teambit/envs");
118
-
119
83
  _envs = function () {
120
84
  return data;
121
85
  };
122
-
123
86
  return data;
124
87
  }
125
-
126
88
  function _bit() {
127
89
  const data = require("@teambit/bit");
128
-
129
90
  _bit = function () {
130
91
  return data;
131
92
  };
132
-
133
93
  return data;
134
94
  }
135
-
136
95
  function _scope2() {
137
96
  const data = require("@teambit/scope");
138
-
139
97
  _scope2 = function () {
140
98
  return data;
141
99
  };
142
-
143
100
  return data;
144
101
  }
145
-
146
102
  function _pMapSeries() {
147
103
  const data = _interopRequireDefault(require("p-map-series"));
148
-
149
104
  _pMapSeries = function () {
150
105
  return data;
151
106
  };
152
-
153
107
  return data;
154
108
  }
155
-
156
109
  function _lodash() {
157
110
  const data = require("lodash");
158
-
159
111
  _lodash = function () {
160
112
  return data;
161
113
  };
162
-
163
114
  return data;
164
115
  }
165
-
166
116
  function _aspectDefinition() {
167
117
  const data = require("./aspect-definition");
168
-
169
118
  _aspectDefinition = function () {
170
119
  return data;
171
120
  };
172
-
173
121
  return data;
174
122
  }
175
-
176
123
  function _aspectLoader() {
177
124
  const data = require("./aspect-loader.aspect");
178
-
179
125
  _aspectLoader = function () {
180
126
  return data;
181
127
  };
182
-
183
128
  return data;
184
129
  }
185
-
186
130
  function _constants2() {
187
131
  const data = require("./constants");
188
-
189
132
  _constants2 = function () {
190
133
  return data;
191
134
  };
192
-
193
135
  return data;
194
136
  }
195
-
196
137
  function _exceptions() {
197
138
  const data = require("./exceptions");
198
-
199
139
  _exceptions = function () {
200
140
  return data;
201
141
  };
202
-
203
142
  return data;
204
143
  }
205
-
206
144
  function _coreAspects() {
207
145
  const data = require("./core-aspects");
208
-
209
146
  _coreAspects = function () {
210
147
  return data;
211
148
  };
212
-
213
149
  return data;
214
150
  }
215
-
216
151
  function _plugins() {
217
152
  const data = require("./plugins");
218
-
219
153
  _plugins = function () {
220
154
  return data;
221
155
  };
222
-
223
156
  return data;
224
157
  }
225
-
226
158
  class AspectLoaderMain {
227
159
  constructor(logger, envs, harmony, onAspectLoadErrorSlot, onLoadRequireableExtensionSlot, pluginSlot) {
228
160
  this.logger = logger;
@@ -236,29 +168,23 @@ class AspectLoaderMain {
236
168
  (0, _defineProperty2().default)(this, "_mainAspect", void 0);
237
169
  (0, _defineProperty2().default)(this, "failedLoadAspect", []);
238
170
  }
239
-
240
171
  getCompiler(component) {
241
172
  var _this$envs$getEnv;
242
-
243
173
  const env = (_this$envs$getEnv = this.envs.getEnv(component)) === null || _this$envs$getEnv === void 0 ? void 0 : _this$envs$getEnv.env;
244
174
  return env === null || env === void 0 ? void 0 : env.getCompiler();
245
175
  }
246
-
247
176
  registerOnAspectLoadErrorSlot(onAspectLoadError) {
248
177
  this.onAspectLoadErrorSlot.register(onAspectLoadError);
249
178
  }
250
-
251
179
  registerOnLoadRequireableExtensionSlot(onLoadRequireableExtension) {
252
180
  this.onLoadRequireableExtensionSlot.register(onLoadRequireableExtension);
253
181
  }
182
+
254
183
  /**
255
184
  * returns whether the aspect-load issue has been fixed.
256
185
  */
257
-
258
-
259
186
  async triggerOnAspectLoadError(err, component) {
260
187
  const entries = this.onAspectLoadErrorSlot.toArray(); // e.g. [ [ 'teambit.bit/compiler', [Function: bound onAspectLoadError] ] ]
261
-
262
188
  let isFixed = false;
263
189
  await (0, _pMapSeries().default)(entries, async ([, onAspectFailFunc]) => {
264
190
  const result = await onAspectFailFunc(err, component.id);
@@ -266,44 +192,38 @@ class AspectLoaderMain {
266
192
  });
267
193
  return isFixed;
268
194
  }
269
-
270
195
  async getRuntimePath(component, modulePath, runtime) {
271
196
  const runtimeFile = component.filesystem.files.find(file => {
272
197
  return file.relative.includes(`.${runtime}.runtime`);
273
- }); // @david we should add a compiler api for this.
198
+ });
274
199
 
200
+ // @david we should add a compiler api for this.
275
201
  if (!runtimeFile) return null;
276
-
277
202
  try {
278
203
  const compiler = this.getCompiler(component);
279
-
280
204
  if (!compiler) {
281
205
  return (0, _path().join)(modulePath, runtimeFile.relative);
282
206
  }
283
-
284
207
  const dist = compiler.getDistPathBySrcPath(runtimeFile.relative);
285
208
  return (0, _path().join)(modulePath, dist);
286
209
  } catch (e) {
287
- this.logger.info(`got an error during get runtime path, probably the env is not loaded yet ${e}`); // TODO: we are manually adding the dist here and replace the file name to handle case when
210
+ this.logger.info(`got an error during get runtime path, probably the env is not loaded yet ${e}`);
211
+ // TODO: we are manually adding the dist here and replace the file name to handle case when
288
212
  // we load aspects from scope, and their env in the same iteration, but we get into the aspect before its
289
213
  // env, so it's env doesn't exist yet
290
214
  // we should make sure to first load the env correctly before loading the aspect
291
-
292
215
  const distPath = (0, _path().join)(modulePath, _constants().DEFAULT_DIST_DIRNAME, (0, _compilationModules().replaceFileExtToJs)(runtimeFile.relative));
293
216
  return distPath;
294
217
  }
295
218
  }
296
-
297
219
  isAspectLoaded(id) {
298
220
  if (this.failedAspects.includes(id)) return true;
299
-
300
221
  try {
301
222
  return this.harmony.get(id);
302
223
  } catch (err) {
303
224
  return false;
304
225
  }
305
226
  }
306
-
307
227
  getDescriptor(id) {
308
228
  const instance = this.harmony.get(id);
309
229
  const iconFn = instance.icon;
@@ -313,57 +233,47 @@ class AspectLoaderMain {
313
233
  icon
314
234
  };
315
235
  }
316
-
317
236
  getNotLoadedConfiguredExtensions() {
318
237
  const configuredAspects = Array.from(this.harmony.config.raw.keys());
319
238
  const loadedExtensions = this.harmony.extensionsIds;
320
239
  const extensionsToLoad = (0, _lodash().difference)(configuredAspects, loadedExtensions);
321
240
  return extensionsToLoad;
322
241
  }
323
-
324
242
  loadDefinition(props) {
325
243
  return _aspectDefinition().AspectDefinition.from(props);
326
244
  }
327
-
328
245
  get coreAspects() {
329
246
  return this._coreAspects;
330
247
  }
331
-
332
248
  isCoreAspect(id) {
333
249
  const ids = this.getCoreAspectIds();
334
250
  return ids.includes(id);
335
251
  }
336
-
337
252
  setCoreAspects(aspects) {
338
253
  this._coreAspects = aspects;
339
254
  return this;
340
255
  }
341
-
342
256
  getCoreAspectIds() {
343
257
  const ids = this.coreAspects.map(aspect => aspect.id);
344
258
  return ids.concat(this._reserved);
345
259
  }
260
+
346
261
  /**
347
262
  * Get all the core envs ids which is still register in the bit manifest as core aspect
348
263
  */
349
-
350
-
351
264
  getCoreEnvsIds() {
352
265
  const envsIds = this.envs.getCoreEnvsIds();
353
266
  const allIds = this.getCoreAspectIds();
354
267
  return (0, _lodash().intersection)(allIds, envsIds);
355
268
  }
356
-
357
269
  isCoreEnv(id) {
358
270
  const ids = this.getCoreEnvsIds();
359
271
  return ids.includes(id);
360
272
  }
361
-
362
273
  getUserAspects() {
363
274
  const coreAspectIds = this.getCoreAspectIds();
364
275
  return (0, _lodash().difference)(this.harmony.extensionsIds, coreAspectIds);
365
276
  }
366
-
367
277
  async getCoreAspectDefs(runtimeName) {
368
278
  const defs = await Promise.all(this.coreAspects.map(async aspect => {
369
279
  const id = aspect.id;
@@ -372,39 +282,32 @@ class AspectLoaderMain {
372
282
  }));
373
283
  return defs.filter(def => def.runtimePath);
374
284
  }
375
-
376
285
  async resolveAspects(components, resolver) {
377
286
  const promises = components.map(async component => {
378
287
  const resolvedAspect = await resolver(component);
379
288
  return new (_aspectDefinition().AspectDefinition)(resolvedAspect.aspectPath, resolvedAspect.runtimePath, component);
380
289
  });
381
- const aspectDefs = await Promise.all(promises); // return aspectDefs.filter((def) => def.runtimePath);
382
-
290
+ const aspectDefs = await Promise.all(promises);
291
+ // return aspectDefs.filter((def) => def.runtimePath);
383
292
  return aspectDefs;
384
293
  }
385
-
386
294
  get mainAspect() {
387
295
  return this._mainAspect;
388
296
  }
389
-
390
297
  setMainAspect(mainAspect) {
391
298
  this._mainAspect = mainAspect;
392
299
  return this;
393
300
  }
394
-
395
301
  get failedAspects() {
396
302
  return this.failedLoadAspect;
397
303
  }
398
-
399
304
  resetFailedLoadAspects() {
400
305
  this.failedLoadAspect = [];
401
306
  }
402
-
403
307
  addFailure(id) {
404
308
  if (this.failedAspects.includes(id)) return;
405
309
  this.failedLoadAspect.push(id);
406
310
  }
407
-
408
311
  cloneManifest(manifest) {
409
312
  const cloned = Object.assign(Object.create(Object.getPrototypeOf(manifest)), manifest);
410
313
  cloned.provider = manifest.provider;
@@ -412,21 +315,21 @@ class AspectLoaderMain {
412
315
  cloned.getRuntime = manifest.getRuntime;
413
316
  return cloned;
414
317
  }
318
+
415
319
  /**
416
320
  * run "require" of the component code to get the manifest
417
321
  */
418
-
419
-
420
322
  async doRequire(requireableExtension) {
421
323
  const idStr = requireableExtension.component.id.toString();
422
324
  const aspect = await requireableExtension.require();
423
325
  const manifest = aspect.default || aspect;
424
- manifest.id = idStr; // It's important to clone deep the manifest here to prevent mutate dependencies of other manifests as they point to the same location in memory
425
-
326
+ manifest.id = idStr;
327
+ // It's important to clone deep the manifest here to prevent mutate dependencies of other manifests as they point to the same location in memory
426
328
  const cloned = this.cloneManifest(manifest);
427
329
  const newManifest = await this.runOnLoadRequireableExtensionSubscribers(requireableExtension, cloned);
428
330
  return newManifest;
429
331
  }
332
+
430
333
  /**
431
334
  * in case the extension failed to load, prefer to throw an error, unless `throwOnError` param
432
335
  * passed as `false`.
@@ -447,18 +350,14 @@ class AspectLoaderMain {
447
350
  * @todo: this is not the final word however about throwing/non throwing errors here.
448
351
  * in some cases, such as "bit tag", it's better not to tag if an extension changes the model.
449
352
  */
450
-
451
-
452
353
  async loadRequireableExtensions(requireableExtensions, throwOnError = false) {
453
354
  const manifests = await this.getManifestsFromRequireableExtensions(requireableExtensions, throwOnError);
454
355
  return this.loadExtensionsByManifests(manifests, throwOnError);
455
356
  }
456
-
457
357
  async getManifestsFromRequireableExtensions(requireableExtensions, throwOnError = false) {
458
358
  const manifestsP = (0, _pMapSeries().default)(requireableExtensions, async requireableExtension => {
459
359
  if (!requireableExtensions) return undefined;
460
360
  const idStr = requireableExtension.component.id.toString();
461
-
462
361
  try {
463
362
  return await this.doRequire(requireableExtension);
464
363
  } catch (firstErr) {
@@ -466,10 +365,8 @@ class AspectLoaderMain {
466
365
  this.logger.warn(`failed loading an aspect "${idStr}", will try to fix and reload`, firstErr);
467
366
  const isFixed = await this.triggerOnAspectLoadError(firstErr, requireableExtension.component);
468
367
  let errAfterReLoad;
469
-
470
368
  if (isFixed) {
471
369
  this.logger.info(`the loading issue might be fixed now, re-loading ${idStr}`);
472
-
473
370
  try {
474
371
  return await this.doRequire(requireableExtension);
475
372
  } catch (err) {
@@ -477,38 +374,31 @@ class AspectLoaderMain {
477
374
  errAfterReLoad = err;
478
375
  }
479
376
  }
480
-
481
377
  const error = errAfterReLoad || firstErr;
482
378
  this.handleExtensionLoadingError(error, idStr, throwOnError);
483
379
  }
484
-
485
380
  return undefined;
486
381
  });
487
- const manifests = await manifestsP; // Remove empty manifests as a result of loading issue
382
+ const manifests = await manifestsP;
488
383
 
384
+ // Remove empty manifests as a result of loading issue
489
385
  return (0, _lodash().compact)(manifests);
490
386
  }
491
-
492
387
  handleExtensionLoadingError(error, idStr, throwOnError) {
493
388
  const errorMsg = error.message.split('\n')[0]; // show only the first line if the error is long (e.g. happens with MODULE_NOT_FOUND errors)
494
-
495
389
  const msg = (0, _constants2().UNABLE_TO_LOAD_EXTENSION)(idStr, errorMsg);
496
-
497
390
  if (throwOnError) {
498
391
  // @ts-ignore
499
392
  this.logger.console(error);
500
393
  throw new (_exceptions().CannotLoadExtension)(idStr, error);
501
394
  }
502
-
503
395
  this.logger.error(msg, error);
504
-
505
396
  if (this.logger.isLoaderStarted) {
506
397
  this.logger.consoleFailure(msg);
507
398
  } else {
508
399
  this.logger.console(msg);
509
400
  }
510
401
  }
511
-
512
402
  async runOnLoadRequireableExtensionSubscribers(requireableExtension, manifest) {
513
403
  let updatedManifest = manifest;
514
404
  const entries = this.onLoadRequireableExtensionSlot.toArray();
@@ -517,63 +407,54 @@ class AspectLoaderMain {
517
407
  });
518
408
  return updatedManifest;
519
409
  }
520
-
521
410
  getPluginDefs() {
522
411
  return (0, _lodash().flatten)(this.pluginSlot.values());
523
412
  }
524
-
525
413
  getPlugins(component, componentPath) {
526
414
  const defs = this.getPluginDefs();
527
415
  return _plugins().Plugins.from(component, defs, relativePath => {
528
416
  const compiler = this.getCompiler(component);
529
-
530
417
  if (!compiler) {
531
418
  return (0, _path().join)(componentPath, relativePath);
532
419
  }
533
-
534
420
  const dist = compiler.getDistPathBySrcPath(relativePath);
535
421
  return (0, _path().join)(componentPath, dist);
536
422
  });
537
423
  }
538
-
539
424
  isAspect(manifest) {
540
425
  return !!(manifest.addRuntime && manifest.getRuntime);
541
426
  }
542
-
543
427
  isValidAspect(manifest) {
544
428
  return this.isAspect(manifest) || manifest.provider;
545
429
  }
546
-
547
430
  isAspectComponent(component) {
548
431
  return this.envs.isUsingAspectEnv(component);
549
432
  }
433
+
550
434
  /**
551
435
  * get or create a global scope, import the non-core aspects, load bit from that scope, create
552
436
  * capsules for the aspects and load them from the capsules.
553
437
  */
554
-
555
-
556
438
  async loadAspectsFromGlobalScope(aspectIds) {
557
439
  const globalScope = await _scope().default.ensure(_constants().GLOBAL_SCOPE, 'global-scope');
558
440
  await globalScope.ensureDir();
559
441
  const globalScopeHarmony = await (0, _bit().loadBit)(globalScope.path);
560
442
  const scope = globalScopeHarmony.get(_scope2().ScopeAspect.id);
561
- const aspectLoader = globalScopeHarmony.get(_aspectLoader().AspectLoaderAspect.id); // @todo: Gilad make this work
443
+ const aspectLoader = globalScopeHarmony.get(_aspectLoader().AspectLoaderAspect.id);
444
+ // @todo: Gilad make this work
562
445
  // const ids = await scope.resolveMultipleComponentIds(aspectIds);
563
-
564
446
  const ids = aspectIds.map(id => _component().ComponentID.fromLegacy(_legacyBitId().BitId.parse(id, true)));
565
447
  const hasVersions = ids.every(id => id.hasVersion());
566
448
  const useCache = hasVersions; // if all components has versions, try to use the cached aspects
567
-
568
449
  const components = await scope.import(ids, {
569
450
  useCache,
570
451
  throwIfNotExist: true
571
- }); // don't use `await scope.loadAspectsFromCapsules(components, true);`
452
+ });
453
+
454
+ // don't use `await scope.loadAspectsFromCapsules(components, true);`
572
455
  // it won't work for globalScope because `this !== scope.aspectLoader` (this instance
573
456
  // is not the same as the aspectLoader instance Scope has)
574
-
575
457
  const resolvedAspects = await scope.getResolvedAspects(components);
576
-
577
458
  try {
578
459
  await aspectLoader.loadRequireableExtensions(resolvedAspects, true);
579
460
  } catch (err) {
@@ -586,37 +467,32 @@ class AspectLoaderMain {
586
467
  throw err;
587
468
  }
588
469
  }
589
-
590
470
  return {
591
471
  components,
592
472
  globalScopeHarmony
593
473
  };
594
474
  }
595
-
596
475
  prepareManifests(manifests) {
597
476
  return manifests.map(manifest => {
598
477
  if (this.isAspect(manifest)) return manifest;
599
478
  manifest.runtime = _cli().MainRuntime;
600
479
  if (!manifest.id) throw new Error('manifest must have static id');
601
-
602
480
  const aspect = _harmony().Aspect.create({
603
481
  id: manifest.id
604
482
  });
605
-
606
483
  aspect.addRuntime(manifest);
607
484
  return aspect;
608
485
  });
609
486
  }
487
+
610
488
  /**
611
489
  * register a plugin.
612
490
  */
613
-
614
-
615
491
  registerPlugins(pluginDefs) {
616
492
  this.pluginSlot.register(pluginDefs);
617
- } // TODO: change to use the new logger, see more info at loadExtensions function in the workspace
618
-
493
+ }
619
494
 
495
+ // TODO: change to use the new logger, see more info at loadExtensions function in the workspace
620
496
  async loadExtensionsByManifests(extensionsManifests, throwOnError = true) {
621
497
  try {
622
498
  const manifests = extensionsManifests.filter(manifest => {
@@ -624,44 +500,37 @@ class AspectLoaderMain {
624
500
  if (!isValid) this.logger.warn(`${manifest.id} is invalid. please make sure the extension is valid.`);
625
501
  return isValid;
626
502
  });
627
- const preparedManifests = this.prepareManifests(manifests); // @ts-ignore TODO: fix this
628
-
503
+ const preparedManifests = this.prepareManifests(manifests);
504
+ // @ts-ignore TODO: fix this
629
505
  await this.harmony.load(preparedManifests);
630
506
  } catch (e) {
631
- const ids = extensionsManifests.map(manifest => manifest.id || 'unknown'); // TODO: improve texts
632
-
507
+ const ids = extensionsManifests.map(manifest => manifest.id || 'unknown');
508
+ // TODO: improve texts
633
509
  const errorMsg = e.message.split('\n')[0];
634
510
  const warning = (0, _constants2().UNABLE_TO_LOAD_EXTENSION_FROM_LIST)(ids, errorMsg);
635
511
  this.logger.error(warning, e);
636
-
637
512
  if (this.logger.isLoaderStarted) {
638
513
  this.logger.consoleFailure(warning);
639
514
  } else {
640
515
  this.logger.console(warning);
641
516
  this.logger.console(e);
642
517
  }
643
-
644
518
  if (throwOnError) {
645
519
  throw e;
646
520
  }
647
521
  }
648
522
  }
649
-
650
523
  static async provider([loggerExt, envs], config, [onAspectLoadErrorSlot, onLoadRequireableExtensionSlot, pluginSlot], harmony) {
651
524
  const logger = loggerExt.createLogger(_aspectLoader().AspectLoaderAspect.id);
652
525
  const aspectLoader = new AspectLoaderMain(logger, envs, harmony, onAspectLoadErrorSlot, onLoadRequireableExtensionSlot, pluginSlot);
653
526
  return aspectLoader;
654
527
  }
655
-
656
528
  }
657
-
658
529
  exports.AspectLoaderMain = AspectLoaderMain;
659
530
  (0, _defineProperty2().default)(AspectLoaderMain, "runtime", _cli().MainRuntime);
660
531
  (0, _defineProperty2().default)(AspectLoaderMain, "dependencies", [_logger().LoggerAspect, _envs().EnvsAspect]);
661
532
  (0, _defineProperty2().default)(AspectLoaderMain, "slots", [_harmony().Slot.withType(), _harmony().Slot.withType(), _harmony().Slot.withType()]);
662
-
663
533
  _aspectLoader().AspectLoaderAspect.addRuntime(AspectLoaderMain);
664
-
665
534
  var _default = AspectLoaderMain;
666
535
  exports.default = _default;
667
536