UNPKG

@forzalabs/remora

Version:

A powerful CLI tool for seamless data translation.

117 lines (116 loc) 6.31 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.run = void 0; const chalk_1 = __importDefault(require("chalk")); const Environment_1 = __importDefault(require("../engines/Environment")); const compile_1 = require("./compile"); const Helper_1 = __importDefault(require("../helper/Helper")); const LicenceManager_1 = __importDefault(require("../licencing/LicenceManager")); const ExecutorOrchestrator_1 = __importDefault(require("../executors/ExecutorOrchestrator")); const ProcessENVManager_1 = __importDefault(require("../engines/ProcessENVManager")); const run = (consumerName, options) => __awaiter(void 0, void 0, void 0, function* () { try { (0, compile_1.compile)(); // needed for newline console.log(); const consumersToExecute = []; if (consumerName && consumerName.length > 0) { const cons = Environment_1.default.getConsumer(consumerName); if (!cons) throw new Error(`Consumer with name "${consumerName}" was not found.`); consumersToExecute.push(cons); } else if ((options === null || options === void 0 ? void 0 : options.project) && options.project.length > 0) { const projectConsumers = Environment_1.default._env.consumers.filter(c => { var _a; return ((_a = c.project) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === options.project.toLowerCase(); }); if (projectConsumers.length === 0) { throw new Error(`No consumers found for project "${options.project}".`); } consumersToExecute.push(...projectConsumers); } else { consumersToExecute.push(...Environment_1.default._env.consumers); } if (consumersToExecute.length === 1) console.log(chalk_1.default.blue(`Running consumer ${consumersToExecute[0].name}...\n`)); else console.log(chalk_1.default.blue(`Running consumers ${consumersToExecute.map(x => x.name).join(', ')}...\n`)); const results = []; for (let i = 0; i < consumersToExecute.length; i++) { const consumer = consumersToExecute[i]; try { const remoraLicenceKey = ProcessENVManager_1.default.getEnvVariable('REMORA_LICENCE_KEY'); const check = LicenceManager_1.default.validate(remoraLicenceKey); if (!check.valid) { console.error(`Invalid Remora licence key, the product is not active: remember to set "REMORA_LICENCE_KEY" environment variable.`); process.exit(1); } const response = yield ExecutorOrchestrator_1.default.launch({ consumer, details: { invokedBy: 'CLI', user: { _id: check.customer, name: check.customer, type: 'licence' } }, logProgress: true }); results.push({ success: true, consumer, response }); } catch (error) { const myErr = Helper_1.default.asError(error); results.push({ success: false, consumer, error: myErr.message }); if (Helper_1.default.isDev()) console.log(myErr.stack); } } results.forEach(({ response, consumer, success, error }) => { if (success) { const { elapsedMS, outputCount, resultUri } = response; const rowCount = outputCount; const duration = Helper_1.default.formatDuration(elapsedMS); const performanceInfo = chalk_1.default.gray(` (${rowCount} rows, ${duration})`); if (resultUri) console.log(chalk_1.default.green(`• Consumer ${consumer.name} -> ${resultUri}`) + performanceInfo); else console.log(chalk_1.default.green(`• Consumer ${consumer.name} `) + performanceInfo); } else { console.log(chalk_1.default.red(`• Consumer ${consumer.name} -> Failed: ${error}`)); } }); // Calculate totals for successful runs const successfulResults = results.filter(x => x.success); const totalRows = successfulResults.reduce((sum, result) => { var _a, _b; return sum + ((_b = (_a = result.response) === null || _a === void 0 ? void 0 : _a.outputCount) !== null && _b !== void 0 ? _b : 0); }, 0); const totalDuration = successfulResults.reduce((sum, result) => { var _a; return sum + (((_a = result.response) === null || _a === void 0 ? void 0 : _a.elapsedMS) || 0); }, 0); const totalsInfo = chalk_1.default.gray(` (${totalRows} rows, ${Helper_1.default.formatDuration(totalDuration)})`); if (results.some(x => !x.success)) console.log(chalk_1.default.blueBright('\nℹ️ Run completed with errors') + totalsInfo); else console.log(chalk_1.default.green('\n✅ Run complete!') + totalsInfo); process.exit(1); } catch (err) { const myErr = Helper_1.default.asError(err); console.error(chalk_1.default.red.bold('\n❌ Unexpected error during run:'), myErr.message); if (Helper_1.default.isDev()) console.log(myErr.stack); process.exit(1); } }); exports.run = run;