everything-dev
Version:
A consolidated product package for building Module Federation apps with oRPC APIs.
150 lines (148 loc) • 6.83 kB
JavaScript
const require_runtime = require("../_virtual/_rolldown/runtime.cjs");
const require_load = require("../services/resolution/load.cjs");
const require_services_resolution_session = require("../services/resolution/session.cjs");
const require_artifacts = require("../services/codegen/artifacts.cjs");
let node_fs = require("node:fs");
let node_path = require("node:path");
let effect = require("effect");
let node_process = require("node:process");
node_process = require_runtime.__toESM(node_process, 1);
//#region src/handlers/types-gen.ts
const nodeEnvProduction = () => node_process.default.env.NODE_ENV === "production";
function registerTypesGen(builder) {
return { typesGen: builder.typesGen.effect(function* (_opts, input) {
return yield* effect.Effect.tryPromise({
try: async () => {
const configPath = require_load.findConfigPath();
if (!configPath) return {
status: "error",
generated: [],
fetched: [],
skipped: [],
failed: [],
error: `${require_load.MISSING_CONFIG_MESSAGE} in current directory`
};
const projectDir = (0, node_path.resolve)((0, node_path.dirname)(configPath));
const env = input.env ?? (nodeEnvProduction() ? "production" : "development");
const refreshed = await require_services_resolution_session.openResolution({
cwd: projectDir,
env,
remotePlugins: input.remotePlugins
});
if (!refreshed?.config || !refreshed.runtime) return {
status: "error",
generated: [],
fetched: [],
skipped: [],
failed: [],
error: "Failed to load the authored config"
};
const runtime = refreshed.runtime;
const resolvedConfig = refreshed.config;
if (input.dryRun) {
const pluginEntries = Object.entries(runtime.plugins ?? {});
const fetched = [];
const skipped = [];
const hasLocalApiWorkspace = (0, node_fs.existsSync)((0, node_path.join)(projectDir, "api", "src"));
if (runtime.api.source !== "local") fetched.push(`api remote (${runtime.api.url})`);
else {
const path = runtime.api.localPath ? ` (${(0, node_path.relative)(projectDir, runtime.api.localPath)})` : "";
skipped.push(`api local${path}`);
}
if (runtime.auth) {
if (runtime.auth.source !== "local") fetched.push(`auth remote (${runtime.auth.url})`);
else {
const path = runtime.auth.localPath ? ` (${(0, node_path.relative)(projectDir, runtime.auth.localPath)})` : "";
skipped.push(`auth local${path}`);
}
}
for (const [key, plugin] of pluginEntries) if (plugin.url && plugin.source !== "local") fetched.push(`${key} remote (${plugin.url})`);
else if (plugin.localPath) skipped.push(`${key} local (${(0, node_path.relative)(projectDir, plugin.localPath)})`);
else skipped.push(`${key} no URL resolved`);
const generated = [
"ui/src/lib/api-types.gen.ts",
"ui/src/lib/auth-types.gen.ts",
".bos/bos.resolved-config.json"
];
if (hasLocalApiWorkspace) generated.push("api/src/lib/plugins-types.gen.ts", "api/src/lib/auth-types.gen.ts");
if ((0, node_fs.existsSync)((0, node_path.join)(projectDir, "host", "src"))) generated.push("host/src/lib/auth-types.gen.ts");
for (const [_key, plugin] of pluginEntries) {
const localPath = plugin.localPath;
if (!localPath) continue;
const pluginSrc = (0, node_path.join)(localPath, "src", "lib", "plugins-client.gen.ts");
if ((0, node_fs.existsSync)(pluginSrc)) generated.push((0, node_path.relative)(projectDir, pluginSrc));
}
if (runtime.auth?.localPath) {
const authSrc = (0, node_path.join)(runtime.auth.localPath, "src", "lib", "plugins-client.gen.ts");
if ((0, node_fs.existsSync)(authSrc)) generated.push((0, node_path.relative)(projectDir, authSrc));
}
return {
status: "success",
generated,
fetched,
skipped,
failed: []
};
}
const artifacts = await require_artifacts.generateCodeArtifacts(projectDir, resolvedConfig, {
env,
extendsChain: [...refreshed.chain],
runtimeConfig: runtime,
forceRouteTree: true,
runtimeMapStrict: true
});
const hasLocalApiWorkspace = (0, node_fs.existsSync)((0, node_path.join)(projectDir, "api", "src"));
const generated = ["ui/src/lib/api-types.gen.ts"];
if (hasLocalApiWorkspace) generated.push("api/src/lib/plugins-types.gen.ts", "api/src/lib/auth-types.gen.ts");
if (runtime.auth && (runtime.auth.source !== "local" || runtime.auth.localPath)) generated.push("ui/src/lib/auth-types.gen.ts");
if ((0, node_fs.existsSync)((0, node_path.join)(projectDir, "host", "src"))) generated.push("host/src/lib/auth-types.gen.ts");
for (const [_key, plugin] of Object.entries(runtime.plugins ?? {})) {
const localPath = plugin.localPath;
if (!localPath) continue;
const pluginSrc = (0, node_path.join)(localPath, "src", "lib", "plugins-client.gen.ts");
if ((0, node_fs.existsSync)(pluginSrc)) generated.push((0, node_path.relative)(projectDir, pluginSrc));
}
if (runtime.auth?.localPath) {
const authSrc = (0, node_path.join)(runtime.auth.localPath, "src", "lib", "plugins-client.gen.ts");
if ((0, node_fs.existsSync)(authSrc)) generated.push((0, node_path.relative)(projectDir, authSrc));
}
const contractStatus = artifacts?.contractStatus ?? [];
const fetched = [];
const skipped = [];
const failed = [];
for (const entry of contractStatus) if (entry.source === "remote") fetched.push(entry.url ? `${entry.key} remote (${entry.url})` : entry.key);
else if (entry.source === "local") {
const path = entry.localPath ? ` (${(0, node_path.relative)(projectDir, entry.localPath)})` : "";
skipped.push(`${entry.key} local${path}`);
} else if (entry.source === "skipped") skipped.push(`${entry.key} no URL resolved`);
else if (entry.source === "failed") {
const detail = entry.error ? `: ${entry.error}` : "";
failed.push(`${entry.key}${detail}`);
}
return {
status: "success",
generated: [
...generated,
".bos/bos.resolved-config.json",
...(0, node_fs.existsSync)((0, node_path.join)(projectDir, ".bos/generated/runtime-map.json")) ? [".bos/generated/runtime-map.json"] : [],
...runtime.ui?.source === "local" && runtime.ui.localPath && (0, node_fs.existsSync)((0, node_path.join)(runtime.ui.localPath, "src/lib/runtime-map.gen.ts")) ? ["ui/src/lib/runtime-map.gen.ts"] : []
],
fetched,
skipped,
failed
};
},
catch: (error) => ({
status: "error",
generated: [],
fetched: [],
skipped: [],
failed: [],
error: error instanceof Error ? error.message : "Unknown error"
})
});
}) };
}
//#endregion
exports.registerTypesGen = registerTypesGen;
//# sourceMappingURL=types-gen.cjs.map