UNPKG

@cere/rob-cli

Version:

CLI tool for deploying and managing rafts and data sources

103 lines 3.47 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.loadScriptFile = loadScriptFile; exports.processQueryScripts = processQueryScripts; exports.processIndexingScript = processIndexingScript; exports.processRaftScripts = processRaftScripts; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const logger_1 = require("./logger"); const logger = new logger_1.Logger('ScriptHandler'); /** * Load content from a script file */ function loadScriptFile(filePath, basePath = process.cwd()) { try { // Try both as relative to basePath and as absolute path let resolvedPath = path_1.default.resolve(basePath, filePath); if (!fs_1.default.existsSync(resolvedPath)) { // If file not found, try absolute path resolvedPath = path_1.default.resolve(filePath); if (!fs_1.default.existsSync(resolvedPath)) { throw new Error(`Script file not found: ${filePath}`); } } logger.debug(`Loading script file: ${resolvedPath}`); return fs_1.default.readFileSync(resolvedPath, 'utf8'); } catch (error) { if (error instanceof Error) { logger.error(`Error loading script file: ${error.message}`); } else { logger.error('Unknown error loading script file'); } throw error; } } /** * Process script file references in a query operation */ function processQueryScripts(query, basePath) { const result = { name: '', alias: '', description: '', script: { name: '', tsCode: '', }, }; if (!query.script && query.scriptFile) { const fileName = (path_1.default.basename(query.scriptFile) || '').split('.')[0]; const scriptContent = loadScriptFile(query.scriptFile, basePath); if (query.id) { result.id = query.id; } result.name = fileName; result.alias = query.alias; result.description = query.description; result.script = { name: fileName, tsCode: scriptContent, }; } return result; } /** * Process script file reference in indexing configuration */ function processIndexingScript(indexingScript, basePath) { const result = { name: '', tsCode: '', }; if (indexingScript.scriptFile) { const fileName = (path_1.default.basename(indexingScript.scriptFile) || '').split('.')[0]; const scriptContent = loadScriptFile(indexingScript.scriptFile, basePath); if (indexingScript.id) { result.id = indexingScript.id; } result.name = fileName; result.tsCode = scriptContent; } return result; } /** * Process all script file references in a raft */ function processRaftScripts(raft, basePath) { // Process indexing script if (raft.indexingScript) { raft.indexingScript = processIndexingScript(raft.indexingScript, basePath); } // Process query scripts if (raft.queryOperations && raft.queryOperations.length > 0) { raft.queryOperations = raft.queryOperations.map(query => processQueryScripts(query, basePath)); } return raft; } //# sourceMappingURL=script-handler.js.map