Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support latest version of Storybook #1024

Merged
merged 1 commit into from
Dec 24, 2024
Merged
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
2 changes: 2 additions & 0 deletions storybook/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
storybook-server

*storybook.log
7 changes: 7 additions & 0 deletions storybook/_package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "storybook-server",
"version": "1.0.0",
"main": "index.js",
"author": "",
"description": ""
}
24 changes: 22 additions & 2 deletions storybook/storybook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (

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

_ "embed"

"github.com/a-h/templ"
"github.com/rs/cors"
"go.uber.org/zap"
Expand Down Expand Up @@ -171,16 +173,30 @@ func (sh *Storybook) previewHandler(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r)
return
}
name = strings.TrimPrefix(name, "/")

h, found := sh.Handlers[name]
if !found {
sh.Log.Info("Component name not found", zap.String("name", name), zap.String("url", r.URL.String()))
sh.Log.Info("Component name not found", zap.String("name", name), zap.String("url", r.URL.String()), zap.Strings("available", keysOfMap(sh.Handlers)))
http.NotFound(w, r)
return
}
h.ServeHTTP(w, r)
}

func keysOfMap[K comparable, V any](handler map[K]V) (keys []K) {
keys = make([]K, len(handler))
var i int
for k := range handler {
keys[i] = k
i++
}
return keys
}

//go:embed _package.json
var packageJSON string

func (sh *Storybook) installStorybook() (err error) {
_, err = os.Stat(sh.Path)
if err == nil {
Expand All @@ -192,6 +208,10 @@ func (sh *Storybook) installStorybook() (err error) {
if err != nil {
return fmt.Errorf("templ-storybook: error creating @storybook/server directory: %w", err)
}
err = os.WriteFile(filepath.Join(sh.Path, "package.json"), []byte(packageJSON), 0644)
if err != nil {
return fmt.Errorf("templ-storybook: error writing package.json: %w", err)
}
}
var cmd exec.Cmd
cmd.Dir = sh.Path
Expand All @@ -201,7 +221,7 @@ func (sh *Storybook) installStorybook() (err error) {
if err != nil {
return fmt.Errorf("templ-storybook: cannot install storybook, cannot find npx on the path, check that Node.js is installed: %w", err)
}
cmd.Args = []string{"npx", "sb", "init", "-t", "server"}
cmd.Args = []string{"npx", "sb", "init", "-t", "server", "--no-dev"}
return cmd.Run()
}

Expand Down
Loading