UNPKG

@forzalabs/remora

Version:

A powerful CLI tool for seamless data translation.

78 lines (77 loc) 4.13 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.automap = void 0; const chalk_1 = __importDefault(require("chalk")); const ora_1 = __importDefault(require("ora")); const compile_1 = require("./compile"); const AutoMapperEngine_1 = __importDefault(require("../engines/ai/AutoMapperEngine")); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const Environment_1 = __importDefault(require("../engines/Environment")); const ProducerEngine_1 = __importDefault(require("../engines/ProducerEngine")); /** * e.g. npm run automap -- myclaims Claim */ const automap = (producerName, schemaNames) => __awaiter(void 0, void 0, void 0, function* () { try { (0, compile_1.compile)(); const spinner = (0, ora_1.default)(chalk_1.default.blue('Auto-mapping producer data...\n')).start(); // Get the producer const producer = Environment_1.default.getProducer(producerName); if (!producer) { throw new Error(`Producer ${producerName} not found`); } const source = Environment_1.default.getSource(producer.source); if (!source) { throw new Error(`Source ${producer.source} not found`); } // Get the specified schemas const schemas = []; for (const schemaName of schemaNames) { const schema = Environment_1.default.getSchema(schemaName); if (!schema) { throw new Error(`Schema ${schemaName} not found`); } schemas.push(schema); } // Read and convert sample data const sampleData = yield ProducerEngine_1.default.readSampleData(producer); // Convert sample data to strings for AutoMapperEngine const sampleStrings = sampleData.map(item => JSON.stringify(item)); // Call the automapper const mapResult = yield AutoMapperEngine_1.default.map(sampleStrings, schemas, producer.settings.fileKey, [source]); // Create the producers based on the mapping for (const producer of mapResult.producers) { const producerPath = path_1.default.join('remora/producers', `${producer.name}.json`); fs_1.default.writeFileSync(producerPath, JSON.stringify(producer, null, 4)); console.log(chalk_1.default.blue(`Created producer: ${producer.name}`)); } // Create the consumers based on the mapping for (const consumer of mapResult.consumers) { const consumerPath = path_1.default.join('remora/consumers', `${consumer.name}.json`); fs_1.default.writeFileSync(consumerPath, JSON.stringify(consumer, null, 4)); console.log(chalk_1.default.blue(`Created consumer: ${consumer.name}`)); } spinner.succeed('Producer has been successfully mapped'); console.log(chalk_1.default.green(`\n✅ Created ${mapResult.producers.length} producers!`)); console.log(chalk_1.default.green(`✅ Created ${mapResult.consumers.length} consumers!`)); process.exit(0); } catch (err) { console.error(chalk_1.default.red.bold('\n❌ Unexpected error during automapping:'), err instanceof Error ? err.message : String(err)); process.exit(1); } }); exports.automap = automap;