-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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 <[email protected]>
- Loading branch information
1 parent
f55b781
commit 2255af1
Showing
4 changed files
with
59 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters