|
| 1 | +const {spawn} = require('child_process') |
| 2 | +const {returnStringType} = require('./helpers') |
| 3 | + |
| 4 | +function getRunProcess(cmd) { |
| 5 | + const {command, args = [], runOpts = {}} = cmd |
| 6 | + return spawn(command, args, runOpts) |
| 7 | +} |
| 8 | + |
| 9 | +function buildSpawnRunner(failedByAssert, runOpts) { |
| 10 | + |
| 11 | + const { |
| 12 | + addSpecificOptionsBeforeRun, |
| 13 | + currentExecutionVariable, |
| 14 | + longestProcessTime, |
| 15 | + debugProcess, |
| 16 | + reformatCommand, |
| 17 | + stackAnalize |
| 18 | + } = runOpts |
| 19 | + |
| 20 | + const executeCommandAsync = (cmd) => new Promise((resolve) => { |
| 21 | + const now = +Date.now() |
| 22 | + |
| 23 | + let watcher = null |
| 24 | + let additionalOpts = null |
| 25 | + let originalCmd = cmd |
| 26 | + let specificCallBack = null |
| 27 | + let killTooLongExecution = null |
| 28 | + let executionStack = '' |
| 29 | + |
| 30 | + /** |
| 31 | + * @now this variable will be used for process kill if time more than @longestProcessTime |
| 32 | + */ |
| 33 | + |
| 34 | + /** |
| 35 | + * @param {undefined|function} addSpecificOptions if function cmd will go to this function as argument |
| 36 | + */ |
| 37 | + |
| 38 | + if(addSpecificOptionsBeforeRun) { |
| 39 | + const cmdObj = addSpecificOptionsBeforeRun(cmd) |
| 40 | + cmd = cmdObj.cmd |
| 41 | + |
| 42 | + specificCallBack = cmdObj.cmdExecutableCB |
| 43 | + } |
| 44 | + |
| 45 | + if(debugProcess) {console.log(cmd)} |
| 46 | + |
| 47 | + const proc = getRunProcess(cmd) |
| 48 | + |
| 49 | + |
| 50 | + watcher = setInterval(() => executionWatcher(debugProcess, now, longestProcessTime, watcher, proc, resolve, cmd), 5000) |
| 51 | + |
| 52 | + proc.on('exit', () => {clearInterval(watcher)}) |
| 53 | + |
| 54 | + proc.stdout.on('data', (data) => { |
| 55 | + (debugProcess) && console.log(data.toString('utf8')); executionStack += data.toString() |
| 56 | + }) |
| 57 | + proc.stderr.on('data', (data) => console.log(data.toString('utf8'))) |
| 58 | + proc.on('error', (e) => {console.error(e)}) |
| 59 | + |
| 60 | + proc.on('close', async (code) => { |
| 61 | + |
| 62 | + let commandToRerun = null |
| 63 | + |
| 64 | + if(debugProcess) {console.log(executionStack, code)} |
| 65 | + |
| 66 | + // if process code 0 - exit as a success result |
| 67 | + if(code === 0) { |
| 68 | + resolve(commandToRerun); return |
| 69 | + } |
| 70 | + // stackAnalize - check that stack contains or not contains some specific data |
| 71 | + if(code !== 0 && stackAnalize && stackAnalize(executionStack)) { |
| 72 | + commandToRerun = cmd |
| 73 | + } else if(code !== 0 && reformatCommand) { |
| 74 | + commandToRerun = cmd |
| 75 | + } else { |
| 76 | + failedByAssert.push(cmd) |
| 77 | + } |
| 78 | + |
| 79 | + // if code === 0 do nothing, success |
| 80 | + if(specificCallBack) { |
| 81 | + if(specificCallBack.then || returnStringType(specificCallBack) === '[object AsyncFunction]') { |
| 82 | + await specificCallBack() |
| 83 | + } else { |
| 84 | + specificCallBack() |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if(reformatCommand && commandToRerun) { |
| 89 | + commandToRerun = reformatCommand(commandToRerun, executionStack) |
| 90 | + } |
| 91 | + // addSpecificOptionsBeforeRun was defined - we should remove useless opts what will be added in next iteration |
| 92 | + if(additionalOpts) { |
| 93 | + commandToRerun = commandToRerun.replace(additionalOpts, '') |
| 94 | + } |
| 95 | + |
| 96 | + resolve(commandToRerun) |
| 97 | + }) |
| 98 | + }) |
| 99 | + return executeCommandAsync |
| 100 | +} |
| 101 | + |
| 102 | +module.exports = { |
| 103 | + buildSpawnRunner |
| 104 | +} |
0 commit comments