From 2ebe71d8ec07eff9d2c80813b2ef3a9f39692458 Mon Sep 17 00:00:00 2001 From: Guy Goldenberg Date: Thu, 12 Sep 2024 20:05:24 +0300 Subject: [PATCH 1/5] Use playwright cache env var as driver directory --- run.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/run.go b/run.go index aad111f..8213da3 100644 --- a/run.go +++ b/run.go @@ -52,6 +52,10 @@ func NewDriver(options *RunOptions) (*PlaywrightDriver, error) { } func getDefaultCacheDirectory() (string, error) { + if cachePath := os.Getenv("PLAYWRIGHT_BROWSERS_PATH"); cachePath != "" { + return cachePath + } + userHomeDir, err := os.UserHomeDir() if err != nil { return "", fmt.Errorf("could not get user home directory: %w", err) From c14f44d949fb200554f8c2ab994e9b46c983a5f9 Mon Sep 17 00:00:00 2001 From: Guy Goldenberg Date: Thu, 12 Sep 2024 20:12:23 +0300 Subject: [PATCH 2/5] Fix `return` --- run.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/run.go b/run.go index 8213da3..5321d03 100644 --- a/run.go +++ b/run.go @@ -53,7 +53,7 @@ func NewDriver(options *RunOptions) (*PlaywrightDriver, error) { func getDefaultCacheDirectory() (string, error) { if cachePath := os.Getenv("PLAYWRIGHT_BROWSERS_PATH"); cachePath != "" { - return cachePath + return cachePath, nil } userHomeDir, err := os.UserHomeDir() From b792be0f21b5a14b1024811d7c76a1e964c8e1d5 Mon Sep 17 00:00:00 2001 From: Can Stand <70889873+canstand@users.noreply.github.com> Date: Fri, 13 Sep 2024 23:08:14 +0800 Subject: [PATCH 3/5] feat: add PLAYWRIGHT_DRIVER_PATH env to specify the driver directory --- run.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/run.go b/run.go index 5321d03..6f192f2 100644 --- a/run.go +++ b/run.go @@ -37,6 +37,9 @@ type PlaywrightDriver struct { func NewDriver(options *RunOptions) (*PlaywrightDriver, error) { baseDriverDirectory := options.DriverDirectory + if baseDriverDirectory == "" { + baseDriverDirectory := os.Getenv("PLAYWRIGHT_DRIVER_PATH") + } if baseDriverDirectory == "" { var err error baseDriverDirectory, err = getDefaultCacheDirectory() @@ -52,10 +55,6 @@ func NewDriver(options *RunOptions) (*PlaywrightDriver, error) { } func getDefaultCacheDirectory() (string, error) { - if cachePath := os.Getenv("PLAYWRIGHT_BROWSERS_PATH"); cachePath != "" { - return cachePath, nil - } - userHomeDir, err := os.UserHomeDir() if err != nil { return "", fmt.Errorf("could not get user home directory: %w", err) From d6420208d467294d07d12637a4327cab2c909e2c Mon Sep 17 00:00:00 2001 From: Can Stand <70889873+canstand@users.noreply.github.com> Date: Fri, 13 Sep 2024 23:11:27 +0800 Subject: [PATCH 4/5] fix: redundant colon --- run.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/run.go b/run.go index 6f192f2..d4540ff 100644 --- a/run.go +++ b/run.go @@ -38,7 +38,7 @@ type PlaywrightDriver struct { func NewDriver(options *RunOptions) (*PlaywrightDriver, error) { baseDriverDirectory := options.DriverDirectory if baseDriverDirectory == "" { - baseDriverDirectory := os.Getenv("PLAYWRIGHT_DRIVER_PATH") + baseDriverDirectory = os.Getenv("PLAYWRIGHT_DRIVER_PATH") } if baseDriverDirectory == "" { var err error From e543dabf26b91ce32a1f45d663b96704e1482b6f Mon Sep 17 00:00:00 2001 From: Can Stand <70889873+canstand@users.noreply.github.com> Date: Tue, 17 Sep 2024 00:42:33 +0800 Subject: [PATCH 5/5] refactor: transform RunOptions with deafult values and add comments --- run.go | 55 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/run.go b/run.go index d4540ff..9e9fea1 100644 --- a/run.go +++ b/run.go @@ -30,26 +30,22 @@ var ( } ) +// PlaywrightDriver wraps the Playwright CLI of upstream Playwright. +// +// It's required for playwright-go to work. type PlaywrightDriver struct { driverDirectory, Version string options *RunOptions } -func NewDriver(options *RunOptions) (*PlaywrightDriver, error) { - baseDriverDirectory := options.DriverDirectory - if baseDriverDirectory == "" { - baseDriverDirectory = os.Getenv("PLAYWRIGHT_DRIVER_PATH") - } - if baseDriverDirectory == "" { - var err error - baseDriverDirectory, err = getDefaultCacheDirectory() - if err != nil { - return nil, fmt.Errorf("could not get default cache directory: %w", err) - } +func NewDriver(options ...*RunOptions) (*PlaywrightDriver, error) { + transformed, err := transformRunOptions(options...) // get default values + if err != nil { + return nil, err } return &PlaywrightDriver{ - options: options, - driverDirectory: filepath.Join(baseDriverDirectory, "ms-playwright-go", playwrightCliVersion), + options: transformed, + driverDirectory: filepath.Join(transformed.DriverDirectory, "ms-playwright-go", playwrightCliVersion), Version: playwrightCliVersion, }, nil } @@ -225,19 +221,26 @@ func (d *PlaywrightDriver) uninstallBrowsers() error { // RunOptions are custom options to run the driver type RunOptions struct { + // DriverDirectory is the directory where the playwright cli will be downloaded. + // Default depends on the platform: + // - Windows: %USERPROFILE%\AppData\Local + // - macOS: ~/Library/Caches + // - Linux: ~/.cache + // You can specify here or set the environment variable PLAYWRIGHT_DRIVER_PATH DriverDirectory string SkipInstallBrowsers bool - Browsers []string - Verbose bool // default true - Stdout io.Writer - Stderr io.Writer + // if not set and SkipInstallBrowsers is false, will download all browsers (chromium, firefox, webkit) + Browsers []string + Verbose bool // default true + Stdout io.Writer + Stderr io.Writer } // Install does download the driver and the browsers. // // Use this before playwright.Run() or use playwright cli to install the driver and browsers func Install(options ...*RunOptions) error { - driver, err := NewDriver(transformRunOptions(options)) + driver, err := NewDriver(options...) if err != nil { return fmt.Errorf("could not get driver instance: %w", err) } @@ -252,7 +255,7 @@ func Install(options ...*RunOptions) error { // Requires the driver and the browsers to be installed before. // Either use Install() or use playwright cli. func Run(options ...*RunOptions) (*Playwright, error) { - driver, err := NewDriver(transformRunOptions(options)) + driver, err := NewDriver(options...) if err != nil { return nil, fmt.Errorf("could not get driver instance: %w", err) } @@ -268,13 +271,23 @@ func Run(options ...*RunOptions) (*Playwright, error) { return playwright, err } -func transformRunOptions(options []*RunOptions) *RunOptions { +func transformRunOptions(options ...*RunOptions) (*RunOptions, error) { option := &RunOptions{ Verbose: true, } if len(options) == 1 { option = options[0] } + if option.DriverDirectory == "" { // if user did not set it, try to get it from env + option.DriverDirectory = os.Getenv("PLAYWRIGHT_DRIVER_PATH") + } + if option.DriverDirectory == "" { + var err error + option.DriverDirectory, err = getDefaultCacheDirectory() + if err != nil { + return nil, fmt.Errorf("could not get default cache directory: %w", err) + } + } if option.Stdout == nil { option.Stdout = os.Stdout } @@ -283,7 +296,7 @@ func transformRunOptions(options []*RunOptions) *RunOptions { } else { logger.SetOutput(option.Stderr) } - return option + return option, nil } func getNodeExecutable(driverDirectory string) string {