using System.Diagnostics;
namespace SilverOS.Welcome.Core.Apply;

public sealed class ProcessRunner : IProcessRunner
{
    public async Task<ProcessResult> RunAsync(string file, string args, CancellationToken ct = default)
    {
        using var p = new Process { StartInfo = new ProcessStartInfo(file, args)
        {
            RedirectStandardOutput = true, RedirectStandardError = true,
            UseShellExecute = false, CreateNoWindow = true
        }};
        p.Start();
        var outT = p.StandardOutput.ReadToEndAsync(ct);
        var errT = p.StandardError.ReadToEndAsync(ct);
        await p.WaitForExitAsync(ct);
        return new ProcessResult(p.ExitCode, await outT, await errT);
    }
}
