-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstaller.go
More file actions
99 lines (84 loc) · 2.44 KB
/
Copy pathinstaller.go
File metadata and controls
99 lines (84 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Package browserpm provides dependency and driver installation for Playwright.
package browserpm
import (
"fmt"
"io"
"os"
"sync"
"github.com/playwright-community/playwright-go"
)
// Installer handles Playwright dependency installation
type Installer struct {
config *Config
log Logger
installed bool
installErr error
once sync.Once
}
// NewInstaller creates a new installer instance
func NewInstaller(cfg *Config, log Logger) *Installer {
if log == nil {
log = NewNopLogger()
}
return &Installer{
config: cfg,
log: log,
}
}
// Install executes the installation process (idempotent, thread-safe)
// playwright install itself is idempotent - it will update to latest if needed
func (i *Installer) Install() error {
i.once.Do(func() {
i.log.Info("Starting Playwright installation")
// Create driver options
options := &playwright.RunOptions{
SkipInstallBrowsers: false,
Browsers: []string{"chromium"},
Verbose: true,
Stdout: io.Writer(os.Stdout),
Stderr: io.Writer(os.Stderr),
DriverDirectory: defaultInstallPath,
}
// Create driver instance
driver, err := playwright.NewDriver(options)
if err != nil {
i.installErr = fmt.Errorf("failed to create driver: %w", err)
i.log.Error("Failed to create driver", err)
return
}
// Download driver
i.log.Info("Downloading Playwright driver")
if err := driver.DownloadDriver(); err != nil {
i.installErr = fmt.Errorf("failed to download driver: %w", err)
i.log.Error("Failed to download driver", err)
return
}
// Build install command args
args := []string{"install", "chromium"}
if i.config.Install.WithDeps {
args = append(args, "--with-deps")
}
// Install browser
i.log.Info("Installing Chromium browser")
cmd := driver.Command(args...)
cmd.Stdout = options.Stdout
cmd.Stderr = options.Stderr
if err := cmd.Run(); err != nil {
i.installErr = fmt.Errorf("failed to install browser: %w", err)
i.log.Error("Failed to install browser", err)
return
}
i.installed = true
i.log.Info("Playwright installation completed successfully")
})
return i.installErr
}
// IsInstalled returns whether dependencies are installed
func (i *Installer) IsInstalled() bool {
return i.installed
}
// EnsureInstalled checks and installs if necessary
func EnsureInstalled(cfg *Config, log Logger) error {
installer := NewInstaller(cfg, log)
return installer.Install()
}