shadow/utils/vite_dev.go

173 lines
2.9 KiB
Go

package utils
import (
"bufio"
"fmt"
"github.com/spf13/viper"
"io"
"log"
"os"
"os/exec"
"strings"
"sync"
)
//func DevWatcher(engine *html.Engine) {
//
// watcher, err := fsnotify.NewWatcher()
// if err != nil {
// log.Fatalln(err)
// }
// defer watcher.Close()
//
// err = watcher.Add("./src")
// if err != nil {
// log.Fatalln(err)
// }
//
// if err := filepath.Walk("./src", func(path string, info fs.FileInfo, err error) error {
//
// if err != nil {
// return err
// }
//
// if info.IsDir() {
// err = watcher.Add(path)
// if err != nil {
// return err
// }
// }
//
// return nil
// }); err != nil {
// log.Fatalln(err)
// }
//
// err = watcher.Add("./dist/.vite/manifest.json")
// if err != nil {
// log.Fatalln(err)
// }
//
// for {
// select {
// case event, ok := <-watcher.Events:
// if !ok {
// return
// }
//
// fmt.Println(event)
//
// if event.Op&fsnotify.Write == fsnotify.Write {
//
// if filepath.Ext(event.Name) == ".gohtml" {
//
// fmt.Println("Ayo, I updated")
// engine = html.New("./src", ".gohtml")
//
// } else if strings.Contains(event.Name, "manifest.json") {
//
// engine = html.New("./src", ".gohtml")
//
// }
//
// }
// case err, ok := <-watcher.Errors:
// if !ok {
// return
// }
// log.Println("Error:", err)
// }
//
// }
//
//}
var blueRGB = [3]int{71, 202, 255}
var purpleRGB = [3]int{189, 52, 254}
func RunViteServer() {
if viper.GetBool("dev") {
firstBuilt := false
var niceText strings.Builder
str := "Vite Dev"
steps := len(str) - 1
// Why do this? Idk. I like having separation between things on the console.
for i := 0; i < len(str); i++ {
char := str[i]
r := (blueRGB[0]*(steps-i) + purpleRGB[0]*i) / steps
g := (blueRGB[1]*(steps-i) + purpleRGB[1]*i) / steps
b := (blueRGB[2]*(steps-i) + purpleRGB[2]*i) / steps
if _, err := niceText.WriteString(fmt.Sprintf("\033[1m\033[38;2;%d;%d;%dm%c\033[0m", r, g, b, char)); err != nil {
log.Fatalln(err)
}
}
go func() {
cmd := exec.Command("bun", "run", "dev")
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatalln(err)
}
defer stdout.Close()
stderr, err := cmd.StderrPipe()
if err != nil {
log.Fatalln(err)
}
defer stderr.Close()
if err := cmd.Start(); err != nil {
log.Fatalln(err)
}
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
io.Copy(os.Stdout, stderr)
}()
go func() {
defer wg.Done()
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
output := scanner.Text()
if strings.Contains(output, "built in") && !firstBuilt {
firstBuilt = true
go Webserver()
}
fmt.Println(niceText.String() + " \033[1m\033[38;5;244m|\033[22m " + output + "\033[0m")
}
}()
err = cmd.Wait()
wg.Wait()
if cmd.ProcessState.ExitCode() != 0 {
fmt.Println("Command failed with non-zero exit status")
return
}
}()
}
}