using System.Diagnostics;
using System.Runtime.InteropServices;
namespace ModelContextProtocol.Tests.Utils;
///
/// Helper utilities for Node.js and npm operations.
///
public static class NodeHelpers
{
///
/// Creates a ProcessStartInfo configured to run npx with the specified arguments.
///
/// The arguments to pass to npx.
/// A configured ProcessStartInfo for running npx.
public static ProcessStartInfo NpxStartInfo(string arguments)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// On Windows, npx is a PowerShell script, so we need to use cmd.exe to invoke it
return new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = $"/c npx {arguments}",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
}
else
{
// On Unix-like systems, npx is typically a shell script that can be executed directly
return new ProcessStartInfo
{
FileName = "npx",
Arguments = arguments,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
}
}
///
/// Checks if Node.js and npx are installed and available on the system.
///
/// True if npx is available, false otherwise.
public static bool IsNpxInstalled()
{
try
{
var startInfo = NpxStartInfo("--version");
using var process = Process.Start(startInfo);
if (process == null)
{
return false;
}
process.WaitForExit(5000);
return process.ExitCode == 0;
}
catch
{
return false;
}
}
}