-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDevServer.cs
More file actions
98 lines (86 loc) · 3.27 KB
/
DevServer.cs
File metadata and controls
98 lines (86 loc) · 3.27 KB
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
using System.Text;
using System.Text.RegularExpressions;
using CliWrap;
using CliWrap.EventStream;
namespace PhotinoSveltekitDevServer;
public class DevServer
{
private const string URL_PREFIX = "http://";
private CancellationTokenSource cancelDevServer;
private Uri devServerUrl;
public DevServer()
{
devServerUrl = null!;
cancelDevServer = new CancellationTokenSource();
}
public async Task Start()
{
// start a separate process with the npm command.
var cmd = Cli.Wrap(ProgramDefaults.DevTerminalExecutable)
.WithWorkingDirectory(ProgramDefaults.DevUserInterfaceDirectory)
.WithArguments("/k" + ProgramDefaults.DevRunUserInterfaceDevMode);
// listen for all incoming events, especially the stdout from the dev server
await foreach (var cmdEvent in cmd.ListenAsync(Encoding.UTF8, cancelDevServer.Token))
{
switch (cmdEvent)
{
case StartedCommandEvent started:
Console.WriteLine($"Process started; ID: {started.ProcessId}");
break;
case StandardOutputCommandEvent stdOut:
Console.WriteLine($"Out> {stdOut.Text}");
if (stdOut.Text.Contains(URL_PREFIX))
{
// if the text contains an url, try to find the url in the message, so that we know
// where we should redirect the user, so that he can visit the dev server.
devServerUrl = DetermineDevServerUrl(stdOut);
}
break;
case StandardErrorCommandEvent stdErr:
Console.WriteLine($"Err> {stdErr.Text}");
break;
case ExitedCommandEvent exited:
Console.WriteLine($"Process exited; Code: {exited.ExitCode}");
break;
}
}
}
public void WaitUntilReady()
{
// wait until the dev server is up and running
while (true)
{
if (IsReady())
{
break;
}
Thread.Sleep(ProgramDefaults.DevWaitUntilReadyTimeSpan);
}
}
public void Stop()
{
cancelDevServer.Cancel();
}
public Uri GetUrl()
{
return devServerUrl;
}
private static Uri DetermineDevServerUrl(StandardOutputCommandEvent stdOut)
{
// this is really hacky!
// since the text contains lots of interesting characters, such as console colorings, arrows etc
// we need to try to extract the url from all that useless stuff.
// This is the best way i came up with after tons of googeling
var withoutUnicodes = Regex.Replace(stdOut.Text, @"[^\x20-\xaf]+", ""); ;
var withoutColorCodes = Regex.Replace(withoutUnicodes, "\\[[0-9]{1,2}m", string.Empty);
withoutColorCodes = withoutColorCodes.Trim();
var httpIndex = withoutColorCodes.IndexOf(URL_PREFIX);
var host = withoutColorCodes.Substring(httpIndex);
return new Uri(host);
}
private bool IsReady()
{
// assume the dev server is ready, once we were able to determine the dev server url.
return devServerUrl != null;
}
}