runProcess method

Future<int> runProcess(
  1. String command, {
  2. String? workingDirectory,
  3. bool? runInShell,
  4. bool silent = false,
})

Run a process with the given command

Implementation

Future<int> runProcess(String command,
    {String? workingDirectory, bool? runInShell, bool silent = false}) async {
  // Parse command properly handling quotes
  final List<String> parts = _parseCommand(command);
  final String executable = parts[0];
  final List<String> args = parts.sublist(1);

  // Run the process
  final Process process = await Process.start(
    executable,
    args,
    mode: ProcessStartMode.normal,
    workingDirectory: workingDirectory,
    runInShell: runInShell ?? false,
  );

  if (silent == false) {
    // Listen to the process output
    process.stdout.transform(SystemEncoding().decoder).listen((String data) {
      // Print the output of the command
      print(data);
    });

    process.stderr.transform(SystemEncoding().decoder).listen((String data) {
      // Print the error output of the command
      print(data);
    });
  }

  // Wait for the process to complete
  final int exitCode = await process.exitCode;
  if (silent == false) {
    if (exitCode != 0) {
      // Print an error message if the process failed
      error('Command failed with exit code: $exitCode');
    }
  }

  // Return the exit code
  return exitCode;
}