UNPKG

everything-dev

Version:

A consolidated product package for building Module Federation apps with oRPC APIs.

74 lines (72 loc) • 2.91 kB
import { BosConfigSchema } from "../types.mjs"; import { fetchBosConfigFromFastKv } from "../fastkv.mjs"; import { loadAppDescriptorConfig, localConfigEntryPath, readAuthoredConfigInput, readLocalAuthoredConfigInput, resolveConfigComposableEntries } from "../config.mjs"; import { saveBosConfig } from "../utils/save-config.mjs"; import { walkExtendsChain } from "../resolution/session.mjs"; import { BosDepsTag } from "./shared.mjs"; import { applyRegistrySections } from "../registry-use.mjs"; import { readFileSync } from "node:fs"; import { join } from "node:path"; import { Context } from "effect"; import process from "node:process"; //#region src/commands/config.ts function registerConfig(builder) { return { config: builder.config.handler(async ({ input, context }) => { const deps = Context.get(context["effect/context"], BosDepsTag); if (input.full) return buildConfigResult(deps.session?.config ?? null, true); return buildConfigResult(await readAuthoredConfigInput(deps.session?.root), false); }), registryUse: builder.registryUse.handler(async ({ input, context }) => { const root = Context.get(context["effect/context"], BosDepsTag).session?.root ?? process.cwd(); const configPath = localConfigEntryPath(root) ?? join(root, "bos.config.json"); const normalizedFrom = input.from.startsWith("bos://") ? input.from : `bos://${input.from}`; try { const remote = await fetchBosConfigFromFastKv(normalizedFrom); const local = await readLocalAuthoredConfigInput(root) ?? (configPath.endsWith(".json") ? JSON.parse(readFileSync(configPath, "utf-8")) : await loadAppDescriptorConfig(configPath)); const { config: merged, applied } = applyRegistrySections(local, remote, input.sections); if (input.dryRun) return { status: "dry-run", from: normalizedFrom, applied, configPath }; await saveBosConfig(root, merged); return { status: "updated", from: normalizedFrom, applied, configPath }; } catch (error) { return { status: "error", from: normalizedFrom, applied: [], error: error instanceof Error ? error.message : String(error) }; } }) }; } function buildConfigResult(bosConfig, full = false) { const packages = bosConfig?.app && typeof bosConfig.app === "object" ? Object.keys(bosConfig.app) : []; const remotes = packages.filter((name) => name !== "host"); return { config: bosConfig ?? null, packages, remotes, full }; } async function resolveRemoteConfigChain(accountId, gatewayId, registry) { const { config: merged } = await walkExtendsChain(`bos://${accountId}/${gatewayId}`, { env: "production", registry, visit: async () => {} }); return resolveConfigComposableEntries(BosConfigSchema.parse(merged), process.cwd(), "production"); } //#endregion export { registerConfig, resolveRemoteConfigChain }; //# sourceMappingURL=config.mjs.map