Skip to content

Commit

Permalink
Install missing envs upon bit create
Browse files Browse the repository at this point in the history
  • Loading branch information
GiladShoham committed Aug 21, 2023
1 parent 2d4ad20 commit 78ebe3b
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 6 deletions.
14 changes: 14 additions & 0 deletions scopes/generator/generator/component-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { WorkspaceConfigFilesMain } from '@teambit/workspace-config-files';

import { ComponentTemplate, ComponentFile, ComponentConfig } from './component-template';
import { CreateOptions } from './create.cmd';
import { OnComponentCreateSlot } from './generator.main.runtime';

export type GenerateResult = {
id: ComponentID;
Expand All @@ -29,6 +30,8 @@ export type GenerateResult = {
packageName: string;
};

export type OnComponentCreateFn = (generateResults: GenerateResult[]) => Promise<void>;

export class ComponentGenerator {
constructor(
private workspace: Workspace,
Expand All @@ -40,6 +43,7 @@ export class ComponentGenerator {
private tracker: TrackerMain,
private wsConfigFiles: WorkspaceConfigFilesMain,
private logger: Logger,
private onComponentCreateSlot: OnComponentCreateSlot,
private aspectId: string,
private envId?: ComponentID
) {}
Expand Down Expand Up @@ -69,6 +73,10 @@ export class ComponentGenerator {

const ids = generateResults.map((r) => r.id);
await this.tryLinkToNodeModules(ids);
await this.runOnComponentCreateHook(generateResults);
// We are running this after the runOnComponentCreateHook as it require
// the env to be installed to work properly, and the hook might install
// the env.
await this.tryWriteConfigFiles(ids);

return generateResults;
Expand All @@ -87,6 +95,12 @@ export class ComponentGenerator {
}
}

private async runOnComponentCreateHook(generateResults: GenerateResult[]) {
const fns = this.onComponentCreateSlot.values();
if (!fns.length) return;
await Promise.all(fns.map((fn) => fn(generateResults)));
}

/**
* The function `tryWriteConfigFiles` attempts to write workspace config files, and if it fails, it logs an error
* message.
Expand Down
23 changes: 20 additions & 3 deletions scopes/generator/generator/generator.main.runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { GeneratorAspect } from './generator.aspect';
import { CreateCmd, CreateOptions } from './create.cmd';
import { TemplatesCmd } from './templates.cmd';
import { generatorSchema } from './generator.graphql';
import { ComponentGenerator, GenerateResult } from './component-generator';
import { ComponentGenerator, GenerateResult, OnComponentCreateFn } from './component-generator';
import { WorkspaceGenerator } from './workspace-generator';
import { WorkspaceTemplate } from './workspace-template';
import { NewCmd, NewOptions } from './new.cmd';
Expand All @@ -36,6 +36,7 @@ import { GeneratorService } from './generator.service';

export type ComponentTemplateSlot = SlotRegistry<ComponentTemplate[]>;
export type WorkspaceTemplateSlot = SlotRegistry<WorkspaceTemplate[]>;
export type OnComponentCreateSlot = SlotRegistry<OnComponentCreateFn>;

export type TemplateDescriptor = {
aspectId: string;
Expand Down Expand Up @@ -75,6 +76,7 @@ export class GeneratorMain {
constructor(
private componentTemplateSlot: ComponentTemplateSlot,
private workspaceTemplateSlot: WorkspaceTemplateSlot,
private onComponentCreateSlot: OnComponentCreateSlot,
private config: GeneratorConfig,
private workspace: Workspace,
private envs: EnvsMain,
Expand Down Expand Up @@ -103,6 +105,11 @@ export class GeneratorMain {
return this;
}

registerOnComponentCreate(fn: OnComponentCreateFn) {
this.onComponentCreateSlot.register(fn);
return this;
}

/**
* list all component templates registered in the workspace or workspace templates in case the
* workspace is not available
Expand Down Expand Up @@ -307,6 +314,7 @@ export class GeneratorMain {
this.tracker,
this.wsConfigFiles,
this.logger,
this.onComponentCreateSlot,
templateWithId.id,
templateWithId.envName ? ComponentID.fromString(templateWithId.id) : undefined
);
Expand Down Expand Up @@ -468,7 +476,11 @@ export class GeneratorMain {
this.aspectLoaded = true;
}

static slots = [Slot.withType<ComponentTemplate[]>(), Slot.withType<WorkspaceTemplate[]>()];
static slots = [
Slot.withType<ComponentTemplate[]>(),
Slot.withType<WorkspaceTemplate[]>(),
Slot.withType<OnComponentCreateFn>(),
];

static dependencies = [
WorkspaceAspect,
Expand Down Expand Up @@ -516,12 +528,17 @@ export class GeneratorMain {
WorkspaceConfigFilesMain
],
config: GeneratorConfig,
[componentTemplateSlot, workspaceTemplateSlot]: [ComponentTemplateSlot, WorkspaceTemplateSlot]
[componentTemplateSlot, workspaceTemplateSlot, onComponentCreateSlot]: [
ComponentTemplateSlot,
WorkspaceTemplateSlot,
OnComponentCreateSlot
]
) {
const logger = loggerMain.createLogger(GeneratorAspect.id);
const generator = new GeneratorMain(
componentTemplateSlot,
workspaceTemplateSlot,
onComponentCreateSlot,
config,
workspace,
envs,
Expand Down
1 change: 1 addition & 0 deletions scopes/generator/generator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export { GeneratorEnv } from './generator-env-type';
export { GeneratorAspect } from './generator.aspect';
export { TemplateList } from './template-list';
export { StarterList } from './starter-list';
export type { GenerateResult } from './component-generator';
33 changes: 31 additions & 2 deletions scopes/workspace/install/install.main.runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { LinkCommand } from './link';
import InstallCmd from './install.cmd';
import UninstallCmd from './uninstall.cmd';
import UpdateCmd from './update.cmd';
import { GenerateResult, GeneratorAspect, GeneratorMain } from '@teambit/generator';

export type WorkspaceLinkOptions = LinkingOptions & {
rootPolicy?: WorkspacePolicy;
Expand Down Expand Up @@ -175,6 +176,31 @@ export class InstallMain {
this.postInstallSlot.register(fn);
}

async onComponentCreate(generateResults: GenerateResult[]) {
this.workspace.inInstallContext = true;
const ids = generateResults.map((r) => r.id);
const nonLoadedEnvs: string[] = [];
ids.map((id) => this.workspace.clearComponentCache(id));
await pMapSeries(ids, async (id) => {
const component = await this.workspace.get(id);
// const envId = await this.envs.getEnvId(component);
const envId = (await this.envs.calculateEnvId(component)).toString();
const isLoaded = this.envs.isEnvRegistered(envId);
if (!isLoaded) {
nonLoadedEnvs.push(envId);
}
return component;
});
if (!nonLoadedEnvs.length) {
this.workspace.inInstallContext = false;
return;
}
this.logger.consoleWarning(
`the following environments are not installed yet: ${nonLoadedEnvs.join(', ')}. installing them now...`
);
await this.install();
}

private async _addPackages(packages: string[], options?: WorkspaceInstallOptions) {
if ((options?.lifecycleType as string) === 'dev') {
throw new DependencyTypeNotSupportedInPolicy(options?.lifecycleType as string);
Expand Down Expand Up @@ -824,12 +850,13 @@ export class InstallMain {
EnvsAspect,
ApplicationAspect,
IpcEventsAspect,
GeneratorAspect,
];

static runtime = MainRuntime;

static async provider(
[dependencyResolver, workspace, loggerExt, variants, cli, compiler, issues, envs, app, ipcEvents]: [
[dependencyResolver, workspace, loggerExt, variants, cli, compiler, issues, envs, app, ipcEvents, generator]: [
DependencyResolverMain,
Workspace,
LoggerMain,
Expand All @@ -839,7 +866,8 @@ export class InstallMain {
IssuesMain,
EnvsMain,
ApplicationMain,
IpcEventsMain
IpcEventsMain,
GeneratorMain
],
_,
[preLinkSlot, preInstallSlot, postInstallSlot]: [PreLinkSlot, PreInstallSlot, PostInstallSlot]
Expand All @@ -866,6 +894,7 @@ export class InstallMain {
if (issues) {
issues.registerAddComponentsIssues(installExt.addDuplicateComponentAndPackageIssue.bind(installExt));
}
generator.registerOnComponentCreate(installExt.onComponentCreate.bind(installExt));
const commands: CommandList = [
new InstallCmd(installExt, workspace, logger),
new UninstallCmd(installExt),
Expand Down
2 changes: 1 addition & 1 deletion scopes/workspace/workspace/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export class Workspace implements ComponentFactory {
componentLoader: WorkspaceComponentLoader;
bitMap: BitMap;
/**
* Indicate that we are now running installaion process
* Indicate that we are now running installation process
* This is important to know to ignore missing modules across different places
*/
inInstallContext = false;
Expand Down

0 comments on commit 78ebe3b

Please sign in to comment.