5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-04 20:29:37 +08:00
wails/v3/internal/commands/dev.go
2024-07-29 20:46:44 +10:00

59 lines
1.3 KiB
Go

package commands
import (
"fmt"
"net"
"os"
"strconv"
"github.com/wailsapp/wails/v3/internal/flags"
)
const defaultVitePort = 9245
const wailsVitePort = "WAILS_VITE_PORT"
type DevOptions struct {
flags.Common
Config string `description:"The config file including path" default:"./build/devmode.config.yaml"`
VitePort int `name:"port" description:"Specify the vite dev server port"`
Secure bool `name:"s" description:"Enable HTTPS"`
}
func Dev(options *DevOptions) error {
host := "localhost"
// flag takes precedence over environment variable
var port int
if options.VitePort != 0 {
port = options.VitePort
} else if p, err := strconv.Atoi(os.Getenv(wailsVitePort)); err == nil {
port = p
} else {
port = defaultVitePort
}
// check if port is already in use
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", host, port))
if err != nil {
return err
}
if err = l.Close(); err != nil {
return err
}
// Set environment variable for the dev:frontend task
os.Setenv(wailsVitePort, strconv.Itoa(port))
// Set url of frontend dev server
if options.Secure {
os.Setenv("FRONTEND_DEVSERVER_URL", fmt.Sprintf("https://%s:%d", host, port))
} else {
os.Setenv("FRONTEND_DEVSERVER_URL", fmt.Sprintf("http://%s:%d", host, port))
}
return Watcher(&WatcherOptions{
Config: options.Config,
})
}