UNPKG

@jasondark/proompt

Version:

CLI tool for running AI prompts with structure and repeatability

68 lines 2.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.executeProompt = void 0; const child_process_1 = require("child_process"); const fs_1 = require("fs"); const resolver_1 = require("../config/resolver"); const constants_1 = require("../constants"); /** * Replace template variables in content with provided arguments */ const replaceVariables = (content, args) => { let result = content; for (const [key, value] of Object.entries(args)) { const regex = new RegExp(`\\{\\{\\s*${key}\\s*\\}\\}`, 'g'); result = result.replace(regex, String(value)); } return result; }; /** * Execute proompt with the configured LLM CLI and optional arguments */ const executeProompt = (proomptPath, args = {}) => { const resolvedSettings = (0, resolver_1.resolveSettings)({}); const llmCli = resolvedSettings.llmCli; const outputFormat = resolvedSettings.outputFormat; return new Promise((resolve, reject) => { try { // Read the proompt file content const content = (0, fs_1.readFileSync)(proomptPath, 'utf-8'); // Generate output file names based on configured formats const outputFileNames = outputFormat.map((format) => constants_1.OUTPUT_FILE_NAMES[format]); const isPlural = outputFileNames.length > 1; // Inject output format configuration into template variables const enhancedArgs = { ...args, outputFiles: outputFileNames.map((name) => `\`${name}\``).join(' and '), outputFileList: outputFileNames.join(' and '), outputAction: isPlural ? 'Create identical' : 'Create a', fileOrFiles: isPlural ? 'files' : 'file', }; // Replace variables with provided arguments const processedContent = replaceVariables(content, enhancedArgs); // Write content to stdin instead of using shell command substitution const child = (0, child_process_1.spawn)(llmCli, [], { stdio: ['pipe', 'inherit', 'inherit'], }); // Write the processed content to stdin child.stdin?.write(processedContent); child.stdin?.end(); child.on('close', (code) => { if (code === 0) { resolve(); } else { reject(new Error(`${llmCli} command failed with exit code ${code}`)); } }); child.on('error', (error) => { reject(error); }); } catch (error) { reject(error); } }); }; exports.executeProompt = executeProompt; //# sourceMappingURL=executor.js.map