cookie-ai-cli
Version:
A command-line interface tool designed to bridge the gap between natural language processing and command-line operations.
50 lines • 2.23 kB
JavaScript
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 proc = (0, node_child_process_1.exec)(fullCommand, async (error, stdout, stderr) => {
if (error) {
await (0, send_chat_1.sendChat)({ isError: true, message: error.message, rl });
return;
}
if (stdout)
console.log(stdout);
if (stderr)
console.log(stderr);
});
// Listen to the close event
proc.on("close", (code) => {
if (code !== 0) {
console.log(`${colors_1.colors.red}Command exited with error code: ${code}${colors_1.colors.reset}`);
}
resolve(code);
});
}
else {
console.log("Command aborted.");
reject();
process.exit(0);
}
});
}
exports.handleCommand = handleCommand;
//# sourceMappingURL=exec.js.map
;