pipe method

Future<List<ProcessResult>> pipe(
  1. String script
)

Similar to the | on linux it allows to pipe the stdout of the ran commands as stdin to the next command.

Implementation

Future<List<ProcessResult>> pipe(String script) async {
  var results = await this;
  if (results is ProcessResultInternalList) {
    var shellResults = results.shellProcessResults;
    var output = shellResults.stdoutAsUint8List;
    var shell = shellResults.shell;
    var stdinController = ShellUint8ListController();
    stdinController.sink.add(output);
    // Closed now (the output is buffered) so that the command reading its
    // input (i.e. cat) gets the end of it.
    stdinController.close();
    var newShell = shell.cloneWithOptions(
      shell.options.clone(stdin: stdinController.stream),
    );
    return await newShell.run(script);
  }
  throw UnsupportedError(
    'Invalid results type: ${results.runtimeType}, expected ProcessResultInternalList',
  );
}