Skip to content

Commit 42ea4b9

Browse files
koki-developclaude
andcommitted
feat: Add --eval option to run command for inline code execution
Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 005d4b8 commit 42ea4b9

File tree

1 file changed

+51
-4
lines changed

1 file changed

+51
-4
lines changed

src/commands/run.ts

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ const EXTENSION_TO_LANGUAGE: Record<string, string> = {
1313
".rs": "rust",
1414
};
1515

16+
const LANGUAGE_TO_EXTENSION: Record<string, string> = {
17+
typescript: ".ts",
18+
javascript: ".js",
19+
python: ".py",
20+
ruby: ".rb",
21+
go: ".go",
22+
rust: ".rs",
23+
};
24+
1625
function detectLanguage(filePath: string): string | null {
1726
const ext = extname(filePath).toLowerCase();
1827
return EXTENSION_TO_LANGUAGE[ext] ?? null;
@@ -49,7 +58,7 @@ export function registerRunCommand(program: Command): void {
4958
program
5059
.command("run")
5160
.description("Execute source files in the Codize Sandbox")
52-
.argument("<files...>", "Source files to execute")
61+
.argument("[files...]", "Source files to execute")
5362
.option(
5463
"-l, --language <language>",
5564
"Language override (auto-detected from extension by default)",
@@ -59,20 +68,58 @@ export function registerRunCommand(program: Command): void {
5968
"Codize API key (defaults to CODIZE_API_KEY env var)",
6069
)
6170
.option("--json", "Output result as JSON instead of plain text")
71+
.option(
72+
"-e, --eval <code>",
73+
"Inline code to execute (requires --language, can be specified multiple times)",
74+
(val: string, prev: string[]) => [...prev, val],
75+
[] as string[],
76+
)
6277
.action(
6378
async (
6479
files: string[],
65-
options: { language?: string; apiKey?: string; json?: boolean },
80+
options: {
81+
language?: string;
82+
apiKey?: string;
83+
json?: boolean;
84+
eval: string[];
85+
},
6686
) => {
87+
if (options.eval.length > 0 && files.length > 0) {
88+
throw new CliError("Cannot use --eval together with file arguments.");
89+
}
90+
91+
if (options.eval.length === 0 && files.length === 0) {
92+
throw new CliError(
93+
"No input provided. Specify source files or use --eval.",
94+
);
95+
}
96+
6797
const apiKey = options.apiKey ?? process.env["CODIZE_API_KEY"];
6898
if (!apiKey) {
6999
throw new CliError(
70100
"API key is required. Set CODIZE_API_KEY or use --api-key.",
71101
);
72102
}
73103

74-
const language = options.language ?? resolveLanguage(files);
75-
const filePayloads = readFiles(files);
104+
let language: string;
105+
let filePayloads: { name: string; content: string }[];
106+
107+
if (options.eval.length > 0) {
108+
if (!options.language) {
109+
throw new CliError(
110+
"Option --language is required when using --eval.",
111+
);
112+
}
113+
language = options.language;
114+
const ext = LANGUAGE_TO_EXTENSION[language] ?? "";
115+
filePayloads = options.eval.map((code, i) => ({
116+
name: `file${i}${ext}`,
117+
content: code,
118+
}));
119+
} else {
120+
language = options.language ?? resolveLanguage(files);
121+
filePayloads = readFiles(files);
122+
}
76123

77124
const client = new CodizeClient({ apiKey });
78125
let result: Awaited<ReturnType<typeof client.sandbox.execute>>;

0 commit comments

Comments
 (0)