diff --git a/d2cli/main.go b/d2cli/main.go index 6e49ad0615..c3358fc0d8 100644 --- a/d2cli/main.go +++ b/d2cli/main.go @@ -152,6 +152,11 @@ func Run(ctx context.Context, ms *xmain.State) (err error) { return err } + playwrightBrowserFlag := ms.Opts.String("D2_PLAYWRIGHT_BROWSER", "playwright-browser", "", "extended", "Any of the browsers listed here: https://playwright.dev/docs/browsers") + if err != nil { + return err + } + plugins, err := d2plugin.ListPlugins(ctx) if err != nil { return err @@ -324,7 +329,7 @@ func Run(ctx context.Context, ms *xmain.State) (err error) { } var pw png.Playwright if outputFormat.requiresPNGRenderer() { - pw, err = png.InitPlaywrightWithPrompt() + pw, err = png.InitPlaywrightWithPrompt(*playwrightBrowserFlag) if err != nil { return err } @@ -361,21 +366,22 @@ func Run(ctx context.Context, ms *xmain.State) (err error) { ms.Log.Debug.Printf("GIF export: animate-interval not specified, defaulting to 1000ms") } w, err := newWatcher(ctx, ms, watcherOpts{ - plugins: plugins, - layout: layoutFlag, - renderOpts: renderOpts, - animateInterval: animateInterval, - host: *hostFlag, - port: *portFlag, - inputPath: inputPath, - outputPath: outputPath, - bundle: *bundleFlag, - forceAppendix: *forceAppendixFlag, - pw: pw, - fontFamily: fontFamily, - monoFontFamily: monoFontFamily, - outputFormat: outputFormat, - asciiMode: *asciiModeFlag, + plugins: plugins, + layout: layoutFlag, + renderOpts: renderOpts, + animateInterval: animateInterval, + host: *hostFlag, + port: *portFlag, + inputPath: inputPath, + outputPath: outputPath, + bundle: *bundleFlag, + forceAppendix: *forceAppendixFlag, + pw: pw, + fontFamily: fontFamily, + monoFontFamily: monoFontFamily, + outputFormat: outputFormat, + asciiMode: *asciiModeFlag, + playwrightBrowser: *playwrightBrowserFlag, }) if err != nil { return err @@ -1298,7 +1304,7 @@ func populateLayoutOpts(ctx context.Context, ms *xmain.State, ps []d2plugin.Plug } func initPlaywright() error { - pw, err := png.InitPlaywrightWithPrompt() + pw, err := png.InitPlaywrightWithPrompt("chromium") if err != nil { return err } diff --git a/d2cli/watch.go b/d2cli/watch.go index 3e3db95b99..7cfc8d4b15 100644 --- a/d2cli/watch.go +++ b/d2cli/watch.go @@ -43,23 +43,24 @@ var devMode = false var staticFS embed.FS type watcherOpts struct { - layout *string - plugins []d2plugin.Plugin - renderOpts d2svg.RenderOpts - animateInterval int64 - host string - port string - inputPath string - outputPath string - boardPath string - pwd string - bundle bool - forceAppendix bool - pw png.Playwright - fontFamily *d2fonts.FontFamily - monoFontFamily *d2fonts.FontFamily - outputFormat exportExtension - asciiMode string + layout *string + plugins []d2plugin.Plugin + renderOpts d2svg.RenderOpts + animateInterval int64 + host string + port string + inputPath string + outputPath string + boardPath string + pwd string + bundle bool + forceAppendix bool + pw png.Playwright + fontFamily *d2fonts.FontFamily + monoFontFamily *d2fonts.FontFamily + outputFormat exportExtension + asciiMode string + playwrightBrowser string } type watcher struct { diff --git a/lib/png/png.go b/lib/png/png.go index 020826dc7c..4a5dc7640d 100644 --- a/lib/png/png.go +++ b/lib/png/png.go @@ -77,10 +77,15 @@ func startPlaywright(pw *playwright.Playwright) (Playwright, error) { }, nil } -func InitPlaywright() (Playwright, error) { +func InitPlaywright(playwrightBrowser string) (Playwright, error) { + + if playwrightBrowser == "" { + playwrightBrowser = "chromium" + } + err := playwright.Install(&playwright.RunOptions{ Verbose: false, - Browsers: []string{"chromium"}, + Browsers: []string{playwrightBrowser}, }) if err != nil { return Playwright{}, fmt.Errorf("failed to install Playwright: %w", err) @@ -93,13 +98,16 @@ func InitPlaywright() (Playwright, error) { return startPlaywright(pw) } -func InitPlaywrightWithPrompt() (Playwright, error) { +func InitPlaywrightWithPrompt(playwrightBrowser string) (Playwright, error) { if os.Getenv("CI") != "" { - return InitPlaywright() + return InitPlaywright("") } // Just try running first. This only works if drivers and browsers are already installed - pw, err := playwright.Run() + pw, err := playwright.Run( + &playwright.RunOptions{ + Browsers: []string{playwrightBrowser}, + }) if err == nil { return startPlaywright(pw) } @@ -115,7 +123,7 @@ func InitPlaywrightWithPrompt() (Playwright, error) { return Playwright{}, fmt.Errorf("chromium installation cancelled by user") } - return InitPlaywright() + return InitPlaywright(playwrightBrowser) } const pngPrefix = "data:image/png;base64,"