|
1 | 1 | using System; |
2 | | -using System.Diagnostics; |
3 | 2 | using System.IO; |
4 | | -using System.Linq; |
5 | 3 | using System.Runtime.InteropServices; |
6 | | -using System.Security.Principal; |
7 | 4 | using DotMake.CommandLine; |
| 5 | +using NodeSwap.Utils; |
8 | 6 |
|
9 | 7 | namespace NodeSwap.Commands; |
10 | 8 |
|
@@ -61,10 +59,20 @@ public int Run() |
61 | 59 | } |
62 | 60 | } |
63 | 61 |
|
64 | | - if (!IsAdministrator()) |
| 62 | + if (!ProcessElevation.IsAdministrator()) |
65 | 63 | { |
66 | 64 | // Restart the application with elevated privileges |
67 | | - return ElevateApplication(); |
| 65 | + return ProcessElevation.ElevateApplication(); |
| 66 | + } |
| 67 | + |
| 68 | + // |
| 69 | + // Track the previous NodeJS version used |
| 70 | + // |
| 71 | + |
| 72 | + var activeVersion = nodeLocal.GetActiveVersion(); |
| 73 | + if (activeVersion != null) |
| 74 | + { |
| 75 | + File.WriteAllText(globalContext.PreviousVersionTrackerFilePath, activeVersion.ToString()); |
68 | 76 | } |
69 | 77 |
|
70 | 78 | // |
@@ -103,42 +111,6 @@ public int Run() |
103 | 111 | return 0; |
104 | 112 | } |
105 | 113 |
|
106 | | - private static bool IsAdministrator() |
107 | | - { |
108 | | - using var identity = WindowsIdentity.GetCurrent(); |
109 | | - var principal = new WindowsPrincipal(identity); |
110 | | - return principal.IsInRole(WindowsBuiltInRole.Administrator); |
111 | | - } |
112 | | - |
113 | | - private static int ElevateApplication() |
114 | | - { |
115 | | - var currentProcessModule = Process.GetCurrentProcess().MainModule; |
116 | | - if (currentProcessModule == null) throw new Exception("Unable to get the current process module"); |
117 | | - |
118 | | - var process = new Process |
119 | | - { |
120 | | - StartInfo = new ProcessStartInfo |
121 | | - { |
122 | | - FileName = currentProcessModule.FileName, |
123 | | - UseShellExecute = true, |
124 | | - Verb = "runas", // Forces the application to run with elevated permissions |
125 | | - Arguments = string.Join(" ", Environment.GetCommandLineArgs().Skip(1)), |
126 | | - }, |
127 | | - }; |
128 | | - |
129 | | - try |
130 | | - { |
131 | | - process.Start(); |
132 | | - process.WaitForExit(); |
133 | | - return process.ExitCode; |
134 | | - } |
135 | | - catch (Exception ex) |
136 | | - { |
137 | | - Console.Error.WriteLine("Could not restart as Administrator: " + ex.Message); |
138 | | - return 1; |
139 | | - } |
140 | | - } |
141 | | - |
142 | 114 | [DllImport("kernel32.dll")] |
143 | 115 | private static extern bool CreateSymbolicLink( |
144 | 116 | string lpSymlinkFileName, |
|
0 commit comments