Skip to content

Commit 0b7b04b

Browse files
committed
chore: Remove Node.js runtime dependencies
1 parent a135335 commit 0b7b04b

File tree

7 files changed

+104
-4
lines changed

7 files changed

+104
-4
lines changed

.github/workflows/release.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ jobs:
1616

1717
- name: dotnet publish
1818
run: |
19-
dotnet publish src/docfx -f net8.0 -c Release /p:Version=${GITHUB_REF_NAME#v} --self-contained -r win-x64 -o drop/publish/win-x64 /p:PlaywrightPlatform=win
20-
dotnet publish src/docfx -f net8.0 -c Release /p:Version=${GITHUB_REF_NAME#v} --self-contained -r linux-x64 -o drop/publish/linux-x64 /p:PlaywrightPlatform=linux
21-
dotnet publish src/docfx -f net8.0 -c Release /p:Version=${GITHUB_REF_NAME#v} --self-contained -r osx-x64 -o drop/publish/osx-x64 /p:PlaywrightPlatform=osx
19+
dotnet publish src/docfx -f net8.0 -c Release /p:Version=${GITHUB_REF_NAME#v} --self-contained -r win-x64 -o drop/publish/win-x64
20+
dotnet publish src/docfx -f net8.0 -c Release /p:Version=${GITHUB_REF_NAME#v} --self-contained -r linux-x64 -o drop/publish/linux-x64
21+
dotnet publish src/docfx -f net8.0 -c Release /p:Version=${GITHUB_REF_NAME#v} --self-contained -r osx-x64 -o drop/publish/osx-x64
2222
mkdir -p drop/bin
2323
2424
- run: zip -r ../../bin/docfx-win-x64-${GITHUB_REF_NAME}.zip .

Directory.Build.props

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,12 @@
5959
</PackageReference>
6060
</ItemGroup>
6161

62+
63+
<!-- Remove Node.js runtime dependencies that used by playwright -->
64+
<Target Name="RemoveNodeJsRuntimes" AfterTargets="CopyPlaywrightFilesToOutput">
65+
<ItemGroup>
66+
<Content Remove="@(Content)" Condition="$([System.String]::Copy('%(Content.PlaywrightFolder)').Contains('node\'))" />
67+
</ItemGroup>
68+
</Target>
69+
6270
</Project>

docs/reference/docfx-environment-variables-reference.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,9 @@ This setting is intended to be used on offline environment.
2626

2727
## `DOCFX_PDF_TIMEOUT`
2828

29-
Maximum time in milliseconds to override the default [Playwright timeout](https://playwright.dev/docs/api/class-browsercontext#browser-context-set-default-timeout) for PDF generation.
29+
Maximum time in milliseconds to override the default [Playwright timeout](https://playwright.dev/docs/api/class-browsercontext#browser-context-set-default-timeout) for PDF generation.
30+
31+
## `PLAYWRIGHT_NODEJS_PATH`
32+
33+
Custom Node.js executable path that will be used by the `docfx pdf' command.
34+
By default, docfx automatically detect installed Node.js from `PATH`.

src/Docfx.App/Docfx.App.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<ItemGroup>
1414
<InternalsVisibleTo Include="docfx" />
1515
<InternalsVisibleTo Include="docfx.Tests" />
16+
<InternalsVisibleTo Include="docfx.Snapshot.Tests" />
1617
</ItemGroup>
1718

1819
<ItemGroup>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
}

src/Docfx.App/PdfBuilder.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ class Outline
4747
public string? pdfFooterTemplate { get; init; }
4848
}
4949

50+
static PdfBuilder()
51+
{
52+
PlaywrightHelper.EnsurePlaywrightNodeJsPath();
53+
}
54+
5055
public static Task Run(BuildJsonConfig config, string configDirectory, string? outputDirectory = null)
5156
{
5257
var outputFolder = Path.GetFullPath(Path.Combine(

test/docfx.Snapshot.Tests/PercyTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public PercyFactAttribute()
3838

3939
static PercyTest()
4040
{
41+
PlaywrightHelper.EnsurePlaywrightNodeJsPath();
4142
Microsoft.Playwright.Program.Main(["install", "chromium"]);
4243
}
4344

0 commit comments

Comments
 (0)