Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 23 additions & 17 deletions d2cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
35 changes: 18 additions & 17 deletions d2cli/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 14 additions & 6 deletions lib/png/png.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand All @@ -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,"
Expand Down