github.com/Redstoneguy129/cli@v0.0.0-20230211220159-15dca4e91917/internal/gen/types/typescript/typescript.go (about) 1 package typescript 2 3 import ( 4 "context" 5 "errors" 6 "fmt" 7 "net/url" 8 "os" 9 "strings" 10 11 "github.com/Redstoneguy129/cli/internal/utils" 12 "github.com/Redstoneguy129/cli/pkg/api" 13 "github.com/jackc/pgconn" 14 "github.com/spf13/afero" 15 ) 16 17 func Run(ctx context.Context, useLocal bool, useLinked bool, projectId string, dbUrl string, schemas []string, fsys afero.Fs) error { 18 coalesce := func(args ...[]string) []string { 19 for _, arg := range args { 20 if len(arg) != 0 { 21 return arg 22 } 23 } 24 return []string{} 25 } 26 27 // Generating types on `projectId` and `dbUrl` should work without `supabase 28 // init` - i.e. we shouldn't try to load the config for these cases. 29 30 if projectId != "" { 31 included := strings.Join(coalesce(schemas, []string{"public"}), ",") 32 resp, err := utils.GetSupabase().GetTypescriptTypesWithResponse(ctx, projectId, &api.GetTypescriptTypesParams{ 33 IncludedSchemas: &included, 34 }) 35 if err != nil { 36 return err 37 } 38 39 if resp.JSON200 == nil { 40 return errors.New("failed to retrieve generated types: " + string(resp.Body)) 41 } 42 43 fmt.Print(resp.JSON200.Types) 44 return nil 45 } 46 47 if dbUrl != "" { 48 config, err := pgconn.ParseConfig(dbUrl) 49 if err != nil { 50 return errors.New("URL is not a valid Supabase connection string: " + err.Error()) 51 } 52 escaped := fmt.Sprintf( 53 "postgresql://%s@%s:%d/%s", 54 url.UserPassword(config.User, config.Password), 55 config.Host, 56 config.Port, 57 url.PathEscape(config.Database), 58 ) 59 fmt.Fprintln(os.Stderr, "Connecting to", escaped) 60 61 out, err := utils.DockerRunOnce(ctx, utils.PgmetaImage, []string{ 62 "PG_META_DB_URL=" + escaped, 63 }, []string{ 64 "node", 65 "dist/server/app.js", 66 "gen", 67 "types", 68 "typescript", 69 "--include-schemas", 70 strings.Join(coalesce(schemas, []string{"public"}), ","), 71 }) 72 if err != nil { 73 return err 74 } 75 76 fmt.Print(out) 77 return nil 78 } 79 80 // only load config on `--local` or `--linked` 81 if err := utils.LoadConfigFS(fsys); err != nil { 82 return err 83 } 84 85 if useLocal { 86 if err := utils.AssertSupabaseDbIsRunning(); err != nil { 87 return err 88 } 89 90 out, err := utils.DockerRunOnce(ctx, utils.PgmetaImage, []string{ 91 "PG_META_DB_HOST=" + utils.DbId, 92 }, []string{ 93 "node", 94 "dist/server/app.js", 95 "gen", 96 "types", 97 "typescript", 98 "--include-schemas", 99 strings.Join(coalesce(schemas, utils.Config.Api.Schemas, []string{"public"}), ","), 100 }) 101 if err != nil { 102 return err 103 } 104 105 fmt.Print(out) 106 return nil 107 } 108 109 if useLinked { 110 projectId, err := utils.LoadProjectRef(fsys) 111 if err != nil { 112 return err 113 } 114 115 included := strings.Join(coalesce(schemas, utils.Config.Api.Schemas, []string{"public"}), ",") 116 resp, err := utils.GetSupabase().GetTypescriptTypesWithResponse(ctx, projectId, &api.GetTypescriptTypesParams{ 117 IncludedSchemas: &included, 118 }) 119 if err != nil { 120 return err 121 } 122 123 if resp.JSON200 == nil { 124 return errors.New("failed to retrieve generated types: " + string(resp.Body)) 125 } 126 127 fmt.Print(resp.JSON200.Types) 128 return nil 129 } 130 131 return nil 132 }