runWithResult function

Future<ProcessResult> runWithResult(
  1. String cmd,
  2. List<String> args
)

Runs a cmd with args and returns the ProcessResult.

Capture stdout/stderr and prints them to the console. Throws an Exception if the exit code is non-zero.

Implementation

Future<ProcessResult> runWithResult(String cmd, List<String> args) async {
  print('šŸ‘‰ Running: $cmd ${args.join(" ")}\n');

  final env = getInjectedEnvironment();

  try {
    final result = await Process.run(
      cmd,
      args,
      runInShell: true,
      environment: env,
    );

    /// āœ… STDOUT
    if (result.stdout.toString().isNotEmpty) {
      stdout.write(result.stdout);
    }

    /// āŒ STDERR
    if (result.stderr.toString().isNotEmpty) {
      stderr.write(result.stderr);
    }

    /// āŒ Exit Code Check
    if (result.exitCode != 0) {
      throw Exception(
        '$cmd failed with exit code ${result.exitCode}',
      );
    }

    print('\nāœ… $cmd completed\n');
    return result;
  } catch (e) {
    print('\nāŒ Error while running $cmd');
    rethrow;
  }
}