Skip to content

Commit 3eaa56a

Browse files
authored
feat: support latest version of Storybook (#1024)
1 parent 1d87e1a commit 3eaa56a

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

storybook/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
storybook-server
2+
3+
*storybook.log

storybook/_package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "storybook-server",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"author": "",
6+
"description": ""
7+
}

storybook/storybook.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818

1919
"golang.org/x/mod/sumdb/dirhash"
2020

21+
_ "embed"
22+
2123
"github.com/a-h/templ"
2224
"github.com/rs/cors"
2325
"go.uber.org/zap"
@@ -171,16 +173,30 @@ func (sh *Storybook) previewHandler(w http.ResponseWriter, r *http.Request) {
171173
http.NotFound(w, r)
172174
return
173175
}
176+
name = strings.TrimPrefix(name, "/")
174177

175178
h, found := sh.Handlers[name]
176179
if !found {
177-
sh.Log.Info("Component name not found", zap.String("name", name), zap.String("url", r.URL.String()))
180+
sh.Log.Info("Component name not found", zap.String("name", name), zap.String("url", r.URL.String()), zap.Strings("available", keysOfMap(sh.Handlers)))
178181
http.NotFound(w, r)
179182
return
180183
}
181184
h.ServeHTTP(w, r)
182185
}
183186

187+
func keysOfMap[K comparable, V any](handler map[K]V) (keys []K) {
188+
keys = make([]K, len(handler))
189+
var i int
190+
for k := range handler {
191+
keys[i] = k
192+
i++
193+
}
194+
return keys
195+
}
196+
197+
//go:embed _package.json
198+
var packageJSON string
199+
184200
func (sh *Storybook) installStorybook() (err error) {
185201
_, err = os.Stat(sh.Path)
186202
if err == nil {
@@ -192,6 +208,10 @@ func (sh *Storybook) installStorybook() (err error) {
192208
if err != nil {
193209
return fmt.Errorf("templ-storybook: error creating @storybook/server directory: %w", err)
194210
}
211+
err = os.WriteFile(filepath.Join(sh.Path, "package.json"), []byte(packageJSON), 0644)
212+
if err != nil {
213+
return fmt.Errorf("templ-storybook: error writing package.json: %w", err)
214+
}
195215
}
196216
var cmd exec.Cmd
197217
cmd.Dir = sh.Path
@@ -201,7 +221,7 @@ func (sh *Storybook) installStorybook() (err error) {
201221
if err != nil {
202222
return fmt.Errorf("templ-storybook: cannot install storybook, cannot find npx on the path, check that Node.js is installed: %w", err)
203223
}
204-
cmd.Args = []string{"npx", "sb", "init", "-t", "server"}
224+
cmd.Args = []string{"npx", "sb", "init", "-t", "server", "--no-dev"}
205225
return cmd.Run()
206226
}
207227

0 commit comments

Comments
 (0)