From 789b4e297844563b6a56514e7edb2f49f223834b Mon Sep 17 00:00:00 2001 From: Qiao Han Date: Sat, 13 Apr 2024 01:52:32 +0800 Subject: [PATCH 1/2] fix: suggest bootstrap start command from template --- cmd/bootstrap.go | 24 ++++++++++++++++-------- internal/bootstrap/bootstrap.go | 17 ++++++++++------- internal/bootstrap/bootstrap_test.go | 13 ++++++------- 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/cmd/bootstrap.go b/cmd/bootstrap.go index 8fdfac61a..003713af6 100644 --- a/cmd/bootstrap.go +++ b/cmd/bootstrap.go @@ -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" @@ -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, @@ -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()) }, } ) @@ -73,8 +81,8 @@ 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) @@ -82,7 +90,7 @@ func promptStarterTemplate(ctx context.Context, templates []bootstrap.StarterTem return err } if choice.Index < len(templates) { - templateUrl = templates[choice.Index].Url + starter = templates[choice.Index] } return nil } diff --git a/internal/bootstrap/bootstrap.go b/internal/bootstrap/bootstrap.go index 4eacd9625..ffcd02a54 100644 --- a/internal/bootstrap/bootstrap.go +++ b/internal/bootstrap/bootstrap.go @@ -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) @@ -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 { @@ -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 @@ -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 { @@ -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)) @@ -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) { diff --git a/internal/bootstrap/bootstrap_test.go b/internal/bootstrap/bootstrap_test.go index 34661e9d9..6cf6eb6e4 100644 --- a/internal/bootstrap/bootstrap_test.go +++ b/internal/bootstrap/bootstrap_test.go @@ -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) }) } From 2d271ca914b5be9b1088a8ecd86da79c779dc258 Mon Sep 17 00:00:00 2001 From: Qiao Han Date: Sat, 13 Apr 2024 02:00:49 +0800 Subject: [PATCH 2/2] chore: bold created project url --- internal/projects/create/create.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/projects/create/create.go b/internal/projects/create/create.go index 7e107500e..c7d76f43f 100644 --- a/internal/projects/create/create.go +++ b/internal/projects/create/create.go @@ -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 }