Skip to content

Commit

Permalink
Disable npm update notification (#2561)
Browse files Browse the repository at this point in the history
* Disable `npm` update notification

* De-duplicate code
  • Loading branch information
ITaluone committed Jan 15, 2024
1 parent 3ce7286 commit fc4ab4b
Showing 1 changed file with 34 additions and 16 deletions.
50 changes: 34 additions & 16 deletions Build/CustomNpmTasks.cs
Expand Up @@ -16,7 +16,7 @@ public static class CustomNpmTasks
static AbsolutePath NodeDirPerOs;
static AbsolutePath WorkingDirectory;

static IReadOnlyDictionary<string, string> NpmEnvironmentVariables = null;
static IReadOnlyDictionary<string, string> NpmEnvironmentVariables;

static Tool Npm;

Expand Down Expand Up @@ -48,24 +48,28 @@ static AbsolutePath DownloadNodeArchive()

if (EnvironmentInfo.IsWin)
{
os = "win-x64";
os = "win";
archiveType = ".zip";
}
else if (EnvironmentInfo.IsOsx)
{
os = "darwin-x64";
os = "darwin";
archiveType = ".tar.gz";
}
else if (EnvironmentInfo.IsLinux)
{
os = "linux-x64";
os = "linux";
archiveType = ".tar.xz";
}

os.NotNull();
archiveType.NotNull();

os = EnvironmentInfo.IsArm64 ? os!.Replace("x64", "arm64") : os;
string architecture =
EnvironmentInfo.IsArm64 ? "arm64" :
EnvironmentInfo.Is64Bit ? "x64" : "x86";

os = $"{os}-{architecture}";

if (!HasCachedNodeModules)
{
Expand Down Expand Up @@ -119,20 +123,19 @@ static void ExtractNodeArchive(this AbsolutePath archive)

static void LinkTools()
{
AbsolutePath npmExecutable;

if (EnvironmentInfo.IsWin)
{
Information("Resolve tool npm...");
Npm = ToolResolver.GetTool(NodeDirPerOs / "npm.cmd");
NpmVersion();
npmExecutable = NodeDirPerOs / "npm.cmd";
}
else
{
WorkingDirectory /= "bin";

var nodeExecutable = WorkingDirectory / "node";
var npmNodeModules = NodeDirPerOs / "lib" / "node_modules";
var npmExecutable = npmNodeModules / "npm" / "bin" / "npm";
var npmSymlink = WorkingDirectory / "npm";
npmExecutable = npmNodeModules / "npm" / "bin" / "npm";

Information($"Set execution permissions for {nodeExecutable}...");
nodeExecutable.SetExecutable();
Expand All @@ -145,19 +148,24 @@ static void LinkTools()
ln($"-sf {npmExecutable} npm", workingDirectory: WorkingDirectory);
ln($"-sf {npmNodeModules} node_modules", workingDirectory: WorkingDirectory);

Information("Resolve tool npm...");
Npm = ToolResolver.GetTool(npmSymlink);
NpmVersion();
npmExecutable = WorkingDirectory / "npm";
}

Information("Resolve tool npm...");
npmExecutable.NotNull();
Npm = ToolResolver.GetTool(npmExecutable);

NpmConfig("set update-notifier false");
NpmVersion();

SetEnvVars();
}

static void SetEnvVars()
{
NpmEnvironmentVariables = EnvironmentInfo.Variables
.ToDictionary(x => x.Key, x => x.Value)
.SetKeyValue("path", WorkingDirectory).AsReadOnly();
.ToDictionary(x => x.Key, x => x.Value)
.SetKeyValue("path", WorkingDirectory).AsReadOnly();
}

public static void NpmInstall(bool silent = false, string workingDirectory = null)
Expand All @@ -183,7 +191,17 @@ static void NpmVersion()
environmentVariables: NpmEnvironmentVariables);
}

static Action<OutputType, string> NpmLogger = (outputType, msg) =>
static void NpmConfig(string args)
{
Npm($"config {args}".TrimMatchingDoubleQuotes(),
workingDirectory: WorkingDirectory,
logInvocation: false,
logOutput: false,
logger: NpmLogger,
environmentVariables: NpmEnvironmentVariables);
}

static readonly Action<OutputType, string> NpmLogger = (outputType, msg) =>
{
if (EnvironmentInfo.IsWsl && msg.Contains("wslpath"))
return;
Expand Down

0 comments on commit fc4ab4b

Please sign in to comment.