UNPKG

cookie-ai-cli

Version:

A command-line interface tool designed to bridge the gap between natural language processing and command-line operations.

63 lines 2.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.handleCommand = void 0; const node_child_process_1 = require("node:child_process"); const ask_question_1 = require("./ask-question"); const send_chat_1 = require("./send-chat"); const colors_1 = require("./utils/colors"); async function handleCommand({ rl, command, values, description, }) { return new Promise(async (resolve, reject) => { let fullCommand = command; if (values) { fullCommand = command.replaceAll(/{(\w+)}/g, (_, match) => { const keyWithBraces = `{${match}}`; // Construct the key with curly braces return values[keyWithBraces] || `{${match}}`; // Replace with value or keep the placeholder if value is not found }); } console.log(`command: ${colors_1.colors.red}${fullCommand}${colors_1.colors.reset}`); if (description) { console.log(`description: ${colors_1.colors.yellow}${description}${colors_1.colors.reset}`); } const answer = await (0, ask_question_1.askQuestion)(rl, `Run this command? (y/n) `); if (answer === "y") { console.log("Executing command: ", `${colors_1.colors.blue}${fullCommand}${colors_1.colors.reset}`); const [bin, ...args] = fullCommand.split(" "); const proc = (0, node_child_process_1.spawn)(bin, args, { stdio: ["inherit", "inherit", "pipe"], shell: true, cwd: process.cwd(), }); let stderrOutput = ""; proc.stderr.on("data", (data) => { console.log(data.toString()); stderrOutput += data.toString(); }); // Listen to the close event proc.on("close", async (code) => { if (code !== 0) { console.log(`${colors_1.colors.red}Command exited with error code: ${code}${colors_1.colors.reset}`); await (0, send_chat_1.sendChat)({ isError: true, message: `Command exited with error code: ${code}\n${stderrOutput}`, rl, }); } console.log(``); resolve(code); }); process.on("SIGINT", () => { // Kill the spawned child processes and parent process if the user presses Ctrl+C proc.kill("SIGINT"); reject(); process.exit(0); }); } else { console.log("Command aborted."); reject(); process.exit(0); } }); } exports.handleCommand = handleCommand; //# sourceMappingURL=handle-command.js.map