@teambit/envs 1.0.108 → 1.0.109

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,1058 +0,0 @@
1
- import { isCoreAspect } from '@teambit/bit';
2
- import pLocate from 'p-locate';
3
- import { parse } from 'comment-json';
4
- import { SourceFile } from '@teambit/legacy/dist/consumer/component/sources';
5
- import { CLIAspect, CLIMain, MainRuntime } from '@teambit/cli';
6
- import { Component, ComponentAspect, ComponentMain } from '@teambit/component';
7
- import { GraphqlAspect, GraphqlMain } from '@teambit/graphql';
8
- import IssuesAspect, { IssuesMain } from '@teambit/issues';
9
- import pMapSeries from 'p-map-series';
10
- import { IssuesClasses } from '@teambit/component-issues';
11
- import { Harmony, Slot, SlotRegistry } from '@teambit/harmony';
12
- import { Logger, LoggerAspect, LoggerMain } from '@teambit/logger';
13
- import type { AspectDefinition } from '@teambit/aspect-loader';
14
- import { ExtensionDataList, ExtensionDataEntry } from '@teambit/legacy/dist/consumer/config/extension-data';
15
- import { BitError } from '@teambit/bit-error';
16
- import findDuplications from '@teambit/legacy/dist/utils/array/find-duplications';
17
- import { head, uniq } from 'lodash';
18
- import WorkerAspect, { WorkerMain } from '@teambit/worker';
19
- import { ComponentID } from '@teambit/component-id';
20
- import { EnvService } from './services';
21
- import { Environment } from './environment';
22
- import { EnvsAspect } from './environments.aspect';
23
- import { environmentsSchema } from './environments.graphql';
24
- import { EnvRuntime, Runtime } from './runtime';
25
- import { EnvDefinition } from './env-definition';
26
- import { EnvServiceList } from './env-service-list';
27
- import { EnvsCmd, GetEnvCmd, ListEnvsCmd } from './envs.cmd';
28
- import { EnvFragment } from './env.fragment';
29
- import { EnvNotFound, EnvNotConfiguredForComponent } from './exceptions';
30
- import { EnvPlugin } from './env.plugin';
31
-
32
- export type EnvsRegistry = SlotRegistry<Environment>;
33
-
34
- export type EnvsConfig = {
35
- env: string;
36
- options: EnvOptions;
37
- };
38
-
39
- type GetCalcEnvOptions = {
40
- skipWarnings?: boolean;
41
- };
42
-
43
- export type EnvOptions = {};
44
-
45
- export type EnvTransformer = (env: Environment) => Environment;
46
-
47
- export type ServicesRegistry = SlotRegistry<Array<EnvService<any>>>;
48
-
49
- export type RegularCompDescriptor = {
50
- id: string;
51
- icon?: string;
52
- type?: string;
53
- name?: string;
54
- description?: string;
55
- };
56
- export type EnvCompDescriptorProps = RegularCompDescriptor & {
57
- services?: {
58
- env: {
59
- id: string;
60
- icon: string;
61
- name?: string;
62
- description?: string;
63
- };
64
- services: Array<{
65
- id: string;
66
- name?: string;
67
- description?: string;
68
- data: any;
69
- }>;
70
- };
71
- };
72
-
73
- export type EnvCompDescriptor = EnvCompDescriptorProps & {
74
- self?: EnvCompDescriptorProps;
75
- };
76
-
77
- export type Descriptor = RegularCompDescriptor | EnvCompDescriptor;
78
-
79
- export const DEFAULT_ENV = 'teambit.harmony/node';
80
-
81
- export class EnvsMain {
82
- /**
83
- * Envs that are failed to load
84
- */
85
- private failedToLoadEnvs = new Set<string>();
86
- /**
87
- * Extensions that are failed to load
88
- * We use this as sometime when we couldn't load an extension we don't know if it's an env or not
89
- * We should ideally take it from the aspect loader aspect, but right now the aspect loader is using envs
90
- */
91
- private failedToLoadExt = new Set<string>();
92
- /**
93
- * Ids of envs (not neccesrraly loaded successfully)
94
- */
95
- public envIds = new Set<string>();
96
-
97
- static runtime = MainRuntime;
98
-
99
- private alreadyShownWarning = {};
100
-
101
- /**
102
- * icon of the extension.
103
- */
104
- icon() {
105
- return `<svg width="50" height="50" xmlns="http://www.w3.org/2000/svg">
106
- <circle cx="25" cy="25" r="20"/>
107
- </svg>`;
108
- }
109
-
110
- constructor(
111
- /**
112
- * environments extension configuration.
113
- */
114
- readonly config: EnvsConfig,
115
-
116
- /**
117
- * harmony context.
118
- */
119
- private harmony: Harmony,
120
-
121
- /**
122
- * slot for allowing extensions to register new environment.
123
- */
124
- private envSlot: EnvsRegistry,
125
-
126
- private logger: Logger,
127
-
128
- private servicesRegistry: ServicesRegistry,
129
-
130
- private componentMain: ComponentMain,
131
-
132
- private loggerMain: LoggerMain,
133
-
134
- private workerMain: WorkerMain
135
- ) {}
136
-
137
- /**
138
- * creates a new runtime environments for a set of components.
139
- */
140
- async createEnvironment(components: Component[]): Promise<Runtime> {
141
- return this.createRuntime(components);
142
- }
143
-
144
- /**
145
- *
146
- * @param id
147
- */
148
- /**
149
- * This function adds an extension ID to a set of failed to load extensions.
150
- * This mostly used by the aspect loader to add such issues
151
- * Then it is used to hide different errors that are caused by the same issue.
152
- * @param {string} id - string - represents the unique identifier of an extension that failed to load.
153
- */
154
- addFailedToLoadEnvs(id: string) {
155
- this.failedToLoadEnvs.add(id);
156
- this.envIds.add(id);
157
- }
158
-
159
- addFailedToLoadExt(id: string) {
160
- this.failedToLoadExt.add(id);
161
- if (this.envIds.has(id)) {
162
- this.addFailedToLoadEnvs(id);
163
- }
164
- }
165
-
166
- resetFailedToLoadEnvs() {
167
- this.failedToLoadEnvs.clear();
168
- this.failedToLoadExt.clear();
169
- }
170
-
171
- getFailedToLoadEnvs() {
172
- const failedToLoadEnvs = Array.from(this.failedToLoadEnvs);
173
- // Add all extensions which are also envs
174
- for (const extId of this.failedToLoadExt) {
175
- if (this.envIds.has(extId)) {
176
- failedToLoadEnvs.push(extId);
177
- }
178
- }
179
- return uniq(failedToLoadEnvs);
180
- }
181
-
182
- /**
183
- * get the configured default env.
184
- */
185
- getDefaultEnv(): EnvDefinition {
186
- const defaultEnv = this.envSlot.get(DEFAULT_ENV);
187
- if (!defaultEnv) throw new Error('default env must be set.');
188
-
189
- return new EnvDefinition(DEFAULT_ENV, defaultEnv);
190
- }
191
-
192
- getCoreEnvsIds(): string[] {
193
- return [
194
- 'teambit.harmony/aspect',
195
- 'teambit.react/react',
196
- 'teambit.harmony/node',
197
- 'teambit.react/react-native',
198
- 'teambit.html/html',
199
- 'teambit.mdx/mdx',
200
- 'teambit.envs/env',
201
- 'teambit.mdx/readme',
202
- 'teambit.harmony/bit-custom-aspect',
203
- ];
204
- }
205
-
206
- /**
207
- * compose a new environment from a list of environment transformers.
208
- */
209
- compose(targetEnv: Environment, envTransformers: EnvTransformer[]) {
210
- const a = envTransformers.reduce((acc, transformer) => {
211
- acc = transformer(acc);
212
- return acc;
213
- }, targetEnv);
214
-
215
- return a;
216
- }
217
-
218
- /**
219
- * create an env transformer which overrides specific env properties.
220
- */
221
- override(propsToOverride: Environment): EnvTransformer {
222
- return (env: Environment) => {
223
- return this.merge(propsToOverride, env);
224
- };
225
- }
226
-
227
- /**
228
- * compose two environments into one.
229
- */
230
- merge<T extends Environment, S extends Environment>(targetEnv: Environment, sourceEnv: Environment): T & S {
231
- const allNames = new Set<string>();
232
- const keys = ['icon', 'name', 'description'];
233
- for (let o = sourceEnv; o !== Object.prototype; o = Object.getPrototypeOf(o)) {
234
- for (const name of Object.getOwnPropertyNames(o)) {
235
- allNames.add(name);
236
- }
237
- }
238
-
239
- allNames.forEach((key: string) => {
240
- const fn = sourceEnv[key];
241
- if (targetEnv[key]) return;
242
- if (keys.includes(key)) targetEnv[key] = fn;
243
- if (!fn || !fn.bind) {
244
- return;
245
- }
246
- targetEnv[key] = fn.bind(sourceEnv);
247
- });
248
-
249
- return targetEnv as T & S;
250
- }
251
-
252
- /**
253
- * This function checks if an environment manifest file exists in a given component or set of legacy files.
254
- * @param {Component} [envComponent] - A component object that represents an environment. It contains information about
255
- * the files and directories that make up the environment.
256
- * @param {SourceFile[]} [legacyFiles] - An optional array of SourceFile objects representing the files in the legacy
257
- * file system. If this parameter is not provided, the function will attempt to retrieve the files from the envComponent
258
- * parameter.
259
- * @returns a boolean value indicating whether an `env.jsonc` or `env.json` file exists in the `files` array of either
260
- * the `envComponent` object or the `legacyFiles` array. If neither `envComponent` nor `legacyFiles` are provided, the
261
- * function returns `undefined`.
262
- */
263
- hasEnvManifest(envComponent?: Component, legacyFiles?: SourceFile[]): boolean | undefined {
264
- if (!envComponent && !legacyFiles) return undefined;
265
- // @ts-ignore
266
- const files = legacyFiles || envComponent.filesystem.files;
267
- const envJson = files.find((file) => {
268
- return file.relative === 'env.jsonc' || file.relative === 'env.json';
269
- });
270
-
271
- if (!envJson) return false;
272
- return true;
273
- }
274
-
275
- getEnvManifest(envComponent?: Component, legacyFiles?: SourceFile[]): Object | undefined {
276
- // TODO: maybe throw an error here?
277
- if (!envComponent && !legacyFiles) return undefined;
278
- // @ts-ignore
279
- const files = legacyFiles || envComponent.filesystem.files;
280
- const envJson = files.find((file) => {
281
- return file.relative === 'env.jsonc' || file.relative === 'env.json';
282
- });
283
-
284
- if (!envJson) return undefined;
285
-
286
- const object = parse(envJson.contents.toString('utf8'));
287
- return object;
288
- }
289
-
290
- async hasEnvManifestById(envId: string, requesting: string): Promise<boolean | undefined> {
291
- const component = await this.getEnvComponentByEnvId(envId, requesting);
292
- return this.hasEnvManifest(component);
293
- }
294
-
295
- getEnvData(component: Component): Descriptor {
296
- let envsData = component.state.aspects.get(EnvsAspect.id);
297
- if (!envsData) {
298
- // TODO: remove this once we re-export old components used to store the data here
299
- envsData = component.state.aspects.get('teambit.workspace/workspace');
300
- }
301
- if (!envsData) throw new Error(`env was not configured on component ${component.id.toString()}`);
302
- return envsData.data as Descriptor;
303
- }
304
-
305
- /**
306
- * Return the id of the env as configured in the envs data (without version by default)
307
- * The reason it's not contain version by default is that we want to take the version from the aspect defined on the component itself
308
- * As this version is stay up to date during tagging the env along with the component
309
- * @param component
310
- * @param ignoreVersion
311
- */
312
- private getEnvIdFromEnvsData(component: Component, ignoreVersion = true): string | undefined {
313
- const envsData = this.getEnvData(component);
314
- if (!envsData) return undefined;
315
- const rawEnvId = envsData.id;
316
- if (!rawEnvId) return undefined;
317
- if (!ignoreVersion) return rawEnvId;
318
- const envIdWithoutVersion = ComponentID.fromString(rawEnvId).toStringWithoutVersion();
319
- return envIdWithoutVersion;
320
- }
321
-
322
- /**
323
- * get the env id of the given component.
324
- */
325
- getEnvId(component: Component): string {
326
- const envIdFromEnvData = this.getEnvIdFromEnvsData(component);
327
- if (!envIdFromEnvData) {
328
- // This should never happen
329
- throw new Error(`no env found for ${component.id.toString()}`);
330
- }
331
- const withVersion = this.resolveEnv(component, envIdFromEnvData);
332
- const withVersionMatch = this.envSlot.toArray().find(([envId]) => {
333
- return withVersion?.toString() === envId;
334
- });
335
- const withVersionMatchId = withVersionMatch?.[0];
336
- if (withVersionMatchId) return withVersionMatchId;
337
-
338
- // Handle core envs
339
- const exactMatch = this.envSlot.toArray().find(([envId]) => {
340
- return envIdFromEnvData === envId;
341
- });
342
-
343
- const exactMatchId = exactMatch?.[0];
344
- if (exactMatchId) return exactMatchId;
345
-
346
- if (!withVersion) throw new EnvNotConfiguredForComponent(envIdFromEnvData, component.id.toString());
347
- return withVersion.toString();
348
- }
349
-
350
- isUsingCoreEnv(component: Component): boolean {
351
- const envId = this.getEnvId(component);
352
- return this.isCoreEnv(envId);
353
- }
354
-
355
- isCoreEnv(envId: string): boolean {
356
- return this.getCoreEnvsIds().includes(envId);
357
- }
358
-
359
- /**
360
- * get the env of the given component.
361
- * In case you are asking for the env during on load you should use calculateEnv instead
362
- */
363
- getEnv(component: Component): EnvDefinition {
364
- const id = this.getEnvId(component);
365
- const envDef = this.getEnvDefinitionByStringId(id);
366
- if (envDef) {
367
- return envDef;
368
- }
369
- // Do not allow a non existing env
370
- throw new EnvNotFound(id, component.id.toString());
371
- }
372
-
373
- /**
374
- * get the env component of the given component.
375
- */
376
- async getEnvComponent(component: Component): Promise<Component> {
377
- const envId = this.getEnvId(component);
378
- return this.getEnvComponentByEnvId(envId, component.id.toString());
379
- }
380
-
381
- /**
382
- * get the env component by the env id.
383
- */
384
- async getEnvComponentByEnvId(envId: string, requesting?: string): Promise<Component> {
385
- const host = this.componentMain.getHost();
386
- const newId = await host.resolveComponentId(envId);
387
- const envComponent = await host.get(newId);
388
- if (!envComponent) {
389
- throw new BitError(`can't load env. env id is ${envId} used by component ${requesting || 'unknown'}`);
390
- }
391
- return envComponent;
392
- }
393
-
394
- /**
395
- * get the env of the given component.
396
- * This will try to use the regular getEnv but fallback to the calculate env (in case you are using it during on load)
397
- * This is safe to be used on onLoad as well
398
- */
399
- getOrCalculateEnv(component: Component): EnvDefinition {
400
- try {
401
- return this.getEnv(component);
402
- } catch (err) {
403
- return this.calculateEnv(component);
404
- }
405
- }
406
-
407
- /**
408
- * get an environment Descriptor.
409
- */
410
- getDescriptor(component: Component): Descriptor | undefined {
411
- const envsData = this.getEnvData(component);
412
- envsData.id = this.resolveEnv(component, envsData.id)?.toString() || envsData.id;
413
- return envsData;
414
- }
415
-
416
- async calcDescriptor(component: Component, opts: GetCalcEnvOptions = {}): Promise<Descriptor | undefined> {
417
- const componentDescriptor = await this.getComponentEnvDescriptor(component, opts);
418
- if (!componentDescriptor) return undefined;
419
- const envComponentSelfDescriptor = await this.getEnvSelfDescriptor(component);
420
- const result = envComponentSelfDescriptor
421
- ? { ...componentDescriptor, self: envComponentSelfDescriptor }
422
- : componentDescriptor;
423
- return result;
424
- }
425
-
426
- /**
427
- * Get env descriptor from the env itself if the component is an env
428
- * This will be empty for regular component, and will only contain data for env themself
429
- */
430
- private async getEnvSelfDescriptor(envComponent: Component): Promise<EnvCompDescriptorProps | undefined> {
431
- // !important calculate only on the env itself.
432
- if (!this.isEnvRegistered(envComponent.id.toString())) {
433
- return undefined;
434
- }
435
-
436
- const envDef = this.getEnvFromComponent(envComponent);
437
- if (!envDef) return undefined;
438
-
439
- const services = this.getServices(envDef);
440
- // const selfDescriptor = (await this.getEnvDescriptorFromEnvDef(envDef)) || {};
441
- const selfDescriptor = await this.getEnvDescriptorFromEnvDef(envDef);
442
-
443
- if (!selfDescriptor) return undefined;
444
- return {
445
- ...selfDescriptor,
446
- services: services.toObject(),
447
- };
448
- }
449
-
450
- /**
451
- * Get env descriptor from the env that a given component is using
452
- */
453
- private async getComponentEnvDescriptor(
454
- component: Component,
455
- opts: GetCalcEnvOptions = {}
456
- ): Promise<RegularCompDescriptor | undefined> {
457
- const envDef = this.calculateEnv(component, opts);
458
- return this.getEnvDescriptorFromEnvDef(envDef);
459
- }
460
-
461
- private async getEnvDescriptorFromEnvDef(envDef: EnvDefinition): Promise<RegularCompDescriptor | undefined> {
462
- if (!envDef.env.__getDescriptor || typeof envDef.env.__getDescriptor !== 'function') {
463
- return undefined;
464
- }
465
- const systemDescriptor = await envDef.env.__getDescriptor();
466
-
467
- return {
468
- type: systemDescriptor.type,
469
- // Make sure to store the env id in the data without the version
470
- // The version should always come from the aspect id configured on the component
471
- id: envDef.id.split('@')[0],
472
- name: envDef.name,
473
- icon: envDef.env.icon,
474
- description: envDef.description,
475
- };
476
- }
477
-
478
- resolveEnv(component: Component, id: string) {
479
- const matchedEntry = component.state.aspects.entries.find((aspectEntry) => {
480
- return id === aspectEntry.id.toString() || id === aspectEntry.id.toString({ ignoreVersion: true });
481
- });
482
-
483
- return matchedEntry?.id;
484
- }
485
-
486
- /**
487
- * This used to calculate the actual env during the component load.
488
- * Do not use it to get the env (use getEnv instead)
489
- * This should be used only during on load
490
- */
491
- async calculateEnvId(component: Component): Promise<ComponentID> {
492
- // Search first for env configured via envs aspect itself
493
- const envIdFromEnvsConfig = this.getEnvIdFromEnvsConfig(component);
494
- // if (!envIdFromEnvsConfig) return this.getDefaultEnv();
495
- const envIdFromEnvsConfigWithoutVersion = envIdFromEnvsConfig
496
- ? ComponentID.fromString(envIdFromEnvsConfig).toStringWithoutVersion()
497
- : undefined;
498
-
499
- if (envIdFromEnvsConfig && this.isCoreEnv(envIdFromEnvsConfig)) {
500
- return ComponentID.fromString(envIdFromEnvsConfig);
501
- }
502
-
503
- // in some cases we have the id configured in the teambit.envs/envs but without the version
504
- // in such cases we won't find it in the slot
505
- // we search in the component aspect list a matching aspect which is match the id from the teambit.envs/envs
506
- if (envIdFromEnvsConfigWithoutVersion) {
507
- const matchedEntry = component.state.aspects.entries.find((aspectEntry) => {
508
- return (
509
- envIdFromEnvsConfigWithoutVersion === aspectEntry.id.toString() ||
510
- envIdFromEnvsConfigWithoutVersion === aspectEntry.id.toString({ ignoreVersion: true })
511
- );
512
- });
513
-
514
- if (matchedEntry?.id) return matchedEntry?.id;
515
- }
516
-
517
- // in case there is no config in teambit.envs/envs search the aspects for the first env that registered as env
518
- let ids: string[] = [];
519
- component.state.aspects.entries.forEach((aspectEntry) => {
520
- ids.push(aspectEntry.id.toString());
521
- // ids.push(aspectEntry.id.toString({ ignoreVersion: true }));
522
- });
523
- ids = uniq(ids);
524
- const envId = await this.findFirstEnv(ids);
525
- const finalId = envId || this.getDefaultEnv().id;
526
- return ComponentID.fromString(finalId);
527
- }
528
-
529
- /**
530
- * This used to calculate the actual env during the component load.
531
- * Do not use it to get the env (use getEnv instead)
532
- * This should be used only during on load
533
- */
534
- calculateEnv(component: Component, opts: GetCalcEnvOptions = {}): EnvDefinition {
535
- // Search first for env configured via envs aspect itself
536
- const envIdFromEnvsConfig = this.getEnvIdFromEnvsConfig(component);
537
- let envIdFromEnvsConfigWithoutVersion;
538
- if (envIdFromEnvsConfig) {
539
- envIdFromEnvsConfigWithoutVersion = ComponentID.fromString(envIdFromEnvsConfig).toStringWithoutVersion();
540
- const envDef = this.getEnvDefinitionByStringId(envIdFromEnvsConfigWithoutVersion);
541
- if (envDef) {
542
- this.envIds.add(envDef.id);
543
- return envDef;
544
- }
545
- }
546
-
547
- // in some cases we have the id configured in the teambit.envs/envs but without the version
548
- // in such cases we won't find it in the slot
549
- // we search in the component aspect list a matching aspect which is match the id from the teambit.envs/envs
550
- if (envIdFromEnvsConfigWithoutVersion) {
551
- const matchedEntry = component.state.aspects.entries.find((aspectEntry) => {
552
- return (
553
- envIdFromEnvsConfigWithoutVersion === aspectEntry.id.toString() ||
554
- envIdFromEnvsConfigWithoutVersion === aspectEntry.id.toString({ ignoreVersion: true })
555
- );
556
- });
557
- if (matchedEntry) {
558
- // during the tag process, the version in the aspect-entry-id is changed and is not the
559
- // same as it was when it registered to the slot.
560
- const envDef = this.getEnvDefinitionById(matchedEntry.id);
561
- if (envDef) {
562
- this.envIds.add(envDef.id);
563
- return envDef;
564
- }
565
- if (!opts.skipWarnings) {
566
- // Do not allow a non existing env
567
- this.printWarningIfFirstTime(
568
- matchedEntry.id.toString(),
569
- `environment with ID: ${matchedEntry.id.toString()} configured on component ${component.id.toString()} was not loaded (run "bit install")`
570
- );
571
- }
572
- }
573
- // Do not allow configure teambit.envs/envs on the component without configure the env aspect itself
574
- // const errMsg = new EnvNotConfiguredForComponent(envIdFromEnvsConfig as string, component.id.toString()).message;
575
- // this.printWarningIfFirstTime(envIdFromEnvsConfig as string, errMsg);
576
- }
577
-
578
- // in case there is no config in teambit.envs/envs search the aspects for the first env that registered as env
579
- let envDefFromList;
580
- component.state.aspects.entries.find((aspectEntry) => {
581
- const envDef = this.getEnvDefinitionById(aspectEntry.id);
582
- if (envDef) {
583
- envDefFromList = envDef;
584
- }
585
- return !!envDef;
586
- });
587
-
588
- if (envDefFromList) {
589
- this.envIds.add(envDefFromList.id);
590
- return envDefFromList;
591
- }
592
- return this.getDefaultEnv();
593
- }
594
-
595
- /**
596
- * an env can be configured on a component in two ways:
597
- * 1) explicitly inside "teambit.envs/envs". `{ "teambit.envs/envs": { "env": "my-env" } }`
598
- * 2) the env aspect is set on the variant as any other aspect, e.g. `{ "my-env": {} }`
599
- *
600
- * this method returns #1 if exists, otherwise, #2.
601
- */
602
- getAllEnvsConfiguredOnComponent(component: Component): EnvDefinition[] {
603
- // if a component has "envs" config, use it and ignore other components that are set up
604
- // in this components which happen to be envs.
605
- const envDef = this.getEnvFromEnvsConfig(component);
606
- if (envDef) {
607
- return [envDef];
608
- }
609
-
610
- return this.getEnvsNotFromEnvsConfig(component);
611
- }
612
-
613
- /**
614
- * whether a component has an env configured (either by variant or .bitmap).
615
- */
616
- hasEnvConfigured(component: Component): boolean {
617
- return Boolean(this.getAllEnvsConfiguredOnComponent(component).length);
618
- }
619
-
620
- getAllRegisteredEnvs(): string[] {
621
- return this.envSlot.toArray().map((envData) => envData[0]);
622
- }
623
-
624
- getEnvPlugin() {
625
- return new EnvPlugin(this.envSlot, this.servicesRegistry, this.loggerMain, this.workerMain, this.harmony);
626
- }
627
-
628
- /**
629
- * an env can be configured on a component in two ways:
630
- * 1) explicitly inside "teambit.envs/envs". `{ "teambit.envs/envs": { "env": "my-env" } }`
631
- * 2) the env aspect is set on the variant as any other aspect, e.g. `{ "my-env": {} }`
632
- *
633
- * this method returns only #1
634
- */
635
- getEnvFromEnvsConfig(component: Component): EnvDefinition | undefined {
636
- const envIdFromEnvsConfig = this.getEnvIdFromEnvsConfig(component);
637
- if (!envIdFromEnvsConfig) {
638
- return undefined;
639
- }
640
- const envIdFromEnvsConfigWithoutVersion = ComponentID.fromString(envIdFromEnvsConfig).toStringWithoutVersion();
641
- const envDef = this.getEnvDefinitionByStringId(envIdFromEnvsConfigWithoutVersion);
642
- return envDef;
643
- }
644
-
645
- /**
646
- * an env can be configured on a component in two ways:
647
- * 1) explicitly inside "teambit.envs/envs". `{ "teambit.envs/envs": { "env": "my-env" } }`
648
- * 2) the env aspect is set on the variant as any other aspect, e.g. `{ "my-env": {} }`
649
- *
650
- * this method returns only #2
651
- */
652
- getEnvsNotFromEnvsConfig(component: Component): EnvDefinition[] {
653
- return component.state.aspects.entries.reduce((acc: EnvDefinition[], aspectEntry) => {
654
- const envDef = this.getEnvDefinitionById(aspectEntry.id);
655
- if (envDef) acc.push(envDef);
656
- return acc;
657
- }, []);
658
- }
659
-
660
- getEnvIdFromEnvsLegacyExtensions(extensions: ExtensionDataList): string | undefined {
661
- const envsAspect = extensions.findCoreExtension(EnvsAspect.id);
662
- const envIdFromEnvsConfig = envsAspect?.data.id;
663
- return envIdFromEnvsConfig;
664
- }
665
-
666
- /**
667
- * @deprecated DO NOT USE THIS METHOD ANYMORE!!! (PLEASE USE .calculateEnvId() instead!)
668
- */
669
- async calculateEnvIdFromExtensions(extensions: ExtensionDataList): Promise<string> {
670
- // Search first for env configured via envs aspect itself
671
- const envsAspect = extensions.findCoreExtension(EnvsAspect.id);
672
- const envIdFromEnvsConfig = envsAspect?.config.env;
673
-
674
- const envIdFromEnvsConfigWithoutVersion = envIdFromEnvsConfig
675
- ? ComponentID.fromString(envIdFromEnvsConfig).toStringWithoutVersion()
676
- : undefined;
677
-
678
- if (envIdFromEnvsConfig && this.isCoreEnv(envIdFromEnvsConfig)) {
679
- return envIdFromEnvsConfig;
680
- }
681
-
682
- // in some cases we have the id configured in the teambit.envs/envs but without the version
683
- // in such cases we won't find it in the slot
684
- // we search in the component aspect list a matching aspect which is match the id from the teambit.envs/envs
685
- if (envIdFromEnvsConfigWithoutVersion) {
686
- const matchedEntry = extensions.find((extension) => {
687
- if (extension.extensionId) {
688
- return (
689
- envIdFromEnvsConfigWithoutVersion === extension.extensionId.toString() ||
690
- envIdFromEnvsConfigWithoutVersion === extension.extensionId.toString({ ignoreVersion: true })
691
- );
692
- }
693
- return envIdFromEnvsConfigWithoutVersion === extension.stringId;
694
- });
695
- if (matchedEntry?.id) return matchedEntry?.stringId;
696
- }
697
-
698
- // in case there is no config in teambit.envs/envs search the aspects for the first env that registered as env
699
- const ids: string[] = [];
700
- extensions.forEach((extension) => {
701
- if (extension.extensionId) {
702
- ids.push(extension.extensionId.toString());
703
- } else {
704
- ids.push(extension.stringId);
705
- }
706
- });
707
- const envId = await this.findFirstEnv(ids);
708
- const finalId = envId || this.getDefaultEnv().id;
709
- return finalId;
710
- }
711
-
712
- /**
713
- * @deprecated DO NOT USE THIS METHOD ANYMORE!!! (PLEASE USE .calculateEnv() instead!)
714
- */
715
- async calculateEnvFromExtensions(extensions: ExtensionDataList): Promise<EnvDefinition> {
716
- // Search first for env configured via envs aspect itself
717
- const envsAspect = extensions.findCoreExtension(EnvsAspect.id);
718
- const envIdFromEnvsConfig = envsAspect?.config.env;
719
- let envIdFromEnvsConfigWithoutVersion;
720
-
721
- if (envIdFromEnvsConfig) {
722
- envIdFromEnvsConfigWithoutVersion = ComponentID.fromString(envIdFromEnvsConfig).toStringWithoutVersion();
723
- const envDef = this.getEnvDefinitionByStringId(envIdFromEnvsConfigWithoutVersion);
724
- if (envDef) {
725
- this.envIds.add(envDef.id);
726
- return envDef;
727
- }
728
- }
729
-
730
- // in some cases we have the id configured in the teambit.envs/envs but without the version
731
- // in such cases we won't find it in the slot
732
- // we search in the component aspect list a matching aspect which is match the id from the teambit.envs/envs
733
- if (envIdFromEnvsConfigWithoutVersion) {
734
- const matchedEntry = extensions.find((extension) => {
735
- if (extension.extensionId) {
736
- return (
737
- envIdFromEnvsConfigWithoutVersion === extension.extensionId.toString() ||
738
- envIdFromEnvsConfigWithoutVersion === extension.extensionId.toString({ ignoreVersion: true })
739
- );
740
- }
741
- return envIdFromEnvsConfigWithoutVersion === extension.stringId;
742
- });
743
- if (matchedEntry) {
744
- // during the tag process, the version in the aspect-entry-id is changed and is not the
745
- // same as it was when it registered to the slot.
746
- const envDef = this.getEnvDefinitionByLegacyExtension(matchedEntry);
747
- if (envDef) {
748
- this.envIds.add(envDef.id);
749
- return envDef;
750
- }
751
- // Do not allow a non existing env
752
- // this.printWarningIfFirstTime(
753
- // matchedEntry.id.toString(),
754
- // `environment with ID: ${matchedEntry.id.toString()} was not found`
755
- // );
756
- }
757
- // Do not allow configure teambit.envs/envs on the component without configure the env aspect itself
758
- // const errMsg = new EnvNotConfiguredForComponent(envIdFromEnvsConfig).message;
759
- // this.printWarningIfFirstTime(envIdFromEnvsConfig, errMsg);
760
- }
761
-
762
- // in case there is no config in teambit.envs/envs search the aspects for the first env that registered as env
763
- const ids: string[] = [];
764
- extensions.forEach((extension) => {
765
- if (extension.extensionId) {
766
- ids.push(extension.extensionId.toString());
767
- } else {
768
- ids.push(extension.stringId);
769
- }
770
- });
771
- const envId = await this.findFirstEnv(ids);
772
- const envDef = envId ? this.getEnvDefinitionByStringId(envId) : undefined;
773
-
774
- return envDef || this.getDefaultEnv();
775
- }
776
-
777
- /**
778
- * This function finds the first environment ID from a list of IDs by checking if it is register as env (to the slot).
779
- * or contains env.jsonc file
780
- * @param {string[]} ids - `ids` is an array of string values representing environment IDs. The function `findFirstEnv`
781
- * takes this array as input and returns a Promise that resolves to a string value representing the first environment ID
782
- * that matches certain conditions.
783
- * @returns The `findFirstEnv` function returns a Promise that resolves to a string or undefined. The string represents
784
- * the ID of the first environment that matches the conditions specified in the function, or undefined if no environment
785
- * is found.
786
- */
787
- private async findFirstEnv(ids: string[]): Promise<string | undefined> {
788
- let isFoundWithoutVersion = false;
789
- const envId = await pLocate(ids, async (id) => {
790
- const idWithoutVersion = id.split('@')[0];
791
- if (this.isCoreEnv(idWithoutVersion)) return true;
792
- if (isCoreAspect(idWithoutVersion)) return false;
793
- const envDef = this.getEnvDefinitionByStringId(id);
794
- if (envDef) return true;
795
- const envDefWithoutVersion = this.getEnvDefinitionByStringId(idWithoutVersion);
796
- if (envDefWithoutVersion) {
797
- isFoundWithoutVersion = true;
798
- return true;
799
- }
800
- const envComponent = await this.getEnvComponentByEnvId(id);
801
- const hasManifest = this.hasEnvManifest(envComponent);
802
- if (hasManifest) return true;
803
- const isUsingEnvEnv = this.isUsingEnvEnv(envComponent);
804
- return !!isUsingEnvEnv;
805
- });
806
- let finalEnvId;
807
- if (envId) {
808
- finalEnvId = isFoundWithoutVersion ? envId?.split('@')[0] : envId;
809
- this.envIds.add(envId);
810
- }
811
- return finalEnvId;
812
- }
813
-
814
- private getEnvDefinitionByLegacyExtension(extension: ExtensionDataEntry): EnvDefinition | undefined {
815
- const envDef = extension.extensionId
816
- ? this.getEnvDefinitionById(extension.extensionId)
817
- : this.getEnvDefinitionByStringId(extension.stringId);
818
- return envDef;
819
- }
820
-
821
- getEnvIdFromEnvsConfig(component: Component): string | undefined {
822
- const envsAspect = component.state.aspects.get(EnvsAspect.id);
823
- return envsAspect?.config.env;
824
- }
825
-
826
- getEnvDefinition(id: ComponentID) {
827
- const allVersions = this.envSlot.toArray();
828
- const all = allVersions.filter(([envId]) => envId.includes(id.toStringWithoutVersion()));
829
- const first = head(all);
830
- if (!first) return undefined;
831
- const [envId, env] = first;
832
- return new EnvDefinition(envId, env);
833
- }
834
-
835
- getEnvDefinitionById(id: ComponentID): EnvDefinition | undefined {
836
- const envDef =
837
- this.getEnvDefinitionByStringId(id.toString()) ||
838
- this.getEnvDefinitionByStringId(id.toString({ ignoreVersion: true }));
839
- return envDef;
840
- }
841
-
842
- async getEnvDefinitionByLegacyId(id: ComponentID): Promise<EnvDefinition | undefined> {
843
- const host = this.componentMain.getHost();
844
- const newId = await host.resolveComponentId(id);
845
- return this.getEnvDefinitionById(newId);
846
- }
847
-
848
- public getEnvDefinitionByStringId(envId: string): EnvDefinition | undefined {
849
- const env = this.envSlot.get(envId);
850
- if (env) {
851
- return new EnvDefinition(envId, env as Environment);
852
- }
853
- return undefined;
854
- }
855
-
856
- getEnvFromComponent(envComponent: Component): EnvDefinition | undefined {
857
- const env = this.getEnvDefinitionById(envComponent.id);
858
- return env;
859
- }
860
-
861
- /**
862
- * Return the env definition of teambit.envs/env
863
- */
864
- getEnvsEnvDefinition(): EnvDefinition {
865
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
866
- return this.getEnvDefinitionByStringId('teambit.envs/env')!;
867
- }
868
-
869
- private printWarningIfFirstTime(envId: string, message: string) {
870
- if (!this.alreadyShownWarning[envId] && !this.failedToLoadEnvs.has(envId)) {
871
- this.alreadyShownWarning[envId] = true;
872
- this.logger.consoleWarning(message);
873
- this.addFailedToLoadEnvs(envId);
874
- }
875
- }
876
-
877
- /**
878
- * determines whether an env is registered.
879
- */
880
- public isEnvRegistered(id: string) {
881
- return Boolean(this.envSlot.get(id));
882
- }
883
-
884
- isUsingAspectEnv(component: Component): boolean {
885
- const data = this.getEnvData(component);
886
- if (!data) return false;
887
- return data.type === 'aspect';
888
- }
889
-
890
- isUsingEnvEnv(component: Component): boolean {
891
- const data = this.getEnvData(component);
892
- if (!data) return false;
893
- return data.type === 'env';
894
- }
895
-
896
- /**
897
- * Check if the given component is an env component.
898
- * @param component
899
- * @returns
900
- */
901
- isEnv(component: Component): boolean {
902
- return (
903
- this.isUsingEnvEnv(component) ||
904
- this.isEnvRegistered(component.id.toString()) ||
905
- this.isEnvRegistered(component.id.toStringWithoutVersion())
906
- );
907
- }
908
-
909
- /**
910
- * register a new environment service.
911
- */
912
- registerService(...envServices: EnvService<any>[]) {
913
- this.servicesRegistry.register(envServices);
914
- return this;
915
- }
916
-
917
- /**
918
- * get list of services enabled on an env.
919
- */
920
- getServices(env: EnvDefinition): EnvServiceList {
921
- const allServices = this.servicesRegistry.toArray();
922
- const services: [string, EnvService<any>][] = [];
923
- allServices.forEach(([id, currentServices]) => {
924
- currentServices.forEach((service) => {
925
- try {
926
- if (this.implements(env, service)) {
927
- services.push([id, service]);
928
- }
929
- } catch (err) {
930
- this.logger.warn(`failed loading service ${id} for env ${env.id}`);
931
- }
932
- });
933
- });
934
- return new EnvServiceList(env, services);
935
- }
936
-
937
- implements(env: EnvDefinition, service: EnvService<any>) {
938
- // TODO: remove this after refactoring everything and remove getDescriptor from being optional.
939
- if (!service.getDescriptor) return false;
940
- return !!service.getDescriptor(env);
941
- }
942
-
943
- /**
944
- * register an environment.
945
- */
946
- registerEnv(env: Environment) {
947
- return this.envSlot.register(env);
948
- }
949
-
950
- async addNonLoadedEnvAsComponentIssues(components: Component[]) {
951
- await pMapSeries(components, async (component) => {
952
- const envId = await this.calculateEnvId(component);
953
- const envIdStr = envId.toString();
954
- if (!this.isEnvRegistered(envIdStr)) {
955
- this.addFailedToLoadEnvs(envIdStr);
956
- // If there is no version and the env is not in the workspace this is not valid
957
- // you can't set external env without version
958
- if (!envIdStr.includes('@')) {
959
- const foundComp = components.find((c) => c.id.toStringWithoutVersion() === envIdStr);
960
- if (!foundComp) {
961
- component.state.issues.getOrCreate(IssuesClasses.ExternalEnvWithoutVersion).data = {
962
- envId: envIdStr,
963
- componentId: component.id.toString(),
964
- };
965
- } else {
966
- component.state.issues.getOrCreate(IssuesClasses.NonLoadedEnv).data = envIdStr;
967
- }
968
- } else {
969
- component.state.issues.getOrCreate(IssuesClasses.NonLoadedEnv).data = envIdStr;
970
- }
971
- }
972
- });
973
- }
974
-
975
- // refactor here
976
- private async createRuntime(components: Component[]): Promise<Runtime> {
977
- return new Runtime(await this.aggregateByDefs(components), this.logger);
978
- }
979
-
980
- // :TODO can be refactored to few utilities who will make repeating this very easy.
981
- private async aggregateByDefs(components: Component[]): Promise<EnvRuntime[]> {
982
- this.throwForDuplicateComponents(components);
983
- const envsMap = {};
984
- components.forEach((component: Component) => {
985
- const envDef = this.getEnv(component);
986
- const envId = envDef.id;
987
- const env = envDef.env;
988
- // handle config as well when aggregating envs.
989
- if (envsMap[envId]) envsMap[envId].components.push(component);
990
- else
991
- envsMap[envId] = {
992
- components: [component],
993
- env,
994
- };
995
- });
996
-
997
- return Promise.all(
998
- Object.keys(envsMap).map(async (key) => {
999
- const envAspectDef = await this.getEnvAspectDef(key);
1000
- return new EnvRuntime(key, envsMap[key].env, envsMap[key].components, envAspectDef);
1001
- })
1002
- );
1003
- }
1004
-
1005
- private async getEnvAspectDef(envId: string): Promise<AspectDefinition> {
1006
- const host = this.componentMain.getHost();
1007
- const id = await host.resolveComponentId(envId);
1008
- // We don't want to filter by runtime here as we want to also get envs that configured as plugins. so they don't
1009
- // contain the runtime path.
1010
- const resolvedAspects = await host.resolveAspects(MainRuntime.name, [id], {
1011
- requestedOnly: true,
1012
- filterByRuntime: false,
1013
- useScopeAspectsCapsule: true,
1014
- });
1015
- const def = resolvedAspects[0];
1016
-
1017
- return def;
1018
- }
1019
-
1020
- private throwForDuplicateComponents(components: Component[]) {
1021
- const idsStr = components.map((c) => c.id.toString());
1022
- const duplications = findDuplications(idsStr);
1023
- if (duplications.length) {
1024
- throw new Error(`found duplicated components: ${duplications.join(', ')}`);
1025
- }
1026
- }
1027
-
1028
- static slots = [Slot.withType<Environment>(), Slot.withType<EnvService<any>>()];
1029
-
1030
- static dependencies = [GraphqlAspect, LoggerAspect, ComponentAspect, CLIAspect, WorkerAspect, IssuesAspect];
1031
-
1032
- static async provider(
1033
- [graphql, loggerAspect, component, cli, worker, issues]: [
1034
- GraphqlMain,
1035
- LoggerMain,
1036
- ComponentMain,
1037
- CLIMain,
1038
- WorkerMain,
1039
- IssuesMain
1040
- ],
1041
- config: EnvsConfig,
1042
- [envSlot, servicesRegistry]: [EnvsRegistry, ServicesRegistry],
1043
- context: Harmony
1044
- ) {
1045
- const logger = loggerAspect.createLogger(EnvsAspect.id);
1046
- const envs = new EnvsMain(config, context, envSlot, logger, servicesRegistry, component, loggerAspect, worker);
1047
- component.registerShowFragments([new EnvFragment(envs)]);
1048
- if (issues) issues.registerAddComponentsIssues(envs.addNonLoadedEnvAsComponentIssues.bind(envs));
1049
-
1050
- const envsCmd = new EnvsCmd(envs, component);
1051
- envsCmd.commands = [new ListEnvsCmd(envs, component), new GetEnvCmd(envs, component)];
1052
- cli.register(envsCmd);
1053
- graphql.register(environmentsSchema(envs));
1054
- return envs;
1055
- }
1056
- }
1057
-
1058
- EnvsAspect.addRuntime(EnvsMain);