everything-dev
Version:
A consolidated product package for building Module Federation apps with oRPC APIs.
131 lines (129 loc) • 4.71 kB
JavaScript
import { findConfigPath } from "../services/resolution/load.mjs";
import { installBundleFetchFromEnv } from "../services/resolution/resolver.mjs";
import { createPluginRuntime } from "../sdk.mjs";
import { resolveFrameworkPackage } from "../services/framework-version.mjs";
import { secretsRenderers } from "../handlers/secrets.mjs";
import { formatUnknownError } from "../utils/errors.mjs";
import plugin_default from "../plugin.mjs";
import { printBanner } from "../utils/banner.mjs";
import { findCommandDescriptor } from "./catalog.mjs";
import { dbRenderers } from "./commands/db.mjs";
import { runDev, runStart } from "./commands/dev.mjs";
import { runInit } from "./commands/init.mjs";
import { warnIfOutdated } from "./status.mjs";
import { inspectRenderers } from "./commands/inspect.mjs";
import { lifecycleRenderers } from "./commands/lifecycle.mjs";
import { publishRenderers, rollbackPreCall } from "./commands/publish.mjs";
import { scaffoldRenderers } from "./commands/scaffold.mjs";
import { printCommandHelp, printHelp } from "./help.mjs";
import { parseCommandInput } from "./parse.mjs";
import { dirname } from "node:path";
import { Data, Effect, Exit } from "effect";
import process from "node:process";
//#region src/cli/main.ts
var CliError = class extends Data.TaggedError("CliError") {};
const customRunners = {
dev: runDev,
start: runStart,
init: runInit
};
const preCalls = { rollback: rollbackPreCall };
const renderers = {
...dbRenderers,
...inspectRenderers,
...lifecycleRenderers,
...publishRenderers,
...scaffoldRenderers,
...secretsRenderers
};
const cliProgram = Effect.tryPromise({
try: async () => {
const args = process.argv.slice(2);
const helpIndex = args.indexOf("--help") !== -1 ? args.indexOf("--help") : args.indexOf("-h");
if (helpIndex !== -1) {
const helpTarget = findCommandDescriptor(args.slice(0, helpIndex));
if (helpTarget) {
printCommandHelp(helpTarget.descriptor);
return;
}
printHelp();
return;
}
const invocationArgs = args.length > 0 ? args : ["dev"];
const command = invocationArgs[0] ?? "dev";
const commandMatch = findCommandDescriptor(invocationArgs);
if (!commandMatch) {
console.error(`Unknown command: ${command}`);
process.exit(1);
}
const { descriptor, consumed } = commandMatch;
const commandArgs = invocationArgs.slice(consumed);
let parsedInput;
try {
parsedInput = parseCommandInput(descriptor, commandArgs);
} catch (error) {
throw new CliError({ message: formatUnknownError(error) });
}
const bootInput = parsedInput;
const cwdConfigPath = findConfigPath();
const configPath = bootInput.configPath ?? cwdConfigPath;
installBundleFetchFromEnv({ configPath });
const projectDir = cwdConfigPath ? dirname(cwdConfigPath) : void 0;
const edResolved = projectDir ? resolveFrameworkPackage(projectDir, "everything-dev") : void 0;
const displayVersion = edResolved?.installedVersion ? `${edResolved.installedVersion}${edResolved.isLinked ? " (linked)" : ""}` : void 0;
if (!process.env.BOS_NO_BANNER) printBanner("everything-dev", displayVersion);
const pluginRuntime = createPluginRuntime({
registry: { bos: { module: plugin_default } },
secrets: {}
});
const client = (await pluginRuntime.usePlugin.bind(pluginRuntime)("bos", {
variables: {
configPath: configPath ?? void 0,
configDir: cwdConfigPath ? dirname(cwdConfigPath) : void 0
},
secrets: {}
})).createClient();
const outdatedWarning = warnIfOutdated(client, command);
try {
const input = parsedInput;
const ctx = {
client,
input,
descriptor,
projectDir,
configPath,
outdatedWarning
};
await preCalls[descriptor.key]?.(ctx);
const custom = customRunners[descriptor.key];
if (custom) {
await custom(ctx);
return;
}
const result = await client[descriptor.key](input);
if (result?.status === "error" && descriptor.key !== "publish" && descriptor.key !== "deploy") {
console.error(`[CLI] ${formatUnknownError(result.error)}`);
process.exit(1);
}
await renderers[descriptor.key]?.(result, ctx);
} catch (error) {
throw new CliError({ message: formatUnknownError(error) });
}
},
catch: (error) => new CliError({ message: formatUnknownError(error) })
});
const runCli = async () => {
const exit = await Effect.runPromiseExit(cliProgram);
if (Exit.isFailure(exit)) {
const reasons = exit.cause.reasons ?? [];
for (const reason of reasons) {
if (reason?._tag !== "Fail") continue;
const failure = reason.error;
console.error(`[CLI] ${formatUnknownError(failure)}`);
}
process.exit(1);
}
};
//#endregion
export { CliError, runCli };
//# sourceMappingURL=main.mjs.map