using System.Diagnostics;

namespace File_Folder_Helper.Helpers;

internal static class HelperNPM
{

    internal static string RunCommand(string commandFileName, string commandToRun, string workingDirectory)
    {
        string result;
        if (!string.IsNullOrEmpty(commandFileName))
            File.WriteAllText(Path.Combine(workingDirectory, commandFileName), commandToRun);
        if (string.IsNullOrEmpty(workingDirectory))
            workingDirectory = Directory.GetDirectoryRoot(Directory.GetCurrentDirectory());
        ProcessStartInfo processStartInfo = new()
        {
            FileName = "cmd",
            RedirectStandardError = true,
            RedirectStandardInput = true,
            RedirectStandardOutput = true,
            WorkingDirectory = workingDirectory
        };
        Process? process = Process.Start(processStartInfo) ?? throw new Exception("Process should not be null.");
        process.StandardInput.WriteLine($"{commandToRun} & exit");
        process.WaitForExit();
        result = $"{process.StandardOutput.ReadToEnd()}{Environment.NewLine}{process.StandardError.ReadToEnd()}{Environment.NewLine}{process.ExitCode}";
        return result;
    }

}