Merge pull request #2058 from alixander/fix-watch-underscore
d2cli: fix watch mode back links
This commit is contained in:
commit
610b708eb4
4 changed files with 62 additions and 4 deletions
|
|
@ -15,3 +15,4 @@
|
|||
- Sequence diagram: long self-referential edge labels no longer can collide neighboring actors (or its own) lifeline edges [#2050](https://github.com/terrastruct/d2/pull/2050)
|
||||
- Globs: An edge case was fixed where globs used in edges were creating nodes when it shouldn't have [#2051](https://github.com/terrastruct/d2/pull/2051)
|
||||
- Render: Multi-line class labels/headers are rendered correctly [#2057](https://github.com/terrastruct/d2/pull/2057)
|
||||
- CLI: Watch mode uses correct backlinks (`_` usages) [#2058](https://github.com/terrastruct/d2/pull/2058)
|
||||
|
|
|
|||
|
|
@ -480,13 +480,13 @@ func compile(ctx context.Context, ms *xmain.State, plugins []d2plugin.Plugin, fs
|
|||
}, time.Second*5)
|
||||
defer cancel()
|
||||
|
||||
diagram, g, err := d2lib.Compile(ctx, string(input), opts, &renderOpts)
|
||||
rootDiagram, g, err := d2lib.Compile(ctx, string(input), opts, &renderOpts)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
cancel()
|
||||
|
||||
diagram = diagram.GetBoard(boardPath)
|
||||
diagram := rootDiagram.GetBoard(boardPath)
|
||||
if diagram == nil {
|
||||
return nil, false, fmt.Errorf(`render target "%s" not found`, strings.Join(boardPath, "."))
|
||||
}
|
||||
|
|
@ -589,11 +589,11 @@ func compile(ctx context.Context, ms *xmain.State, plugins []d2plugin.Plugin, fs
|
|||
compileDur := time.Since(start)
|
||||
if animateInterval <= 0 {
|
||||
// Rename all the "root.layers.x" to the paths that the boards get output to
|
||||
linkToOutput, err := resolveLinks("root", outputPath, diagram)
|
||||
linkToOutput, err := resolveLinks("root", outputPath, rootDiagram)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
err = relink("root", diagram, linkToOutput)
|
||||
err = relink("root", rootDiagram, linkToOutput)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -520,6 +520,10 @@ func (w *watcher) handleRoot(hw http.ResponseWriter, r *http.Request) {
|
|||
if idx := strings.LastIndexByte(boardPath, '.'); idx != -1 {
|
||||
boardPath = boardPath[:idx]
|
||||
}
|
||||
// if path is "/index", we just want "/"
|
||||
if boardPath == "index" {
|
||||
boardPath = ""
|
||||
}
|
||||
recompile := false
|
||||
if boardPath != w.boardPath {
|
||||
w.boardPath = boardPath
|
||||
|
|
|
|||
|
|
@ -986,6 +986,59 @@ layers: {
|
|||
assert.Success(t, err)
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "watch-underscore-link",
|
||||
serial: true,
|
||||
run: func(t *testing.T, ctx context.Context, dir string, env *xos.Env) {
|
||||
writeFile(t, dir, "index.d2", `
|
||||
bobby
|
||||
|
||||
layers: {
|
||||
cream: {
|
||||
back.link: _
|
||||
}
|
||||
}`)
|
||||
stderr := &stderrWrapper{}
|
||||
tms := testMain(dir, env, "--watch", "--browser=0", "index.d2")
|
||||
tms.Stderr = stderr
|
||||
|
||||
tms.Start(t, ctx)
|
||||
defer func() {
|
||||
// Manually close, since watcher is daemon
|
||||
err := tms.Signal(ctx, os.Interrupt)
|
||||
assert.Success(t, err)
|
||||
}()
|
||||
|
||||
// Wait for watch server to spin up and listen
|
||||
urlRE := regexp.MustCompile(`127.0.0.1:([0-9]+)`)
|
||||
watchURL, err := waitLogs(ctx, stderr, urlRE)
|
||||
assert.Success(t, err)
|
||||
|
||||
stderr.Reset()
|
||||
|
||||
// Start a client
|
||||
c, _, err := websocket.Dial(ctx, fmt.Sprintf("ws://%s/watch", watchURL), nil)
|
||||
assert.Success(t, err)
|
||||
defer c.CloseNow()
|
||||
|
||||
_, _, err = c.Read(ctx)
|
||||
assert.Success(t, err)
|
||||
|
||||
err = getWatchPage(ctx, t, fmt.Sprintf("http://%s/%s", watchURL, "cream"))
|
||||
assert.Success(t, err)
|
||||
|
||||
// Get the link
|
||||
_, msg, err := c.Read(ctx)
|
||||
aRE := regexp.MustCompile(`href=\\"([^\"]*)\\"`)
|
||||
match := aRE.FindSubmatch(msg)
|
||||
assert.Equal(t, 2, len(match))
|
||||
assert.Equal(t, "index.svg", string(match[1]))
|
||||
|
||||
successRE := regexp.MustCompile(`broadcasting update to 1 client`)
|
||||
_, err = waitLogs(ctx, stderr, successRE)
|
||||
assert.Success(t, err)
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "watch-bad-link",
|
||||
serial: true,
|
||||
|
|
|
|||
Loading…
Reference in a new issue