import { Context, Effect, Layer } from "effect"; import { DeployFailed, HealthTimeout, HostingAdapter, HostingOperationFailed, InstanceNotFound, StageTeardown, } from "everything-dev/hosting"; export type StageSubstrate = "docker" | "fly"; export interface StageVariables { readonly substrate: StageSubstrate; readonly region?: string | undefined; } export interface StageDescriptor { readonly stage: string; readonly image: string; readonly identityEnv: Record; } export interface StageSnapshot { readonly stage: string; readonly running: boolean; readonly url?: string | undefined; readonly image?: string | undefined; } export { DeployFailed, HealthTimeout, HostingOperationFailed, InstanceNotFound }; export class StageOrchestrator extends Context.Service< StageOrchestrator, { readonly substrate: StageSubstrate; deploy( descriptor: StageDescriptor, ): Effect.Effect<{ stage: string; url: string }, DeployFailed | HealthTimeout>; current(stage: string): Effect.Effect; stop(stage: string): Effect.Effect; destroy(stage: string): Effect.Effect; } >()("deployments/StageOrchestrator") {} const loadSubstrateLayer = async ( variables: StageVariables, ): Promise> => { if (variables.substrate === "fly") { const mod = await import("everything-dev/hosting/fly"); return mod.flyHostingLayers({ region: variables.region }); } const mod = await import("everything-dev/hosting/docker"); return mod.dockerHostingLayers({ adopt: true }); }; export const StageOrchestratorLive = (variables: StageVariables): Layer.Layer => // oxlint-disable-next-line effecttsgo/missing-layer-context -- everything-dev/hosting resolves as any under tsgo when its dist is unbuilt (CI lints pre-build); the requirement seam is closed by the provides below Layer.effect( StageOrchestrator, Effect.gen(function* () { const layer = yield* Effect.promise(() => loadSubstrateLayer(variables)); const adapter = yield* HostingAdapter.pipe(Effect.provide(layer)); const teardown = yield* StageTeardown.pipe(Effect.provide(layer)); return StageOrchestrator.of({ substrate: variables.substrate, deploy: (descriptor) => adapter.deploy(descriptor), current: (stage) => adapter.current(stage), stop: (stage) => adapter.stop(stage), destroy: (stage) => teardown.destroy(stage), }); }), );