Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(envs) store envs data for env itself + load envs as aspect from scope #7124

Merged
merged 5 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 89 additions & 10 deletions scopes/envs/envs/environments.main.runtime.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CLIAspect, CLIMain, MainRuntime } from '@teambit/cli';
import { Component, ComponentAspect, ComponentMain, ComponentID, AspectData } from '@teambit/component';
import { Component, ComponentAspect, ComponentMain, ComponentID } from '@teambit/component';
import { GraphqlAspect, GraphqlMain } from '@teambit/graphql';
import { Harmony, Slot, SlotRegistry } from '@teambit/harmony';
import { Logger, LoggerAspect, LoggerMain } from '@teambit/logger';
Expand Down Expand Up @@ -35,12 +35,36 @@ export type EnvTransformer = (env: Environment) => Environment;

export type ServicesRegistry = SlotRegistry<Array<EnvService<any>>>;

export type Descriptor = {
export type RegularCompDescriptor = {
id: string;
icon: string;
services?: [];
icon?: string;
type?: string;
name?: string;
description?: string;
};
export type EnvCompDescriptorProps = RegularCompDescriptor & {
services?: {
env: {
id: string;
icon: string;
name?: string;
description?: string;
};
services: Array<{
id: string;
name?: string;
description?: string;
data: any;
}>;
};
};

export type EnvCompDescriptor = EnvCompDescriptorProps & {
self?: EnvCompDescriptorProps;
};

export type Descriptor = RegularCompDescriptor | EnvCompDescriptor;

export const DEFAULT_ENV = 'teambit.harmony/node';

export class EnvsMain {
Expand Down Expand Up @@ -160,14 +184,14 @@ export class EnvsMain {
return targetEnv as T & S;
}

getEnvData(component: Component): AspectData {
getEnvData(component: Component): Descriptor {
let envsData = component.state.aspects.get(EnvsAspect.id);
if (!envsData) {
// TODO: remove this once we re-export old components used to store the data here
envsData = component.state.aspects.get('teambit.workspace/workspace');
}
if (!envsData) throw new Error(`env was not configured on component ${component.id.toString()}`);
return envsData.data;
return envsData.data as Descriptor;
}

/**
Expand Down Expand Up @@ -277,10 +301,65 @@ export class EnvsMain {
*/
getDescriptor(component: Component): Descriptor | null {
const envsData = this.getEnvData(component);
return envsData;
}

async calcDescriptor(component: Component): Promise<Descriptor | undefined> {
const componentDescriptor = await this.getComponentEnvDescriptor(component);
if (!componentDescriptor) return undefined;
const envComponentSelfDescriptor = await this.getEnvSelfDescriptor(component);
const result = envComponentSelfDescriptor
? { ...componentDescriptor, self: envComponentSelfDescriptor }
: componentDescriptor;
return result;
}

/**
* Get env descriptor from the env itself if the component is an env
* This will be empty for regular component, and will only contain data for env themself
*/
private async getEnvSelfDescriptor(envComponent: Component): Promise<EnvCompDescriptorProps | undefined> {
// !important calculate only on the env itself.
if (!this.isEnvRegistered(envComponent.id.toString())) {
return undefined;
}

const envDef = this.getEnvFromComponent(envComponent);
if (!envDef) return undefined;

const services = this.getServices(envDef);
// const selfDescriptor = (await this.getEnvDescriptorFromEnvDef(envDef)) || {};
const selfDescriptor = await this.getEnvDescriptorFromEnvDef(envDef);

if (!selfDescriptor) return undefined;
return {
...selfDescriptor,
services: services.toObject(),
};
}

/**
* Get env descriptor from the env that a given component is using
*/
private async getComponentEnvDescriptor(component: Component): Promise<RegularCompDescriptor | undefined> {
const envDef = this.calculateEnv(component);
return this.getEnvDescriptorFromEnvDef(envDef);
}

private async getEnvDescriptorFromEnvDef(envDef: EnvDefinition): Promise<RegularCompDescriptor | undefined> {
if (!envDef.env.__getDescriptor || typeof envDef.env.__getDescriptor !== 'function') {
return undefined;
}
const systemDescriptor = await envDef.env.__getDescriptor();

return {
id: this.getEnvId(component),
icon: envsData.icon,
services: envsData.services,
type: systemDescriptor.type,
// Make sure to store the env id in the data without the version
// The version should always come from the aspect id configured on the component
id: envDef.id.split('@')[0],
name: envDef.name,
icon: envDef.env.icon,
description: envDef.description,
};
}

Expand Down Expand Up @@ -651,7 +730,7 @@ export class EnvsMain {
const resolvedAspects = await host.resolveAspects(MainRuntime.name, [id], {
requestedOnly: true,
filterByRuntime: false,
useScopeAspectsCapsule: true
useScopeAspectsCapsule: true,
});
const def = resolvedAspects[0];

Expand Down
2 changes: 1 addition & 1 deletion scopes/preview/preview/preview.main.runtime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ export class PreviewMain {
ENV_STRATEGY_ARTIFACT_NAME
);
const envType = this.envs.getEnvData(component).type;
return !!artifacts && !!artifacts.length && ENV_WITH_LEGACY_DOCS.includes(envType);
return !!artifacts && !!artifacts.length && ENV_WITH_LEGACY_DOCS.includes(envType || '');
}

/**
Expand Down
4 changes: 4 additions & 0 deletions scopes/scope/scope/scope.main.runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,10 @@ export class ScopeMain implements ComponentFactory {
if (appData?.data?.appName) {
aspectIds.push(component.id.toString());
}
const envsData = component.state.aspects.get(EnvsAspect.id);
if (envsData?.data?.services || envsData?.data?.self){
aspectIds.push(component.id.toString());
}
await this.loadAspects(aspectIds, true, id.toString(), lane);

return component;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ export class WorkspaceComponentLoader {

// Special load events which runs from the workspace but should run from the correct aspect
// TODO: remove this once those extensions dependent on workspace
const envsData = await this.workspace.getEnvSystemDescriptor(component);
const envsData = await this.envs.calcDescriptor(component);

// Move to deps resolver main runtime once we switch ws<> deps resolver direction
const policy = await this.dependencyResolver.mergeVariantPolicies(
Expand Down
28 changes: 1 addition & 27 deletions scopes/workspace/workspace/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ import {
ComponentFactory,
ComponentID,
AspectList,
AspectData,
InvalidComponent,
ResolveAspectsOptions,
} from '@teambit/component';
import { BitError } from '@teambit/bit-error';
import { REMOVE_EXTENSION_SPECIAL_SIGN } from '@teambit/legacy/dist/consumer/config';
import { ComponentScopeDirMap, ConfigMain } from '@teambit/config';
import { DependencyResolverMain } from '@teambit/dependency-resolver';
import { EnvsMain, EnvsAspect, EnvServiceList } from '@teambit/envs';
import { EnvsMain, EnvsAspect } from '@teambit/envs';
import { GraphqlMain } from '@teambit/graphql';
import { Harmony } from '@teambit/harmony';
import { Logger } from '@teambit/logger';
Expand Down Expand Up @@ -581,31 +580,6 @@ export class Workspace implements ComponentFactory {
return workspaceAspectsLoader.getConfiguredUserAspectsPackages(options);
}

// TODO: @gilad we should refactor this asap into to the envs aspect.
async getEnvSystemDescriptor(component: Component): Promise<AspectData> {
const env = this.envs.calculateEnv(component);
if (env.env.__getDescriptor && typeof env.env.__getDescriptor === 'function') {
const systemDescriptor = await env.env.__getDescriptor();
// !important persist services only on the env itself.
let services: undefined | EnvServiceList;
if (this.envs.isEnvRegistered(component.id.toString())) services = this.envs.getServices(env);
const icon = this.aspectLoader.getDescriptor(env.id)?.icon || env.env.icon;

return {
type: systemDescriptor.type,
// Make sure to store the env id in the data without the version
// The version should always come from the aspect id configured on the component
id: env.id.split('@')[0],
name: env.name,
icon,
description: env.description,
services: services?.toObject(),
};
}

return {};
}

clearCache() {
this.aspectLoader.resetFailedLoadAspects();
this.logger.debug('clearing the workspace and scope caches');
Expand Down