-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
131 lines (109 loc) · 4.64 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.Diagnostics;
using System.ComponentModel;
using System.Xml.Linq;
using System.Windows.Forms;
using System.IO;
using System.Threading;
namespace StartNode
{
class Program
{
static void Main(string[] args)
{
string environment, nodePath, appDir, appStart, appPath;
bool min, appFound, allowOverrides;
string[] defaultApps;
try
{
XElement config = XElement.Load(@"config.xml");
environment = config.Element("mode").Value;
nodePath = config.Element("nodepath").Value;
appDir = config.Element("appdir").Value;
appStart = config.Element("appstart").Value;
min = Convert.ToBoolean(config.Element("minimize").Value);
allowOverrides = Convert.ToBoolean(config.Element("allowoverrides").Value);
defaultApps = config.Element("defaultapps").Value.Split(',');
appFound = true;
// if overrides allowed try current directory if can't find specified.
if (allowOverrides && !Directory.Exists(appDir))
{
Console.WriteLine("Directory not found, overriding to current.");
appDir = Environment.CurrentDirectory.ToString();
}
if (!File.Exists(appDir + @"\" + appStart))
{
appFound = false;
if (allowOverrides)
{
Console.WriteLine("Application not found, checking overrides.");
foreach (var s in defaultApps)
{
if (File.Exists(appDir + @"\" + s))
{
appFound = true;
appStart = s;
Console.WriteLine("Application found at " + appDir + @"\" + s);
break;
}
}
}
}
// set the application startup path.
appPath = appDir + @"\" + appStart;
if (!appFound)
{
MessageBox.Show("Unable to locate Application path located at " + appPath, "Error - Path Not Found", MessageBoxButtons.OK);
Environment.Exit(0);
}
if (!File.Exists(nodePath))
{
MessageBox.Show("Unable to locate Node executable located at " + nodePath, "Error - Node Not Found", MessageBoxButtons.OK);
Environment.Exit(0);
}
Launch(environment, nodePath, appPath, min);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error - Configuration Failure", MessageBoxButtons.OK);
}
}
static void Launch(string env, string nodePath, string appDir, bool min)
{
var startInfo = new ProcessStartInfo();
startInfo.FileName = nodePath;
startInfo.Arguments = appDir;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = false;
if(min)
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
if(!string.IsNullOrEmpty(env))
startInfo.EnvironmentVariables.Add("NODE_ENV", env);
Console.WriteLine("Starting Node...");
Console.WriteLine(Environment.NewLine);
try
{
using (var p = Process.Start(startInfo))
{
p.Exited += UnhandledProcessException;
p.WaitForExit();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error - Process Start Failure", MessageBoxButtons.OK);
}
}
static void UnhandledProcessException(object sender, EventArgs e)
{
var p = (Process)sender;
if (p != null)
Console.WriteLine("Node.exe exited with code: {0} ", p.ExitCode);
else
Console.WriteLine("Node.exe has exited.");
Console.WriteLine("Press Enter to exit...");
Console.ReadLine();
Environment.Exit(1);
}
}
}