UNPKG

@meldscience/meld

Version:

pipeable one-shot prompt scripting toolkit

147 lines 7.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.remarkProcessMeldNodes = void 0; const unist_util_visit_1 = require("unist-util-visit"); const child_process_1 = require("child_process"); const fs_1 = require("fs"); const path_1 = require("path"); const markdownImporter_1 = require("../markdownImporter"); const codeImporter_1 = require("../codeImporter"); const remarkProcessMeldNodes = function (options) { return async function transformer(tree, file) { const errors = []; // Initialize meldErrors in file.data file.data = file.data || {}; file.data.meldErrors = []; (0, unist_util_visit_1.visit)(tree, 'meldDirective', (node, index, parent) => { if (options.debug) { console.log('Visiting node:', { type: node.type, meldType: node.meldType, index, parentType: parent?.type, parentChildren: parent?.children?.length }); } if (index === null || index === undefined) { if (options.debug) console.log('Skipping node due to null/undefined index'); return; } try { if (node.meldType === 'cmd') { if (options.debug) console.log('Processing command:', node.data?.command); try { const output = (0, child_process_1.execSync)(node.data?.command || '', { encoding: 'utf-8', cwd: options.currentFileDir, stdio: ['pipe', 'pipe', 'pipe'] }).trim(); if (options.debug) { console.log('Command output:', output); console.log('Replacing node at index', index); } // Replace with a text node containing the output parent.children[index] = { type: 'text', value: output }; if (options.debug) console.log('Node replaced, new node:', parent.children[index]); } catch (cmdError) { const errorMessage = options.detailedErrors ? 'Command produced error output (included below)\n' + (cmdError.stderr || cmdError.message || 'Unknown error') : 'Error: Error processing cmd directive'; parent.children[index] = { type: 'text', value: errorMessage }; file.data.meldErrors.push(errorMessage); } } else { // It's an import directive const importPath = node.data?.importPath; if (options.debug) console.log('Processing import:', importPath, 'heading:', node.data?.heading); if (!importPath) { throw new Error('Invalid import directive: missing import path'); } // Logic to figure out if it's a .md or .ts if (importPath.endsWith('.md')) { const heading = node.data?.heading; // Reuse existing markdownImporter try { const resolvedPath = (0, path_1.join)(options.currentFileDir, importPath); if (!(0, fs_1.existsSync)(resolvedPath)) { throw new Error('File not found'); } const result = (0, markdownImporter_1.importMarkdownSection)({ filePath: resolvedPath, heading, headingLevelOverride: node.data?.headingLevelOverride, headingTextOverride: node.data?.headingTextOverride }); if (!result.headingFound && heading) { throw new Error(`Heading "${heading}" not found in ${importPath}`); } parent.children[index] = { type: 'text', value: result.content }; } catch (error) { throw new Error(`Error processing import directive: ${error.message}`); } } else { // Assume it's a code file try { const resolvedPath = (0, path_1.join)(options.currentFileDir, importPath); if (!(0, fs_1.existsSync)(resolvedPath)) { throw new Error('File not found'); } const result = (0, codeImporter_1.importCodeSymbols)({ filePath: resolvedPath, symbolList: node.data?.importSymbolList || [], headingLevelOverride: node.data?.headingLevelOverride, headingTextOverride: node.data?.headingTextOverride }); if (result.content.trim() === '') { const errorMessage = 'Error: Error processing import directive'; parent.children[index] = { type: 'text', value: errorMessage }; file.data.meldErrors.push(errorMessage); } else { parent.children[index] = { type: 'text', value: result.content }; } } catch (error) { throw new Error(`Error processing import directive: ${error.message}`); } } } } catch (error) { const errorMessage = `Error: ${error.message}\n\n`; parent.children[index] = { type: 'text', value: errorMessage }; file.data.meldErrors.push(errorMessage); } }); return tree; }; }; exports.remarkProcessMeldNodes = remarkProcessMeldNodes; //# sourceMappingURL=remarkProcessMeldNodes.js.map