/// <summary> /// Initializes the Node environment. /// </summary> public static async Task EnsureNodeFolderCreated(bool callSync = false) { using (await _mutex.Lock(callSync)) { var node_modules = Path.Combine(ExecutionPath, "node_modules"); var log_file = Path.Combine(ExecutionPath, "log.txt"); if (!Directory.Exists(node_modules) || !File.Exists(log_file) || (Directory.Exists(node_modules) && Directory.GetDirectories(node_modules).Length < 32)) { // This is called async at startup. If we do a build before it's completed it gets // called sync but the mutex will block the call until the previous async call has // completed, so we shouldn't get here: we should be set up correctly. // If we do a build after someone's deleted a file, then we'll fail here. if (callSync) { throw new Exception("Node set up not valid on sync call, TSLint not run"); } if (Directory.Exists(ExecutionPath)) { Directory.Delete(ExecutionPath, recursive: true); } Directory.CreateDirectory(ExecutionPath); var tasks = new List <Task> { SaveResourceFileAsync(ExecutionPath, "WebLinter.Node.node_modules.7z", "node_modules.7z"), SaveResourceFileAsync(ExecutionPath, "WebLinter.Node.7z.exe", "7z.exe"), SaveResourceFileAsync(ExecutionPath, "WebLinter.Node.7z.dll", "7z.dll"), SaveResourceFileAsync(ExecutionPath, "WebLinter.Node.prepare.cmd", "prepare.cmd"), SaveResourceFileAsync(ExecutionPath, "WebLinter.Node.server.js", "server.js"), SaveResourceFileAsync(ExecutionPath, "WebLinter.Node.node.7z", "node.7z"), }; await Task.WhenAll(tasks.ToArray()); ProcessStartInfo start = new ProcessStartInfo { WorkingDirectory = ExecutionPath, CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden, FileName = "cmd.exe", Arguments = "/c prepare.cmd" }; Process p = Process.Start(start); await p.WaitForExitAsync(); // If this file is written, then the initialization was successful. using (var writer = new StreamWriter(log_file)) { await writer.WriteAsync(DateTime.Now.ToLongDateString()); } } } }
private async Task EnsureNodeProcessIsRunning(bool callSync) { using (await _mutex.Lock(callSync)) { if (_process != null && !_process.HasExited) { return; } //if (callSync) throw new Exception("Unable to lint: webserver not correctly initialized"); try { Down(); SelectAvailablePort(); ProcessStartInfo start = new ProcessStartInfo(Path.Combine(ExecutionPath, "node.exe")) { WindowStyle = ProcessWindowStyle.Hidden, Arguments = $"\"{Path.Combine(ExecutionPath, "server.js")}\" {BasePort}", UseShellExecute = false, CreateNoWindow = true }; #if DEBUG start.WindowStyle = ProcessWindowStyle.Normal; start.CreateNoWindow = false; start.Arguments = "--inspect " + start.Arguments; #endif _process = Process.Start(start); // Give the node server some time to initialize if (callSync) { System.Threading.Thread.Sleep(100); } else { await Task.Delay(100); } if (_process.HasExited) { throw new Exception($"TsLint server failed to start: {start.FileName} {start.Arguments}"); } } catch (Exception) { Down(); throw; } } }