UNPKG

@microfox/cli

Version:

Universal CLI tool for creating modern TypeScript packages with npm availability checking

217 lines (211 loc) 6.88 kB
#!/usr/bin/env node import { kickstartCommand } from "./chunk-26ORG7CZ.mjs"; import { pushCommand } from "./chunk-ZK4UVP4V.mjs"; import { logsCommand, metricsCommand, statusCommand } from "./chunk-MSVUZOBM.mjs"; import "./chunk-KPJJOO76.mjs"; // src/cli.ts import { Command } from "commander"; import chalk2 from "chalk"; // src/commands/code.ts import chalk from "chalk"; import axios from "axios"; import { spawn } from "child_process"; // src/utils/findProjectRoot.ts import path from "path"; import fs from "fs"; async function findProjectRoot(startPath = process.cwd()) { let currentPath = startPath; let count = 0; while (true) { const microfoxRootPath = path.join(currentPath, "microfox-root"); if (fs.existsSync(microfoxRootPath)) { return currentPath; } const parentPath = path.dirname(currentPath); if (parentPath === currentPath || count > 10) { return null; } currentPath = parentPath; } } // src/commands/code.ts import path2 from "path"; import readline from "readline"; var NEXTJS_PORT = 3e3; var API_URL = `http://localhost:${NEXTJS_PORT}/api/agent`; var createLogger = (rl) => { return (source, message, color) => { readline.cursorTo(process.stdout, 0); readline.clearLine(process.stdout, 0); const prefix = color(`[${source}]`); const lines = message.trim().split("\n"); for (const line of lines) { console.log(`${prefix} ${line}`); } rl.prompt(true); }; }; async function codeCommand() { var _a, _b; let childProcess = null; const killProcess = () => { if (childProcess && childProcess.pid) { console.log(chalk.yellow("\nGracefully shutting down...")); if (process.platform === "win32") { spawn("taskkill", ["/pid", childProcess.pid.toString(), "/f", "/t"]); } else { childProcess.kill("SIGINT"); } childProcess = null; } }; process.on("SIGINT", () => { killProcess(); process.exit(0); }); process.on("exit", killProcess); try { const projectRoot = await findProjectRoot(); if (!projectRoot) { console.error( chalk.red("Error: Could not find project root. Make sure you are inside a Microfox project.") ); process.exit(1); } const codeAppPath = path2.join(projectRoot, "apps", "code"); console.log(chalk.cyan(`Starting Next.js server in ${codeAppPath}...`)); childProcess = spawn("npm", ["run", "dev"], { cwd: codeAppPath, shell: true, env: { ...process.env, FORCE_COLOR: "true" } }); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, prompt: chalk.cyan("> ") }); const log = createLogger(rl); let serverReady = false; const onServerData = (data) => { const output = data.toString(); if (!serverReady) { process.stdout.write(output); if (output.toLowerCase().includes("ready in") || output.toLowerCase().includes("compiled successfully")) { serverReady = true; console.log(chalk.green("\nServer is ready. You can now type your queries.")); rl.prompt(); } } else { log("nextjs", output, chalk.gray); } }; (_a = childProcess.stdout) == null ? void 0 : _a.on("data", onServerData); (_b = childProcess.stderr) == null ? void 0 : _b.on("data", onServerData); childProcess.on("exit", (code) => { log("system", `Next.js process exited with code ${code}`, chalk.red); process.exit(code != null ? code : 1); }); rl.on("line", async (line) => { const query = line.trim(); if (!serverReady) { log("system", "Server is not ready yet, please wait.", chalk.yellow); rl.prompt(); return; } if (query.toLowerCase() === "exit") { rl.close(); } if (query) { try { const response = await axios.post(API_URL, { prompt: query }); const responseData = typeof response.data === "object" ? JSON.stringify(response.data, null, 2) : response.data; log("agent", responseData, chalk.green); } catch (error) { if (axios.isAxiosError(error)) { log("agent", `Error: ${error.message}`, chalk.red); } else if (error instanceof Error) { log("agent", `An unknown error occurred: ${error.message}`, chalk.red); } } } rl.prompt(); }); rl.on("close", () => { killProcess(); process.exit(0); }); } catch (error) { killProcess(); if (error instanceof Error) { console.error(chalk.red(`Error: ${error.message}`)); } process.exit(1); } } // package.json var version = "1.0.10"; // src/cli.ts var program = new Command(); program.name("microfox").description("Universal CLI tool for creating and managing Microfox projects").version(version); program.command("kickstart").description("Kickstart a new TypeScript SDK or agent package").action(async () => { try { console.log(chalk2.blue("\u{1F680} Package Kickstarter\n")); await kickstartCommand(); } catch (error) { console.error(chalk2.red("\u274C Error:"), error instanceof Error ? error.message : String(error)); process.exit(1); } }); program.command("push").description("Deploy your agent to the Microfox platform").action(async () => { try { await pushCommand(); } catch (error) { console.error(chalk2.red("\u274C Error:"), error instanceof Error ? error.message : String(error)); process.exit(1); } }); program.command("status [runId]").description("Check the deployment status of your agent").action(async (runId) => { try { await statusCommand(runId); } catch (error) { console.error(chalk2.red("\u274C Error:"), error instanceof Error ? error.message : String(error)); process.exit(1); } }); program.command("logs [runId]").description("View the deployment logs for your agent").action(async (runId) => { try { await logsCommand(runId); } catch (error) { console.error(chalk2.red("\u274C Error:"), error instanceof Error ? error.message : String(error)); process.exit(1); } }); program.command("metrics [runId]").description("View the deployment metrics for your agent").action(async (runId) => { try { await metricsCommand(runId); } catch (error) { console.error(chalk2.red("\u274C Error:"), error instanceof Error ? error.message : String(error)); process.exit(1); } }); program.command("code").description("Run the code agent for your project").action(async () => { try { await codeCommand(); } catch (error) { console.error(chalk2.red("\u274C Error:"), error instanceof Error ? error.message : String(error)); process.exit(1); } }); if (process.argv.length <= 2) { program.help(); } program.parse(process.argv); //# sourceMappingURL=cli.mjs.map