|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Runtime.InteropServices; |
| 5 | +using Docfx.Common; |
| 6 | +using Docfx.Exceptions; |
| 7 | + |
| 8 | +#nullable enable |
| 9 | + |
| 10 | +namespace Docfx; |
| 11 | + |
| 12 | +internal static class PlaywrightHelper |
| 13 | +{ |
| 14 | + public static void EnsurePlaywrightNodeJsPath() |
| 15 | + { |
| 16 | + // Skip if playwright environment variable exists. |
| 17 | + if (Environment.GetEnvironmentVariable("PLAYWRIGHT_DRIVER_SEARCH_PATH") != null) |
| 18 | + return; |
| 19 | + |
| 20 | + if (Environment.GetEnvironmentVariable("PLAYWRIGHT_NODEJS_PATH") != null) |
| 21 | + return; |
| 22 | + |
| 23 | + if (!TryFindNodeExecutable(out var exePath, out var nodeVersion)) |
| 24 | + { |
| 25 | + throw new DocfxException("Node.js executable is not found. Try to install Node.js or set the `PLAYWRIGHT_NODEJS_PATH` environment variable."); |
| 26 | + } |
| 27 | + |
| 28 | + Logger.LogInfo($"Using custom Node.js {nodeVersion} executable."); |
| 29 | + Logger.LogVerbose($"Path: {exePath}"); |
| 30 | + |
| 31 | + Environment.SetEnvironmentVariable("PLAYWRIGHT_NODEJS_PATH", exePath, EnvironmentVariableTarget.Process); |
| 32 | + } |
| 33 | + |
| 34 | + private static bool TryFindNodeExecutable(out string exePath, out string nodeVersion) |
| 35 | + { |
| 36 | + // Find Node.js executable installation path from PATHs. |
| 37 | + string exeName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "node.exe" : "node"; |
| 38 | + |
| 39 | + var pathEnv = Environment.GetEnvironmentVariable("PATH"); |
| 40 | + if (pathEnv == null) |
| 41 | + throw new DocfxException("Failed to get `PATH` environment variable."); |
| 42 | + |
| 43 | + var paths = pathEnv.Split(Path.PathSeparator); |
| 44 | + foreach (var path in paths) |
| 45 | + { |
| 46 | + string fullPath = Path.GetFullPath(Path.Combine(path, exeName)); |
| 47 | + |
| 48 | + if (File.Exists(fullPath)) |
| 49 | + { |
| 50 | + exePath = fullPath; |
| 51 | + nodeVersion = GetNodeVersion(exePath); |
| 52 | + return true; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + exePath = ""; |
| 57 | + nodeVersion = ""; |
| 58 | + return false; |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Returns `node --version` command result |
| 63 | + /// </summary> |
| 64 | + private static string GetNodeVersion(string exePath) |
| 65 | + { |
| 66 | + using var memoryStream = new MemoryStream(); |
| 67 | + using var stdoutWriter = new StreamWriter(memoryStream); |
| 68 | + |
| 69 | + var exitCode = CommandUtility.RunCommand(new CommandInfo { Name = exePath, Arguments = "--version" }, stdoutWriter); |
| 70 | + |
| 71 | + if (exitCode != 0) |
| 72 | + return ""; |
| 73 | + |
| 74 | + stdoutWriter.Flush(); |
| 75 | + memoryStream.Position = 0; |
| 76 | + |
| 77 | + using var streamReader = new StreamReader(memoryStream); |
| 78 | + return streamReader.ReadLine() ?? ""; |
| 79 | + } |
| 80 | +} |
0 commit comments