Skip to content

Commit

Permalink
refactor: transform RunOptions with deafult values and add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
canstand authored Sep 16, 2024
1 parent dcdd792 commit 6ad91ad
Showing 1 changed file with 34 additions and 21 deletions.
55 changes: 34 additions & 21 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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
}
Expand All @@ -283,7 +296,7 @@ func transformRunOptions(options []*RunOptions) *RunOptions {
} else {
logger.SetOutput(option.Stderr)
}
return option
return option, nil
}

func getNodeExecutable(driverDirectory string) string {
Expand Down

0 comments on commit 6ad91ad

Please sign in to comment.