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

fix: suggest bootstrap start command from template #2147

Merged
merged 2 commits into from
Apr 12, 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
24 changes: 16 additions & 8 deletions cmd/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os/signal"
"strings"

"github.com/go-errors/errors"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -15,7 +16,11 @@ import (
)

var (
templateUrl string
starter = bootstrap.StarterTemplate{
Name: "scratch",
Description: "An empty project from scratch.",
Start: "supabase start",
}

bootstrapCmd = &cobra.Command{
GroupID: groupQuickStart,
Expand All @@ -41,16 +46,19 @@ var (
name := strings.ToLower(args[0])
for _, t := range templates {
if t.Name == name {
templateUrl = t.Url
starter = t
break
}
}
}
if len(templateUrl) == 0 {
if name != starter.Name {
return errors.New("Invalid template: " + args[0])
}
} else {
if err := promptStarterTemplate(ctx, templates); err != nil {
return err
}
}
return bootstrap.Run(ctx, templateUrl, afero.NewOsFs())
return bootstrap.Run(ctx, starter, afero.NewOsFs())
},
}
)
Expand All @@ -73,16 +81,16 @@ func promptStarterTemplate(ctx context.Context, templates []bootstrap.StarterTem
}
items = append(items, utils.PromptItem{
Index: len(items),
Summary: "scratch",
Details: "An empty project from scratch.",
Summary: starter.Name,
Details: starter.Description,
})
title := "Which starter template do you want to use?"
choice, err := utils.PromptChoice(ctx, title, items)
if err != nil {
return err
}
if choice.Index < len(templates) {
templateUrl = templates[choice.Index].Url
starter = templates[choice.Index]
}
return nil
}
17 changes: 10 additions & 7 deletions internal/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
"golang.org/x/term"
)

func Run(ctx context.Context, templateUrl string, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
func Run(ctx context.Context, starter StarterTemplate, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
workdir := viper.GetString("WORKDIR")
if !filepath.IsAbs(workdir) {
workdir = filepath.Join(utils.CurrentDirAbs, workdir)
Expand All @@ -54,9 +54,9 @@ func Run(ctx context.Context, templateUrl string, fsys afero.Fs, options ...func
return err
}
// 0. Download starter template
if len(templateUrl) > 0 {
if len(starter.Url) > 0 {
client := GetGtihubClient(ctx)
if err := downloadSample(ctx, client, templateUrl, fsys); err != nil {
if err := downloadSample(ctx, client, starter.Url, fsys); err != nil {
return err
}
} else if err := initBlank.Run(fsys, nil, nil, utils.InitParams{Overwrite: true}); err != nil {
Expand All @@ -77,7 +77,7 @@ func Run(ctx context.Context, templateUrl string, fsys afero.Fs, options ...func
// 2. Create project
params := api.CreateProjectBody{
Name: filepath.Base(workdir),
TemplateUrl: &templateUrl,
TemplateUrl: &starter.Url,
}
if err := create.Run(ctx, params, fsys); err != nil {
return err
Expand Down Expand Up @@ -122,11 +122,11 @@ func Run(ctx context.Context, templateUrl string, fsys afero.Fs, options ...func
}, policy, newErrorCallback()); err != nil {
return err
}
utils.CmdSuggestion = suggestAppStart(utils.CurrentDirAbs)
utils.CmdSuggestion = suggestAppStart(utils.CurrentDirAbs, starter.Start)
return nil
}

func suggestAppStart(cwd string) string {
func suggestAppStart(cwd, command string) string {
logger := utils.GetDebugLogger()
workdir, err := os.Getwd()
if err != nil {
Expand All @@ -140,7 +140,9 @@ func suggestAppStart(cwd string) string {
if len(workdir) > 0 && workdir != "." {
cmd = append(cmd, "cd "+workdir)
}
cmd = append(cmd, "npm ci", "npm run dev")
if len(command) > 0 {
cmd = append(cmd, command)
}
suggestion := "To start your app:"
for _, c := range cmd {
suggestion += fmt.Sprintf("\n %s", utils.Aqua(c))
Expand Down Expand Up @@ -314,6 +316,7 @@ type StarterTemplate struct {
Name string `json:"name"`
Description string `json:"description"`
Url string `json:"url"`
Start string `json:"start"`
}

func ListSamples(ctx context.Context, client *github.Client) ([]StarterTemplate, error) {
Expand Down
13 changes: 6 additions & 7 deletions internal/bootstrap/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,28 @@ func TestSuggestAppStart(t *testing.T) {
cwd, err := os.Getwd()
require.NoError(t, err)
// Run test
suggestion := suggestAppStart(cwd)
suggestion := suggestAppStart(cwd, "npm ci && npm run dev")
// Check error
assert.Equal(t, "To start your app:\n npm ci\n npm run dev", suggestion)
assert.Equal(t, "To start your app:\n npm ci && npm run dev", suggestion)
})

t.Run("suggest cd", func(t *testing.T) {
cwd, err := os.Getwd()
require.NoError(t, err)
// Run test
suggestion := suggestAppStart(filepath.Dir(cwd))
suggestion := suggestAppStart(filepath.Dir(cwd), "npm ci && npm run dev")
// Check error
expected := "To start your app:"
expected += "\n cd " + filepath.Base(cwd)
expected += "\n npm ci"
expected += "\n npm run dev"
expected += "\n npm ci && npm run dev"
assert.Equal(t, expected, suggestion)
})

t.Run("ignore relative path", func(t *testing.T) {
// Run test
suggestion := suggestAppStart(".")
suggestion := suggestAppStart(".", "supabase start")
// Check error
assert.Equal(t, "To start your app:\n npm ci\n npm run dev", suggestion)
assert.Equal(t, "To start your app:\n supabase start", suggestion)
})
}

Expand Down
2 changes: 1 addition & 1 deletion internal/projects/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func Run(ctx context.Context, params api.CreateProjectBody, fsys afero.Fs) error
}

projectUrl := fmt.Sprintf("%s/project/%s", utils.GetSupabaseDashboardURL(), resp.JSON201.Id)
fmt.Printf("Created a new project %s at %s\n", utils.Aqua(resp.JSON201.Name), projectUrl)
fmt.Printf("Created a new project %s at %s\n", utils.Aqua(resp.JSON201.Name), utils.Bold(projectUrl))
return nil
}

Expand Down
Loading