Skip to content

Commit

Permalink
make devtools work
Browse files Browse the repository at this point in the history
  • Loading branch information
Nilsen84 committed Feb 13, 2023
1 parent d584bdf commit 6aba53f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 25 deletions.
21 changes: 0 additions & 21 deletions chrome_debugger.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import (
"fmt"
"github.com/avast/retry-go/v4"
"github.com/gorilla/websocket"
"github.com/phayes/freeport"
"io"
"net/http"
"os/exec"
"time"
)

Expand All @@ -29,25 +27,6 @@ func (d *ChromeDebugger) Send(method string, params map[string]any) error {
})
}

func StartProcessAndConnectDebugger(program string) (*ChromeDebugger, *exec.Cmd, error) {
port, err := freeport.GetFreePort()
if err != nil {
return nil, nil, err
}

cmd := exec.Command(program, fmt.Sprintf("--remote-debugging-port=%d", port))
if err = cmd.Start(); err != nil {
return nil, nil, err
}

d, err := ConnectDebugger(port)
if err != nil {
return nil, cmd, err
}

return d, cmd, nil
}

func ConnectDebugger(port int) (*ChromeDebugger, error) {
url, err := GetWebsocketDebuggerUrl(port)
if err != nil {
Expand Down
20 changes: 16 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
_ "embed"
"errors"
"fmt"
"github.com/phayes/freeport"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
)
Expand Down Expand Up @@ -48,14 +50,25 @@ func Run() (err error) {
return err
}

d, cmd, err := StartProcessAndConnectDebugger(lunarExe)
port, err := freeport.GetFreePort()
if err != nil {
return fmt.Errorf("failed to find free tcp port: %w", err)
}

cmd := exec.Command(lunarExe, fmt.Sprintf("--remote-debugging-port=%d", port))
cmd.Env = append(os.Environ(), "LC_LAUNCHER_DEV_TOOLS=true")
if err = cmd.Start(); err != nil {
return fmt.Errorf("failed to start '%s': %w", lunarExe, err)
}
defer func() {
if cmd != nil && err != nil {
if err != nil {
_ = cmd.Process.Kill()
}
}()

d, err := ConnectDebugger(port)
if err != nil {
return err
return fmt.Errorf("failed to connect debugger: %w", err)
}
defer d.Close()

Expand All @@ -77,7 +90,6 @@ func Run() (err error) {

func main() {
log.SetFlags(0)

if err := Run(); err != nil {
log.Fatalln(err)
}
Expand Down

0 comments on commit 6aba53f

Please sign in to comment.