(name, command, args, options = {})
| 105 | } |
| 106 | |
| 107 | async function runCommand(name, command, args, options = {}) { |
| 108 | const cwd = options.cwd || root; |
| 109 | const env = { ...process.env, ...(options.env || {}) }; |
| 110 | const timeoutMs = |
| 111 | options.timeoutMs ?? |
| 112 | Number(process.env.CAP_VERIFY_COMMAND_TIMEOUT_MS || "180000"); |
| 113 | const stdoutPath = outputPath(name, "stdout"); |
| 114 | const stderrPath = outputPath(name, "stderr"); |
| 115 | const statusPath = outputPath(name, "status.json"); |
| 116 | const stdout = fs.createWriteStream(stdoutPath); |
| 117 | const stderr = fs.createWriteStream(stderrPath); |
| 118 | const startedAt = Date.now(); |
| 119 | |
| 120 | const result = await new Promise((resolve, reject) => { |
| 121 | const child = spawn(command, args, { |
| 122 | cwd, |
| 123 | env, |
| 124 | stdio: ["ignore", "pipe", "pipe"], |
| 125 | }); |
| 126 | |
| 127 | let stdoutText = ""; |
| 128 | let stderrText = ""; |
| 129 | let timedOut = false; |
| 130 | let killTimer = null; |
| 131 | const timeoutTimer = |
| 132 | timeoutMs > 0 |
| 133 | ? setTimeout(() => { |
| 134 | timedOut = true; |
| 135 | child.kill("SIGTERM"); |
| 136 | killTimer = setTimeout(() => child.kill("SIGKILL"), 5000); |
| 137 | }, timeoutMs) |
| 138 | : null; |
| 139 | |
| 140 | child.stdout.on("data", (chunk) => { |
| 141 | const text = chunk.toString(); |
| 142 | stdoutText += text; |
| 143 | stdout.write(chunk); |
| 144 | }); |
| 145 | |
| 146 | child.stderr.on("data", (chunk) => { |
| 147 | const text = chunk.toString(); |
| 148 | stderrText += text; |
| 149 | stderr.write(chunk); |
| 150 | }); |
| 151 | |
| 152 | child.on("error", (error) => { |
| 153 | if (timeoutTimer) { |
| 154 | clearTimeout(timeoutTimer); |
| 155 | } |
| 156 | if (killTimer) { |
| 157 | clearTimeout(killTimer); |
| 158 | } |
| 159 | reject(error); |
| 160 | }); |
| 161 | child.on("close", (code, signal) => { |
| 162 | if (timeoutTimer) { |
| 163 | clearTimeout(timeoutTimer); |
| 164 | } |
no test coverage detected