UNPKG

everything-dev

Version:

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

211 lines (209 loc) • 7.55 kB
import { fetchRemotePluginManifest } from "../fastkv.mjs"; import { MISSING_CONFIG_MESSAGE } from "../config.mjs"; import { saveBosConfig } from "../utils/save-config.mjs"; import { openResolution } from "../resolution/session.mjs"; import { getPluginRef } from "../build.mjs"; import { generateCodeArtifacts } from "../code-artifacts.mjs"; import { BosDepsTag } from "./shared.mjs"; import { publishToFastKv } from "../publish.mjs"; import { existsSync } from "node:fs"; import { basename, join } from "node:path"; import { Context } from "effect"; //#region src/commands/plugins.ts function registerPlugins(builder) { return { pluginAdd: builder.pluginAdd.handler(async ({ input, context }) => { const session = Context.get(context["effect/context"], BosDepsTag).session; if (!session?.config) return { status: "error", key: "", error: MISSING_CONFIG_MESSAGE }; const isBosRef = input.source.startsWith("bos://"); const isLocal = input.source.startsWith("local:"); const key = sanitizePluginKey(input.as ?? (isBosRef ? input.source.split("/").pop() ?? "plugin" : defaultPluginKey(input.source))); const existing = session.config.plugins?.[key]; const existingEntry = existing && typeof existing === "object" ? existing : {}; const applyEntryDelta = (base) => { if (isBosRef) { const { development: _dropped, ...rest } = base; return { ...rest, extends: input.source }; } if (isLocal) { const { extends: _dropped, ...rest } = base; return { ...rest, development: input.source }; } const { development: _dropped, ...rest } = base; return { ...rest, production: input.production ?? input.source }; }; const nextPlugins = { ...session.config.plugins }; nextPlugins[key] = applyEntryDelta(existingEntry); const nextConfig = { ...session.config, plugins: nextPlugins }; const authored = session.rawConfig; if (authored) { const authoredExisting = authored.plugins?.[key]; const authoredBase = typeof authoredExisting === "string" ? { extends: authoredExisting } : authoredExisting && typeof authoredExisting === "object" ? authoredExisting : {}; await saveBosConfig(session.root, { ...authored, plugins: { ...authored.plugins, [key]: applyEntryDelta(authoredBase) } }); } else await saveBosConfig(session.root, nextConfig); await generateCodeArtifacts(session.root, nextConfig); const stored = nextConfig.plugins?.[key]; const storedObj = stored && typeof stored === "object" ? stored : {}; return { status: "added", key, development: storedObj.development, production: storedObj.production, integrity: storedObj.integrity, version: storedObj.version }; }), pluginRemove: builder.pluginRemove.handler(async ({ input, context }) => { const session = Context.get(context["effect/context"], BosDepsTag).session; if (!session?.config) return { status: "error", key: input.key, error: MISSING_CONFIG_MESSAGE }; if (!session.config.plugins?.[input.key]) return { status: "error", key: input.key, error: `Plugin '${input.key}' is not configured` }; const nextPlugins = { ...session.config.plugins }; delete nextPlugins[input.key]; const nextConfig = { ...session.config, plugins: Object.keys(nextPlugins).length > 0 ? nextPlugins : void 0 }; const authored = session.rawConfig; if (authored) { const authoredPlugins = { ...authored.plugins }; if (authoredPlugins[input.key] === void 0) { if (existsSync(join(session.root, "bos.app.ts"))) return { status: "error", key: input.key, error: `'${input.key}' is inherited from the parent runtime — remove it in the parent config or override it there (the authored descriptor cannot express removal)` }; authoredPlugins[input.key] = null; } else delete authoredPlugins[input.key]; await saveBosConfig(session.root, { ...authored, plugins: Object.keys(authoredPlugins).length > 0 ? authoredPlugins : void 0 }); } else await saveBosConfig(session.root, nextConfig); await generateCodeArtifacts(session.root, nextConfig); return { status: "removed", key: input.key }; }), pluginList: builder.pluginList.handler(async ({ context }) => { return { status: "listed", plugins: listPluginAttachments(Context.get(context["effect/context"], BosDepsTag).session?.config ?? null) }; }), pluginPublish: builder.pluginPublish.handler(async ({ input, context }) => { const session = Context.get(context["effect/context"], BosDepsTag).session; if (!session?.config) return { status: "error", key: input.key, error: MISSING_CONFIG_MESSAGE }; const attachment = session.config?.plugins?.[input.key]; if (!attachment) return { status: "error", key: input.key, error: `Plugin '${input.key}' is not configured` }; const localPath = pluginLocalPath(session.root, attachment); if (!localPath) return { status: "error", key: input.key, error: `Plugin '${input.key}' does not have a local development path` }; const result = await publishToFastKv({ bosConfig: session.config, runtimeConfig: session.runtime, configDir: session.root, env: "production", build: true, dryRun: false, verbose: false, packages: input.key }); if (result.status === "error") return { status: "error", key: input.key, error: result.error }; const nextSession = await openResolution({ cwd: session.root }) ?? session; const production = (nextSession.config?.plugins?.[input.key])?.production; const manifest = production ? await fetchRemotePluginManifest(production) : null; if (production && nextSession.config) await generateCodeArtifacts(session.root, nextSession.config); return { status: "published", key: input.key, path: localPath, production, fingerprint: result.fingerprint, version: manifest?.plugin.version }; }) }; } function sanitizePluginKey(value) { return value.replace(/[^A-Za-z0-9/_-]/g, "-").replace(/\/+/g, "/").split("/").filter(Boolean).map((segment) => segment.replace(/[^A-Za-z0-9_-]/g, "-")).join("/").replace(/^\/+|\/+$/g, ""); } function defaultPluginKey(source) { const normalized = source.replace(/^local:/, "").replace(/\/$/, ""); if (source.startsWith("local:")) return sanitizePluginKey(basename(normalized)) || "plugin"; try { const url = new URL(source); return sanitizePluginKey(basename(url.pathname) || url.hostname) || "plugin"; } catch { return sanitizePluginKey(source) || "plugin"; } } function pluginLocalPath(configDir, attachment) { const ref = getPluginRef(attachment); const source = ref?.development ?? ref?.production; if (!source?.startsWith("local:")) return null; return join(configDir, source.slice(6)); } function listPluginAttachments(config) { return Object.entries(config?.plugins ?? {}).map(([key, attachment]) => { const ref = getPluginRef(attachment); return { key, development: ref?.development, production: ref?.production, localPath: ref?.development?.startsWith("local:") ? ref.development.slice(6) : void 0, source: ref?.development?.startsWith("local:") ? "local" : "remote", integrity: ref?.integrity, version: ref?.version, name: ref?.name }; }).sort((a, b) => a.key.localeCompare(b.key)); } //#endregion export { registerPlugins }; //# sourceMappingURL=plugins.mjs.map