runCommand static method

Future<int> runCommand(
  1. String command, {
  2. required CommandLogger logger,
  3. String? heading,
  4. String? successMessage,
  5. String? failedMessage,
  6. int? scrollRows,
  7. String? workingDirectory,
  8. IOSink? stdoutOverride,
  9. IOSink? stderrOverride,
})

Runs a single command, returning its exit code.

The output is rendered in a scrolling section only when it is destined for the interactive terminal: when the caller redirects output to its own stdoutOverride/stderrOverride sinks (e.g. the launch TUI routes it into a log view) or output is not connected to a terminal (e.g. CI or piped output), the output is streamed directly instead, since the section's cursor movement would otherwise corrupt it.

Implementation

static Future<int> runCommand(
  final String command, {
  required final CommandLogger logger,
  final String? heading,
  final String? successMessage,
  final String? failedMessage,
  final int? scrollRows,
  final String? workingDirectory,
  final IOSink? stdoutOverride,
  final IOSink? stderrOverride,
}) async {
  final workingDir = workingDirectory != null
      ? Directory(workingDirectory)
      : null;

  final redirected = stdoutOverride != null || stderrOverride != null;
  if (!redirected) {
    // Resolve the terminal only when output is destined for it, so the
    // redirected path (e.g. the launch TUI) never creates the shared terminal.
    final term = logger.inlineTerminal;
    if (term.hasTerminal) {
      return _runInScrollingSection(
        command,
        term,
        heading: heading,
        successMessage: successMessage,
        failedMessage: failedMessage,
        scrollRows: scrollRows,
        workingDirectory: workingDir,
      );
    }
  }

  if (heading != null) {
    if (redirected) {
      (stdoutOverride ?? stdout).writeln(heading);
    } else {
      logger.info(heading);
    }
  }
  return execute(
    command,
    stdout: stdoutOverride ?? stdout,
    stderr: stderrOverride ?? stderr,
    workingDirectory: workingDir,
  );
}