Update help, manpages, and cleanup

This commit is contained in:
Bernard Xie 2022-11-17 14:13:23 -08:00
parent d332b2e7d6
commit 530c70445a
No known key found for this signature in database
GPG key ID: 3C3E0036CE0F892C
4 changed files with 15 additions and 24 deletions

View file

@ -9,7 +9,7 @@
.Op Fl -watch Ar false .Op Fl -watch Ar false
.Op Fl -theme Em 0 .Op Fl -theme Em 0
.Ar file.d2 .Ar file.d2
.Op Ar file.svg .Op Ar file.svg|file.png
.Nm d2 .Nm d2
.Op Fl -watch Ar false .Op Fl -watch Ar false
.Op Fl -theme Em 0 .Op Fl -theme Em 0
@ -22,7 +22,9 @@
compiles and renders compiles and renders
.Ar file.d2 .Ar file.d2
to to
.Ar file.svg .Ar file.svg
or
.Ar png
.Ns . .Ns .
.Pp .Pp
Pass - to have Pass - to have

View file

@ -15,9 +15,9 @@ import (
func help(ms *xmain.State) { func help(ms *xmain.State) {
fmt.Fprintf(ms.Stdout, `Usage: fmt.Fprintf(ms.Stdout, `Usage:
%s [--watch=false] [--theme=0] file.d2 [file.svg] %s [--watch=false] [--theme=0] file.d2 [file.svg|file.png]
%[1]s compiles and renders file.d2 to file.svg %[1]s compiles and renders file.d2 to file.svg or png
Use - to have d2 read from stdin or write to stdout. Use - to have d2 read from stdin or write to stdout.
Flags: Flags:

View file

@ -34,7 +34,7 @@ func run(ctx context.Context, ms *xmain.State) (err error) {
watchFlag := ms.FlagSet.BoolP("watch", "w", false, "watch for changes to input and live reload. Use $PORT and $HOST to specify the listening address.\n$D2_PORT and $D2_HOST are also accepted and take priority. Default is localhost:0") watchFlag := ms.FlagSet.BoolP("watch", "w", false, "watch for changes to input and live reload. Use $PORT and $HOST to specify the listening address.\n$D2_PORT and $D2_HOST are also accepted and take priority. Default is localhost:0")
themeFlag := ms.FlagSet.Int64P("theme", "t", 0, "set the diagram theme. For a list of available options, see https://oss.terrastruct.com/d2") themeFlag := ms.FlagSet.Int64P("theme", "t", 0, "set the diagram theme. For a list of available options, see https://oss.terrastruct.com/d2")
bundleFlag := ms.FlagSet.BoolP("bundle", "b", true, "bundle all assets and layers into the output svg") bundleFlag := ms.FlagSet.BoolP("bundle", "b", true, "when outputting SVG, bundle all assets and layers into the output file")
versionFlag := ms.FlagSet.BoolP("version", "v", false, "get the version") versionFlag := ms.FlagSet.BoolP("version", "v", false, "get the version")
debugFlag := ms.FlagSet.BoolP("debug", "d", false, "print debug logs") debugFlag := ms.FlagSet.BoolP("debug", "d", false, "print debug logs")
err = ms.FlagSet.Parse(ms.Args) err = ms.FlagSet.Parse(ms.Args)
@ -119,12 +119,8 @@ func run(ctx context.Context, ms *xmain.State) (err error) {
if err != nil { if err != nil {
return err return err
} }
defer func() error { defer func() {
err = pw.Cleanup(*watchFlag) err = pw.Cleanup()
if err != nil {
return err
}
return nil
}() }()
} }

View file

@ -23,9 +23,6 @@ type Playwright struct {
} }
func (pw *Playwright) RestartBrowser() (newPW Playwright, err error) { func (pw *Playwright) RestartBrowser() (newPW Playwright, err error) {
if err = pw.BrowserContext.Close(); err != nil {
return Playwright{}, err
}
if err = pw.Browser.Close(); err != nil { if err = pw.Browser.Close(); err != nil {
return Playwright{}, err return Playwright{}, err
} }
@ -49,12 +46,7 @@ func (pw *Playwright) RestartBrowser() (newPW Playwright, err error) {
}, nil }, nil
} }
func (pw *Playwright) Cleanup(isWatch bool) (err error) { func (pw *Playwright) Cleanup() (err error) {
if !isWatch {
if err = pw.BrowserContext.Close(); err != nil {
return err
}
}
if err = pw.Browser.Close(); err != nil { if err = pw.Browser.Close(); err != nil {
return err return err
} }
@ -116,8 +108,7 @@ var genPNGScript string
func ExportPNG(ms *xmain.State, page playwright.Page, svg []byte) (outputImage []byte, err error) { func ExportPNG(ms *xmain.State, page playwright.Page, svg []byte) (outputImage []byte, err error) {
if page == nil { if page == nil {
ms.Log.Error.Printf("Playwright was not initialized properly for PNG export") return nil, fmt.Errorf("Playwright was not initialized properly for PNG export")
return nil, fmt.Errorf("Playwright page is nil")
} }
encodedSVG := base64.StdEncoding.EncodeToString(svg) encodedSVG := base64.StdEncoding.EncodeToString(svg)
@ -129,8 +120,10 @@ func ExportPNG(ms *xmain.State, page playwright.Page, svg []byte) (outputImage [
pngString := fmt.Sprintf("%v", pngInterface) pngString := fmt.Sprintf("%v", pngInterface)
pngPrefix := "data:image/png;base64," pngPrefix := "data:image/png;base64,"
if !strings.HasPrefix(pngString, pngPrefix) { if !strings.HasPrefix(pngString, pngPrefix) {
ms.Log.Error.Printf("failed to convert D2 file to PNG") if len(pngString) > 20 {
return nil, fmt.Errorf("Playwright export generated invalid png") pngString = pngString[0:20] + "..."
}
return nil, fmt.Errorf("invalid PNG: %v\nplease report this issue here: https://github.com/terrastruct/d2/issues/new", pngString)
} }
splicedPNGString := pngString[len(pngPrefix):] splicedPNGString := pngString[len(pngPrefix):]
return base64.StdEncoding.DecodeString(splicedPNGString) return base64.StdEncoding.DecodeString(splicedPNGString)