({ signal }: { signal: AbortSignal | null })
| 362 | } |
| 363 | |
| 364 | private async executeCommandAsync({ signal }: { signal: AbortSignal | null }): Promise<void> { |
| 365 | assert(this.command, 'Command must be defined.'); |
| 366 | |
| 367 | const interpolatedCommand = interpolateJobContext({ |
| 368 | target: this.command, |
| 369 | context: this.getInterpolationContext(), |
| 370 | }); |
| 371 | |
| 372 | const command = this.interpolateInputsOutputsAndGlobalContextInTemplate( |
| 373 | `${interpolatedCommand}`, |
| 374 | this.inputs |
| 375 | ); |
| 376 | this.ctx.logger.debug(`Interpolated inputs in the command template`); |
| 377 | |
| 378 | const scriptPath = await saveScriptToTemporaryFileAsync(this.ctx.global, this.id, command); |
| 379 | this.ctx.logger.debug(`Saved script to ${scriptPath}`); |
| 380 | |
| 381 | const { command: shellCommand, args } = getShellCommandAndArgs(this.shell, scriptPath); |
| 382 | this.ctx.logger.debug( |
| 383 | `Executing script: ${shellCommand}${args !== undefined ? ` ${args.join(' ')}` : ''}` |
| 384 | ); |
| 385 | |
| 386 | try { |
| 387 | const workingDirectoryStat = await fs.stat(this.ctx.workingDirectory); |
| 388 | if (!workingDirectoryStat.isDirectory()) { |
| 389 | this.ctx.logger.error( |
| 390 | `Working directory "${this.ctx.workingDirectory}" exists, but is not a directory` |
| 391 | ); |
| 392 | } |
| 393 | } catch (err: any) { |
| 394 | if (err?.code === 'ENOENT') { |
| 395 | this.ctx.logger.error( |
| 396 | { err }, |
| 397 | `Working directory "${this.ctx.workingDirectory}" does not exist` |
| 398 | ); |
| 399 | } else { |
| 400 | this.ctx.logger.error( |
| 401 | { err }, |
| 402 | `Cannot access working directory "${this.ctx.workingDirectory}"` |
| 403 | ); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | await spawnAsync(shellCommand, args ?? [], { |
| 408 | cwd: this.ctx.workingDirectory, |
| 409 | logger: this.ctx.logger, |
| 410 | env: this.getScriptEnv(), |
| 411 | // stdin is /dev/null, std{out,err} are piped into logger. |
| 412 | stdio: ['ignore', 'pipe', 'pipe'], |
| 413 | signal: signal ?? undefined, |
| 414 | }); |
| 415 | this.ctx.logger.debug(`Script completed successfully`); |
| 416 | } |
| 417 | |
| 418 | private async executeFnAsync({ signal }: { signal: AbortSignal | null }): Promise<void> { |
| 419 | assert(this.fn, 'Function (fn) must be defined'); |
no test coverage detected