UNPKG

microfox

Version:

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

187 lines (184 loc) 6.91 kB
#!/usr/bin/env node "use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/commands/code.ts var code_exports = {}; __export(code_exports, { codeCommand: () => codeCommand }); module.exports = __toCommonJS(code_exports); var import_commander = require("commander"); var import_chalk = __toESM(require("chalk")); var import_axios = __toESM(require("axios")); var import_child_process = require("child_process"); // src/utils/findProjectRoot.ts var import_path = __toESM(require("path")); var import_fs = __toESM(require("fs")); async function findProjectRoot(startPath = process.cwd()) { let currentPath = startPath; let count = 0; while (true) { const microfoxRootPath = import_path.default.join(currentPath, "microfox-root"); if (import_fs.default.existsSync(microfoxRootPath)) { return currentPath; } const parentPath = import_path.default.dirname(currentPath); if (parentPath === currentPath || count > 10) { return null; } currentPath = parentPath; } } // src/commands/code.ts var import_path2 = __toESM(require("path")); var import_readline = __toESM(require("readline")); var NEXTJS_PORT = 3e3; var API_URL = `http://localhost:${NEXTJS_PORT}/api/agent`; var createLogger = (rl) => { return (source, message, color) => { import_readline.default.cursorTo(process.stdout, 0); import_readline.default.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 codeAction() { var _a, _b; let childProcess = null; const killProcess = () => { if (childProcess && childProcess.pid) { console.log(import_chalk.default.yellow("\nGracefully shutting down...")); if (process.platform === "win32") { (0, import_child_process.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( import_chalk.default.red("Error: Could not find project root. Make sure you are inside a Microfox project.") ); process.exit(1); } const codeAppPath = import_path2.default.join(projectRoot, "apps", "code"); console.log(import_chalk.default.cyan(`Starting Next.js server in ${codeAppPath}...`)); childProcess = (0, import_child_process.spawn)("npm", ["run", "dev"], { cwd: codeAppPath, shell: true, env: { ...process.env, FORCE_COLOR: "true" } }); const rl = import_readline.default.createInterface({ input: process.stdin, output: process.stdout, prompt: import_chalk.default.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(import_chalk.default.green("\nServer is ready. You can now type your queries.")); rl.prompt(); } } else { log("nextjs", output, import_chalk.default.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}`, import_chalk.default.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.", import_chalk.default.yellow); rl.prompt(); return; } if (query.toLowerCase() === "exit") { rl.close(); } if (query) { try { const response = await import_axios.default.post(API_URL, { prompt: query }); const responseData = typeof response.data === "object" ? JSON.stringify(response.data, null, 2) : response.data; log("agent", responseData, import_chalk.default.green); } catch (error) { if (import_axios.default.isAxiosError(error)) { log("agent", `Error: ${error.message}`, import_chalk.default.red); } else if (error instanceof Error) { log("agent", `An unknown error occurred: ${error.message}`, import_chalk.default.red); } } } rl.prompt(); }); rl.on("close", () => { killProcess(); process.exit(0); }); } catch (error) { killProcess(); if (error instanceof Error) { console.error(import_chalk.default.red(`Error: ${error.message}`)); } process.exit(1); } } var codeCommand = new import_commander.Command("code").description("Run the code agent for your project").action(async () => { try { await codeAction(); } catch (error) { console.error(import_chalk.default.red("\u274C Error:"), error instanceof Error ? error.message : String(error)); process.exit(1); } }); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { codeCommand }); //# sourceMappingURL=code.js.map