UNPKG

projex

Version:
82 lines (81 loc) 2.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.childProcessRunCommandRun = exports.executeCommand = void 0; const _shared_1 = require("../../../../shared/index"); const child_process_1 = require("child_process"); const executeCommand = (commandToUse) => { if (!commandToUse) { throw new Error('no command to execute'); } _shared_1.log.warn(`command to execute: ${commandToUse}`); (0, exports.childProcessRunCommandRun)(commandToUse); }; exports.executeCommand = executeCommand; const childProcessRunCommandRun = function (command) { const task = (0, child_process_1.spawn)(`${command}`, [], { shell: true, }); const validateErrors = (data) => { const ignoreError = () => { let excludeError = false; _shared_1.ERROR_TO_EXCLUDE.map((exclude) => { if (data.toString('utf8').includes(exclude) && !excludeError) { _shared_1.log.debug('Ignore error', { data: data.toString('utf8'), exclude, }); excludeError = true; } }); return excludeError; }; const checkErrors = () => { let haveError = false; _shared_1.ERROR_EXECUTION.map((item) => { if (data.toString('utf8').includes(item) && !haveError) { _shared_1.log.debug('Fail with error', { data: data.toString('utf8'), item, }); haveError = true; } }); return haveError; }; let excludeError = ignoreError(); if (excludeError) return; if (checkErrors()) { setTimeout(() => { _shared_1.log.error(`finish with errors on run the command`); throw new Error('finish execution with errors'); }, 5000); return; } }; const validateSuccess = (data) => { _shared_1.SUCCESS_EXECUTION.map((success) => { if (data.toString('utf8').includes(success)) { _shared_1.log.info(`finish successfully on run the command when detect ${success}`); process.exit(0); } }); }; const acceptPrompt = () => { // simulating user input task.stdin.write('y\n'); }; // Método para imprimir el log normal task.stdout.on('data', (data) => { console.log(data.toString('utf8')); validateErrors(data); validateSuccess(data); acceptPrompt(); }); // Método para imprimir el log de error task.stderr.on('data', function (data) { console.log(data.toString('utf8')); validateErrors(data); }); }; exports.childProcessRunCommandRun = childProcessRunCommandRun;