({ args = [], options = {}, completed = 'Completed running', shouldFail = false })
| 35 | } |
| 36 | |
| 37 | function runInBackground({ args = [], options = {}, completed = 'Completed running', shouldFail = false }) { |
| 38 | let future = Promise.withResolvers(); |
| 39 | let child; |
| 40 | let stderr = ''; |
| 41 | let stdout = []; |
| 42 | |
| 43 | const run = () => { |
| 44 | args.unshift('--no-warnings'); |
| 45 | child = spawn(execPath, args, { encoding: 'utf8', stdio: 'pipe', ...options }); |
| 46 | |
| 47 | child.stderr.on('data', (data) => { |
| 48 | stderr += data; |
| 49 | }); |
| 50 | |
| 51 | const rl = createInterface({ input: child.stdout }); |
| 52 | rl.on('line', (data) => { |
| 53 | if (!data.startsWith('Waiting for graceful termination') && !data.startsWith('Gracefully restarted')) { |
| 54 | stdout.push(data); |
| 55 | if (data.startsWith(completed)) { |
| 56 | future.resolve({ stderr, stdout }); |
| 57 | future = Promise.withResolvers(); |
| 58 | stdout = []; |
| 59 | stderr = ''; |
| 60 | } else if (data.startsWith('Failed running')) { |
| 61 | const settle = () => { |
| 62 | if (shouldFail) { |
| 63 | future.resolve({ stderr, stdout }); |
| 64 | } else { |
| 65 | future.reject({ stderr, stdout }); |
| 66 | } |
| 67 | future = Promise.withResolvers(); |
| 68 | stdout = []; |
| 69 | stderr = ''; |
| 70 | }; |
| 71 | // If stderr is empty, wait for it to receive data before settling. |
| 72 | // This handles the race condition where stdout arrives before stderr. |
| 73 | if (stderr === '') { |
| 74 | child.stderr.once('data', settle); |
| 75 | } else { |
| 76 | settle(); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | }); |
| 81 | }; |
| 82 | |
| 83 | return { |
| 84 | async done() { |
| 85 | child?.kill(); |
| 86 | future.resolve(); |
| 87 | return { stdout, stderr }; |
| 88 | }, |
| 89 | restart(timeout = 1000) { |
| 90 | if (!child) { |
| 91 | run(); |
| 92 | } |
| 93 | const timer = setTimeout(() => { |
| 94 | if (!future.resolved) { |
no outgoing calls
no test coverage detected
searching dependent graphs…