Skip to content

Commit

Permalink
chore: Use GetNpmExecutablePath() on all platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
Dor-bl committed Nov 11, 2023
1 parent 496c849 commit 27988b6
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions test/integration/helpers/Npm.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using OpenQA.Selenium;
using System;
using System;
using System.Diagnostics;
using System.Linq;

Expand All @@ -10,14 +9,7 @@ internal class Npm

public static string GetNpmPrefixPath()
{

string npmPath = "npm";

if (Platform.CurrentPlatform.IsPlatformType(PlatformType.Windows))
{
npmPath = GetNpmExecutablePath();
}

string npmPath = GetNpmExecutablePath();
string npmPrefixPath = RunCommand(npmPath, "-g root");

return npmPrefixPath.Trim();
Expand Down Expand Up @@ -60,21 +52,35 @@ private static string RunCommand(string command, string arguments, int timeoutMi
}
}

private static string GetNpmExecutablePath()
private static string GetNpmExecutablePath()
{
string result = RunCommand("where", "npm");
string commandName = IsWindows() ? "where" : "which";
string result = RunCommand(commandName, "npm");

string npmPath;

string[] lines = result?.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

npmPath = lines?.FirstOrDefault(line => line.EndsWith("npm.cmd"));
if (IsWindows())
{
npmPath = lines?.FirstOrDefault(line => !string.IsNullOrWhiteSpace(line) && line.EndsWith("npm.cmd"));
}
else
{
npmPath = lines?.FirstOrDefault(line => !string.IsNullOrWhiteSpace(line));
}

if (string.IsNullOrWhiteSpace(npmPath))
{
throw new NpmNotFoundException($"NPM executable not found at path: {npmPath}. Please make sure the NPM executable is installed and check the configured path.");
throw new NpmNotFoundException("NPM executable not found. Please make sure the NPM executable is installed and check the configured path.");
}

return npmPath;
}

private static bool IsWindows()
{
return Environment.OSVersion.Platform == PlatformID.Win32NT;
}
}
}

0 comments on commit 27988b6

Please sign in to comment.