Skip to content

Commit

Permalink
Ask to run as administrator instead of requiring user to manually do it
Browse files Browse the repository at this point in the history
  • Loading branch information
simshaun committed Aug 18, 2024
1 parent 0324947 commit 03f1a22
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
42 changes: 42 additions & 0 deletions NodeSwap/Commands/UseCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Principal;
using DotMake.CommandLine;

namespace NodeSwap.Commands;
Expand Down Expand Up @@ -58,6 +61,13 @@ public int Run()
}
}

if (!IsAdministrator())
{
// Restart the application with elevated privileges
ElevateApplication();
return 1;
}

//
// Replace the symlink
//
Expand Down Expand Up @@ -94,6 +104,38 @@ public int Run()
return 0;
}

private static bool IsAdministrator()
{
using var identity = WindowsIdentity.GetCurrent();
var principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}

private static void ElevateApplication()
{
var currentProcessModule = Process.GetCurrentProcess().MainModule;
if (currentProcessModule == null) throw new Exception("Unable to get the current process module");

var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = currentProcessModule.FileName,
UseShellExecute = true,
Verb = "runas", // Forces the application to run with elevated permissions
Arguments = string.Join(" ", Environment.GetCommandLineArgs().Skip(1)),
},
};

try
{
process.Start();
}
catch (Exception ex)
{
Console.Error.WriteLine("Could not restart as Administrator: " + ex.Message);
}
}

[DllImport("kernel32.dll")]
private static extern bool CreateSymbolicLink(
Expand Down
2 changes: 1 addition & 1 deletion NodeSwap/NodeSwap.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<OutputType>Exe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<Version>1.4.0</Version>
<Version>1.4.1</Version>
<!-- Single file app – https://docs.microsoft.com/en-us/dotnet/core/deploying/single-file -->
<PublishSingleFile>true</PublishSingleFile>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ Download and run the latest installer from the [Releases][3] page.
[3]: https://github.com/simshaun/NodeSwap/releases


## Usage

> ### ⚠ Admin rights:
> **NodeSwap** needs to be ran in an elevated terminal (i.e. Run as Administrator).
> It needs this in order to create symlinks when installing and swapping Node.js versions.
## Usage

Type `nodeswap` in your terminal for help.

Expand All @@ -49,6 +45,12 @@ Type `nodeswap` in your terminal for help.
- `nodeswap uninstall <version>` — The version must be specific like `22.6.0`.
- `nodeswap use <version>` — Switch to a specific version. Must be specific like `22.6.0`.

> ### ⚠ Admin privileges:
> NodeSwap uses a symlink to point your CLI to the active Node.js version.
> On Windows, creating symlinks requires admin privileges.
> When swapping Node.js versions, the `use` command will prompt for administrator
> access if your terminal isn't already elevated.

## How-to

Expand Down

0 comments on commit 03f1a22

Please sign in to comment.