32
32
//
33
33
// It's required for playwright-go to work.
34
34
type PlaywrightDriver struct {
35
- driverDirectory , Version string
36
- options * RunOptions
35
+ Version string
36
+ options * RunOptions
37
37
}
38
38
39
39
func NewDriver (options ... * RunOptions ) (* PlaywrightDriver , error ) {
@@ -42,9 +42,8 @@ func NewDriver(options ...*RunOptions) (*PlaywrightDriver, error) {
42
42
return nil , err
43
43
}
44
44
return & PlaywrightDriver {
45
- options : transformed ,
46
- driverDirectory : filepath .Join (transformed .DriverDirectory , "ms-playwright-go" , playwrightCliVersion ),
47
- Version : playwrightCliVersion ,
45
+ options : transformed ,
46
+ Version : playwrightCliVersion ,
48
47
}, nil
49
48
}
50
49
@@ -65,13 +64,15 @@ func getDefaultCacheDirectory() (string, error) {
65
64
}
66
65
67
66
func (d * PlaywrightDriver ) isUpToDateDriver () (bool , error ) {
68
- if _ , err := os .Stat (d .driverDirectory ); os .IsNotExist (err ) {
69
- if err := os .MkdirAll (d .driverDirectory , 0o777 ); err != nil {
67
+ if _ , err := os .Stat (d .options . DriverDirectory ); os .IsNotExist (err ) {
68
+ if err := os .MkdirAll (d .options . DriverDirectory , 0o777 ); err != nil {
70
69
return false , fmt .Errorf ("could not create driver directory: %w" , err )
71
70
}
72
71
}
73
- if _ , err := os .Stat (getDriverCliJs (d .driverDirectory )); os .IsNotExist (err ) {
72
+ if _ , err := os .Stat (getDriverCliJs (d .options . DriverDirectory )); os .IsNotExist (err ) {
74
73
return false , nil
74
+ } else if err != nil {
75
+ return false , fmt .Errorf ("could not check if driver is up2date: %w" , err )
75
76
}
76
77
cmd := d .Command ("--version" )
77
78
output , err := cmd .Output ()
@@ -81,12 +82,13 @@ func (d *PlaywrightDriver) isUpToDateDriver() (bool, error) {
81
82
if bytes .Contains (output , []byte (d .Version )) {
82
83
return true , nil
83
84
}
84
- return false , nil
85
+ // avoid triggering downloads and accidentally overwriting files
86
+ return false , fmt .Errorf ("driver exists but version not %s in : %s" , d .Version , d .options .DriverDirectory )
85
87
}
86
88
87
89
// Command returns an exec.Cmd for the driver.
88
90
func (d * PlaywrightDriver ) Command (arg ... string ) * exec.Cmd {
89
- cmd := exec .Command (getNodeExecutable (d .driverDirectory ), append ([]string {getDriverCliJs (d .driverDirectory )}, arg ... )... )
91
+ cmd := exec .Command (getNodeExecutable (d .options . DriverDirectory ), append ([]string {getDriverCliJs (d .options . DriverDirectory )}, arg ... )... )
90
92
cmd .SysProcAttr = defaultSysProcAttr
91
93
return cmd
92
94
}
@@ -117,7 +119,7 @@ func (d *PlaywrightDriver) Uninstall() error {
117
119
}
118
120
119
121
d .log ("Removing driver..." )
120
- if err := os .RemoveAll (d .driverDirectory ); err != nil {
122
+ if err := os .RemoveAll (d .options . DriverDirectory ); err != nil {
121
123
return fmt .Errorf ("could not remove driver directory: %w" , err )
122
124
}
123
125
@@ -129,13 +131,13 @@ func (d *PlaywrightDriver) Uninstall() error {
129
131
func (d * PlaywrightDriver ) DownloadDriver () error {
130
132
up2Date , err := d .isUpToDateDriver ()
131
133
if err != nil {
132
- return fmt . Errorf ( "could not check if driver is up2date: %w" , err )
134
+ return err
133
135
}
134
136
if up2Date {
135
137
return nil
136
138
}
137
139
138
- d .log (fmt .Sprintf ("Downloading driver to %s" , d .driverDirectory ))
140
+ d .log (fmt .Sprintf ("Downloading driver to %s" , d .options . DriverDirectory ))
139
141
140
142
body , err := downloadDriver (d .getDriverURLs ())
141
143
if err != nil {
@@ -147,7 +149,7 @@ func (d *PlaywrightDriver) DownloadDriver() error {
147
149
}
148
150
149
151
for _ , zipFile := range zipReader .File {
150
- zipFileDiskPath := filepath .Join (d .driverDirectory , zipFile .Name )
152
+ zipFileDiskPath := filepath .Join (d .options . DriverDirectory , zipFile .Name )
151
153
if zipFile .FileInfo ().IsDir () {
152
154
if err := os .MkdirAll (zipFileDiskPath , os .ModePerm ); err != nil {
153
155
return fmt .Errorf ("could not create directory: %w" , err )
@@ -219,12 +221,14 @@ func (d *PlaywrightDriver) uninstallBrowsers() error {
219
221
220
222
// RunOptions are custom options to run the driver
221
223
type RunOptions struct {
222
- // DriverDirectory is the directory where the playwright cli will be downloaded.
223
- // Default depends on the platform:
224
+ // DriverDirectory points to the playwright driver directory.
225
+ // It should have two subdirectories: node and package.
226
+ // You can also specify it using the environment variable PLAYWRIGHT_DRIVER_PATH.
227
+ //
228
+ // Default is user cache directory + "/ms-playwright-go/x.xx.xx":
224
229
// - Windows: %USERPROFILE%\AppData\Local
225
230
// - macOS: ~/Library/Caches
226
231
// - Linux: ~/.cache
227
- // You can specify here or set the environment variable PLAYWRIGHT_DRIVER_PATH
228
232
DriverDirectory string
229
233
SkipInstallBrowsers bool
230
234
// if not set and SkipInstallBrowsers is false, will download all browsers (chromium, firefox, webkit)
@@ -259,7 +263,7 @@ func Run(options ...*RunOptions) (*Playwright, error) {
259
263
}
260
264
up2date , err := driver .isUpToDateDriver ()
261
265
if err != nil || ! up2date {
262
- return nil , fmt .Errorf ("please install the driver (v%s) and browsers first: %w" , playwrightCliVersion , err )
266
+ return nil , fmt .Errorf ("please install the driver (v%s) first: %w" , playwrightCliVersion , err )
263
267
}
264
268
connection , err := driver .run ()
265
269
if err != nil {
@@ -280,11 +284,11 @@ func transformRunOptions(options ...*RunOptions) (*RunOptions, error) {
280
284
option .DriverDirectory = os .Getenv ("PLAYWRIGHT_DRIVER_PATH" )
281
285
}
282
286
if option .DriverDirectory == "" {
283
- var err error
284
- option .DriverDirectory , err = getDefaultCacheDirectory ()
287
+ cacheDirectory , err := getDefaultCacheDirectory ()
285
288
if err != nil {
286
289
return nil , fmt .Errorf ("could not get default cache directory: %w" , err )
287
290
}
291
+ option .DriverDirectory = filepath .Join (cacheDirectory , "ms-playwright-go" , playwrightCliVersion )
288
292
}
289
293
if option .Stdout == nil {
290
294
option .Stdout = os .Stdout
0 commit comments