stack/packages/stack-cli/src/commands/exec.ts
Bilal Godil d4f6f58735 feat(hexclave): PR 2 — visible rebrand to Hexclave
Rebased onto dev after PR 1475 (cl/hexclave-pr1) was squash-merged.
Squashes the original 46-commit branch (including PR1-duplicate commits
that arrived via cherry-picks/merges) into a single commit containing
only PR2's net delta over dev.

Original PR 1481 head: 94872de407873a1cabd4085deb21b69afe8d7699
(kept locally at backup/cl-romantic-mendel-5a2c25-pre-rebase)
2026-05-23 17:35:08 -07:00

97 lines
4.0 KiB
TypeScript

import { Command } from "commander";
import { isProjectAuthWithRefreshToken, resolveAuth, resolveLocalEmulatorAuth, type ProjectAuthWithRefreshToken } from "../lib/auth.js";
import { lookupLocalEmulatorProjectIdByPath } from "../lib/local-emulator-client.js";
import { getAdminProject } from "../lib/app.js";
import { CliError } from "../lib/errors.js";
import { resolveConfigFilePathOption } from "../lib/config-file-path.js";
function getErrorMessage(err: unknown): string {
if (err instanceof Error) {
return err.message;
}
if (typeof err === "string") {
return err;
}
try {
return JSON.stringify(err);
} catch {
return String(err);
}
}
export type ExecTargetOpts = {
cloudProjectId?: string,
configFile?: string,
};
export type ExecTarget =
| { kind: "cloud", projectId: string }
| { kind: "config", configFile: string };
// Validate that exactly one of --cloud-project-id / --config-file was provided
// and return a tagged target. Both branches are mutually exclusive; passing
// neither (or both) is rejected so the user has to make the cloud-vs-local
// choice explicit at every invocation.
export function parseExecTarget(opts: ExecTargetOpts): ExecTarget {
const hasCloud = opts.cloudProjectId != null && opts.cloudProjectId !== "";
const hasConfig = opts.configFile != null && opts.configFile !== "";
if (hasCloud && hasConfig) {
throw new CliError("Pass either --cloud-project-id or --config-file, not both.");
}
if (!hasCloud && !hasConfig) {
throw new CliError("Specify a target: pass --cloud-project-id <id> for the Hexclave cloud API, or --config-file <path> for the local emulator.");
}
if (hasCloud) {
return { kind: "cloud", projectId: opts.cloudProjectId as string };
}
return { kind: "config", configFile: opts.configFile as string };
}
export function registerExecCommand(program: Command) {
program
.command("exec [javascript]")
.description("Execute JavaScript with a pre-configured StackServerApp as `stackServerApp`. Pass --cloud-project-id <id> for the cloud API, or --config-file <path> for the local emulator.")
.option("--cloud-project-id <id>", "Cloud project ID to run against (use --config-file instead for the local emulator)")
.option("--config-file <path>", "Path to a local emulator stack.config.ts (use --cloud-project-id instead for the cloud API)")
.addHelpText("after", "\nFor available API methods, see: https://docs.hexclave.com/docs/sdk")
.action(async (javascript: string | undefined, opts: ExecTargetOpts) => {
if (javascript === undefined) {
throw new CliError("Missing JavaScript argument. Use `stack exec \"<javascript>\"` or `stack exec --help`.");
}
const target = parseExecTarget(opts);
let auth: ProjectAuthWithRefreshToken;
if (target.kind === "cloud") {
const cloudAuth = resolveAuth(target.projectId);
if (!isProjectAuthWithRefreshToken(cloudAuth)) {
throw new CliError("`stack exec --cloud-project-id` requires `stack login`. Remove STACK_SECRET_SERVER_KEY and try again.");
}
auth = cloudAuth;
} else {
const absPath = resolveConfigFilePathOption(target.configFile, { mustExist: true });
const projectId = await lookupLocalEmulatorProjectIdByPath(absPath);
auth = await resolveLocalEmulatorAuth(projectId);
}
const project = await getAdminProject(auth);
// eslint-disable-next-line @typescript-eslint/no-implied-eval
const AsyncFunction = Object.getPrototypeOf(async function(){}).constructor;
let fn;
try {
fn = new AsyncFunction("stackServerApp", javascript);
} catch (err: unknown) {
throw new CliError(`Syntax error in exec code: ${getErrorMessage(err)}`);
}
let result;
try {
result = await fn(project.app);
} catch (err: unknown) {
throw new CliError(`Exec error: ${getErrorMessage(err)}`);
}
if (result !== undefined) {
console.log(JSON.stringify(result, null, 2));
}
});
}