forked from wailsapp/wails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdout_scanner.go
48 lines (43 loc) · 1.1 KB
/
stdout_scanner.go
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
package dev
import (
"bufio"
"net/url"
"os"
"strings"
"github.com/acarl005/stripansi"
)
// stdoutScanner acts as a stdout target that will scan the incoming
// data to find out the vite server url
type stdoutScanner struct {
ViteServerURLChan chan string
}
// NewStdoutScanner creates a new stdoutScanner
func NewStdoutScanner() *stdoutScanner {
return &stdoutScanner{
ViteServerURLChan: make(chan string, 2),
}
}
// Write bytes to the scanner. Will copy the bytes to stdout
func (s *stdoutScanner) Write(data []byte) (n int, err error) {
input := stripansi.Strip(string(data))
match := strings.Index(input, "Local:")
if match != -1 {
sc := bufio.NewScanner(strings.NewReader(input))
for sc.Scan() {
line := sc.Text()
index := strings.Index(line, "Local:")
if index == -1 || len(line) < 7 {
continue
}
viteServerURL := strings.TrimSpace(line[index+6:])
LogGreen("Vite Server URL: %s", viteServerURL)
_, err := url.Parse(viteServerURL)
if err != nil {
LogRed(err.Error())
} else {
s.ViteServerURLChan <- viteServerURL
}
}
}
return os.Stdout.Write(data)
}