( command: string, args: string[], opts?: ProcessOptions )
| 102 | result.stdoutTruncated === true || result.stderrTruncated === true |
| 103 | ? TRUNCATION_MARKER |
| 104 | : undefined, |
| 105 | ] |
| 106 | .filter(value => value !== undefined) |
| 107 | .join(', '); |
| 108 | } |
| 109 | |
| 110 | function terminationMessage(reason: TerminationReason): string { |
| 111 | switch (reason) { |
| 112 | case 'timeout': |
| 113 | return EXEC_TIMEOUT_MESSAGE; |
| 114 | case 'inactivity_timeout': |
| 115 | return EXEC_INACTIVITY_TIMEOUT_MESSAGE; |
| 116 | case 'hard_timeout': |
| 117 | return EXEC_HARD_TIMEOUT_MESSAGE; |
| 118 | case 'abort': |
| 119 | return EXEC_ABORTED_MESSAGE; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | export function runProcess( |
| 124 | command: string, |
| 125 | args: string[], |
| 126 | opts?: ProcessOptions |
| 127 | ): Promise<ExecResult> { |
| 128 | const startedAt = Date.now(); |
| 129 | if (opts?.signal?.aborted) { |
| 130 | return Promise.resolve({ |
| 131 | stdout: '', |
| 132 | stderr: EXEC_ABORTED_MESSAGE, |
| 133 | exitCode: EXEC_TIMEOUT_EXIT_CODE, |
| 134 | elapsedMs: 0, |
| 135 | terminationReason: 'abort', |
| 136 | ...(opts.rawOutput ? { stdoutBytes: Buffer.alloc(0), stderrBytes: Buffer.alloc(0) } : {}), |
| 137 | }); |
| 138 | } |
| 139 | |
| 140 | return new Promise((resolve, reject) => { |
| 141 | const options = { |
| 142 | cwd: opts?.cwd, |
| 143 | ...(opts?.inheritEnv === false |
| 144 | ? { env: opts.env ?? {} } |
| 145 | : opts?.env |
| 146 | ? { env: { ...process.env, ...opts.env } } |
| 147 | : {}), |
| 148 | }; |
| 149 | const owned = currentOwnedProcessScope(); |
| 150 | const proc = |
| 151 | owned?.spawn(command, args, options) ?? |
| 152 | spawn(command, args, { |
| 153 | ...options, |
| 154 | detached: true, |
| 155 | stdio: [opts?.stdinFd ?? 'ignore', 'pipe', 'pipe'], |
| 156 | }); |
| 157 | const stdoutStream = proc.stdout; |
| 158 | const stderrStream = proc.stderr; |
| 159 | if (stdoutStream === null || stderrStream === null) { |
| 160 | proc.kill(); |
| 161 | reject(new Error('Child process did not create output streams')); |
no test coverage detected