run$ method

Future run$(
  1. List<String> command, {
  2. Encoding? encoding,
  3. String? workingDirectory,
  4. Map<String, String>? environment,
  5. bool includeParentEnvironment = true,
  6. bool silent = false,
  7. bool noPrompt = false,
  8. bool returnCode = false,
  9. bool autoQuote = true,
})

Execute command and returns stdout

Implementation

Future<dynamic> run$(
  List<String> command, {
  dart_convert.Encoding? encoding,
  String? workingDirectory,
  Map<String, String>? environment,
  bool includeParentEnvironment = true,
  bool silent = false,
  bool noPrompt = false,
  bool returnCode = false,
  bool autoQuote = true,
}) async {
  String executable = command[0];
  List<String> arguments = command.sublist(1).toList();
  encoding ??= this.encoding;
  if (workingDirectory != null) {
    workingDirectory = std_misc.pathExpand(workingDirectory);
  }
  workingDirectory ??= dart_io.Directory.current.absolute.path;
  if (autoQuote) {
    executable = _quote(executable);
    arguments = arguments.map((x) => _quote(x)).toList();
  }
  String display = std_misc.joinCommandLine([executable, ...arguments]);
  if (useUnixShell) {
    String command = std_misc.joinCommandLine([executable, ...arguments]);
    executable = unixShell;
    arguments = ['-c', command];
  } else {
    executable = _unquote(executable);
    arguments = arguments.map((x) => _unquote(x)).toList();
  }
  if (!noPrompt) {
    //print('[$workingDirectory] \$ $display');
    dart_io.stderr.write('[$workingDirectory] \$ $display\n');
  }
  var completer = dart_async.Completer<dynamic>();
  String buffer = '';
  dart_io.Process.start(
    executable,
    arguments,
    workingDirectory: workingDirectory,
    environment: environment,
    includeParentEnvironment: includeParentEnvironment,
    runInShell: !useUnixShell,
  ).then((process) {
    process.stdout
        .transform((encoding ?? dart_io.SystemEncoding()).decoder)
        .listen((data) {
          if (!silent) {
            dart_io.stdout.write(data);
          }
          buffer += data;
        });
    process.stderr
        .transform((encoding ?? dart_io.SystemEncoding()).decoder)
        .listen((data) {
          dart_io.stderr.write(data);
        });
    process.exitCode.then((code) {
      if (returnCode) {
        completer.complete(code);
        return;
      }
      if (code != 0) {
        throw Exception(
          '$display, exitCode $code, workingDirectory: $workingDirectory',
        );
      }
      buffer = buffer.trimRight();
      buffer = std_misc.adjustTextNewlines(buffer);
      completer.complete(buffer);
    });
  });
  return completer.future;
}