5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-04 13:41:50 +08:00

[v3] Add port flag to dev command (#3429)

* Add `port` flag to dev command, ...

Add support for environment variable WAILS_VITE_PORT

* Check if port is already in use

* Update changelog

---------

Co-authored-by: Lea Anthony <lea.anthony@gmail.com>
This commit is contained in:
abichinger 2024-05-02 13:48:50 +02:00 committed by GitHub
parent f55b781d86
commit 2255af1c45
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 59 additions and 10 deletions

View File

@ -32,6 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add ESM exports from the bundled JS runtime script by [@fbbdev](https://github.com/fbbdev) in [#3295](https://github.com/wailsapp/wails/pull/3295) - Add ESM exports from the bundled JS runtime script by [@fbbdev](https://github.com/fbbdev) in [#3295](https://github.com/wailsapp/wails/pull/3295)
- Add binding generator flag for using the bundled JS runtime script instead of the npm package by [@fbbdev](https://github.com/fbbdev) in [#3334](https://github.com/wailsapp/wails/pull/3334) - Add binding generator flag for using the bundled JS runtime script instead of the npm package by [@fbbdev](https://github.com/fbbdev) in [#3334](https://github.com/wailsapp/wails/pull/3334)
- Implement `setIcon` on linux by [@abichinger](https://github.com/abichinger) in [#3354](https://github.com/wailsapp/wails/pull/3354) - Implement `setIcon` on linux by [@abichinger](https://github.com/abichinger) in [#3354](https://github.com/wailsapp/wails/pull/3354)
- Add flag `-port` to dev command and support environment variable `WAILS_VITE_PORT` by [@abichinger](https://github.com/abichinger) in [#3429](https://github.com/wailsapp/wails/pull/3429)
- Add tests for bound method calls by [@abichinger](https://github.com/abichinger) in [#3431](https://github.com/wailsapp/wails/pull/3431) - Add tests for bound method calls by [@abichinger](https://github.com/abichinger) in [#3431](https://github.com/wailsapp/wails/pull/3431)
### Fixed ### Fixed

View File

@ -0,0 +1,53 @@
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.toml"`
VitePort int `name:"port" description:"Specify the vite dev server port"`
}
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
os.Setenv("FRONTEND_DEVSERVER_URL", fmt.Sprintf("http://%s:%d", host, port))
return Watcher(&WatcherOptions{
Config: options.Config,
})
}

View File

@ -1,19 +1,16 @@
package commands package commands
import ( import (
"os"
"github.com/pterm/pterm" "github.com/pterm/pterm"
"github.com/wailsapp/wails/v3/internal/flags" "github.com/wailsapp/wails/v3/internal/flags"
"os"
) )
func Build(_ *flags.Build) error { func Build(_ *flags.Build) error {
return wrapTask("build") return wrapTask("build")
} }
func Dev(_ *flags.Dev) error {
return wrapTask("dev")
}
func Package(_ *flags.Package) error { func Package(_ *flags.Package) error {
return wrapTask("package") return wrapTask("package")
} }

View File

@ -3,6 +3,7 @@ version: '3'
vars: vars:
APP_NAME: "{{.ProjectName}}" APP_NAME: "{{.ProjectName}}"
BIN_DIR: "bin" BIN_DIR: "bin"
VITE_PORT: {{ "'{{.WAILS_VITE_PORT | default 9245}}'" }}
tasks: tasks:
@ -431,15 +432,12 @@ tasks:
deps: deps:
- task: install:frontend:deps - task: install:frontend:deps
cmds: cmds:
- npm run dev - npm run dev -- --port {{ "{{.VITE_PORT}}" }} --strictPort
dev: dev:
summary: Runs the application in development mode summary: Runs the application in development mode
cmds: cmds:
- wails3 tool watcher -config ./build/devmode.config.toml - wails3 dev -config ./build/devmode.config.toml -port {{ "{{.VITE_PORT}}" }}
env:
# This is the default vite dev server port
FRONTEND_DEVSERVER_URL: 'http://localhost:5173'
dev:reload: dev:reload:
summary: Reloads the application summary: Reloads the application