runWithResult function
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;
}
}