5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-04 07:29:56 +08:00
wails/v3/internal/commands/dev.go
Lea Anthony b4a61e11fa
Add devmode config to config.yml.
Remove devmode.config.yaml.
Update Watcher command.
Update templates.
2024-11-09 09:35:13 +11: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/config.yml"`
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,
})
}