runWithResult function
Runs a cmd with args and returns the ProcessResult.
Capture stdout/stderr and prints them to the console unless silent is true.
Throws an Exception if the exit code is non-zero.
Implementation
Future<ProcessResult> runWithResult(
String cmd,
List<String> args, {
bool silent = false,
}) async {
if (!silent) print('š Running: $cmd ${args.join(" ")}\n');
final env = getInjectedEnvironment();
try {
final result = await Process.run(
cmd,
args,
runInShell: true,
environment: env,
);
if (!silent) {
/// ā
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) {
final errorMsg = '$cmd failed with exit code ${result.exitCode}';
if (!silent) print('\nā $errorMsg');
throw Exception(errorMsg);
}
if (!silent) print('\nā
$cmd completed\n');
return result;
} catch (e) {
if (!silent) print('\nā Error while running $cmd: $e');
rethrow;
}
}