UNPKG

topkat-utils

Version:

A comprehensive collection of TypeScript/JavaScript utility functions for common programming tasks. Includes validation, object manipulation, date handling, string formatting, and more. Zero dependencies, fully typed, and optimized for performance.

64 lines 3.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.execWaitForOutput = void 0; const logger_utils_1 = require("./src/logger-utils"); const child_process_1 = require("child_process"); /** Execute a custom command into a child terminal and wait for process completion */ async function execWaitForOutput(command, config = {}) { let outputStream = ''; const { nbSecondsBeforeKillingProcess = 20, streamConsoleOutput = () => true, errorHandle = 'throw', logOutputStream = true, stringOrRegexpToSearchForConsideringDone, keyCodeToSend = {}, onStartProcess, execOptions } = config; try { return await new Promise((res, reject) => { const to = nbSecondsBeforeKillingProcess > 0 ? setTimeout(() => { logger_utils_1.C.error(`Exec timeout for ${command}`); reject(`Exec timeout for ${command}`); }, nbSecondsBeforeKillingProcess * 1000) : undefined; const process2 = (0, child_process_1.exec)(command, execOptions); if (onStartProcess) onStartProcess(process2); const resolve = () => { process2?.kill('SIGINT'); clearTimeout(to); res(outputStream); }; const stdCallback = (data) => { if (logOutputStream) logger_utils_1.C.log(data); streamConsoleOutput(data); outputStream += data; if (stringOrRegexpToSearchForConsideringDone) { const regexp = typeof stringOrRegexpToSearchForConsideringDone === 'string' ? new RegExp(stringOrRegexpToSearchForConsideringDone) : stringOrRegexpToSearchForConsideringDone; if (regexp.test(data)) resolve(); } }; const exitCallback = (exitCode) => { if (exitCode === 0) resolve(); else if (stringOrRegexpToSearchForConsideringDone || typeof exitCode === 'number' && exitCode !== 0) reject(exitCode); else resolve(); }; process2.stderr?.on('data', stdCallback); process2.stdout?.on('data', stdCallback); process2.on('exit', exitCallback); process2.on('close', exitCallback); for (const keyCode in keyCodeToSend) { setTimeout(() => { process2.stdin?.write(keyCode); if (logOutputStream) logger_utils_1.C.log('Sending charcode to stdin: ' + keyCode); }, keyCodeToSend[keyCode]); } }); } catch (_) { if (errorHandle === 'log') logger_utils_1.C.error(`Something went wrong using this command: ${command}\nPlease check this log:\n${outputStream}`); else throw `Something went wrong using this command: ${command}\nPlease check this log:\n${outputStream}`; } } exports.execWaitForOutput = execWaitForOutput; //# sourceMappingURL=backend.js.map