@lunora/cli
Version:
The Lunora CLI: init, dev, deploy, codegen, run, reset, and migrate commands
70 lines (67 loc) • 2.39 kB
JavaScript
import { LunoraError } from '@lunora/errors';
import { d as defineHandler } from '../packem_shared/command-lYnl4QyF.mjs';
import { r as resolveWorkerUrl } from '../packem_shared/resolve-target-qbsJ_5sF.mjs';
const parseArgsJson = (raw) => {
if (raw === void 0 || raw.length === 0) {
return {};
}
try {
return JSON.parse(raw);
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
throw new LunoraError("INTERNAL", `failed to parse --args as JSON: ${message}`, { cause: error });
}
};
const TRAILING_SLASH = /\/$/u;
const runRpcCommand = async (options) => {
const resolvedUrl = resolveWorkerUrl({ cwd: options.cwd ?? process.cwd(), url: options.url });
const baseUrl = (resolvedUrl ?? "http://localhost:8787").replace(TRAILING_SLASH, "");
const requestUrl = `${baseUrl}/_lunora/rpc`;
const fetchImpl = options.fetchImpl ?? globalThis.fetch;
if (typeof fetchImpl !== "function") {
throw new TypeError("no fetch implementation available — pass --fetch via dependency injection or run on Node >= 18");
}
let parsedArgs;
try {
parsedArgs = parseArgsJson(options.args);
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
options.logger.error(message);
return { body: void 0, code: 1, requestUrl };
}
const payload = {
args: parsedArgs,
functionPath: options.functionPath
};
if (options.shard !== void 0) {
payload.shardKey = options.shard;
}
options.logger.info(`POST ${requestUrl} -> ${options.functionPath}`);
const response = await fetchImpl(requestUrl, {
body: JSON.stringify(payload),
headers: { "content-type": "application/json" },
method: "POST"
});
const text = await response.text();
let body;
try {
body = JSON.parse(text);
} catch {
body = text;
}
options.logger.info(JSON.stringify(body, void 0, 2));
return {
body,
code: response.ok ? 0 : 1,
requestUrl
};
};
const execute = defineHandler(({ argument, cwd, logger, options }) => {
const functionPath = argument[0];
if (!functionPath) {
logger.error("missing function path. Usage: lunora run <functionPath> [--args <json>]");
return { code: 1 };
}
return runRpcCommand({ args: options.args, cwd, functionPath, logger, shard: options.shard, url: options.url });
});
export { execute, runRpcCommand };