gen.go 953 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package tsgen
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/logrusorgru/aurora"
  6. "github.com/zeromicro/go-zero/core/logx"
  7. "github.com/zeromicro/go-zero/tools/goctl/api/parser"
  8. "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
  9. "github.com/urfave/cli"
  10. )
  11. // TsCommand provides the entry to generate typescript codes
  12. func TsCommand(c *cli.Context) error {
  13. apiFile := c.String("api")
  14. dir := c.String("dir")
  15. webAPI := c.String("webapi")
  16. caller := c.String("caller")
  17. unwrapAPI := c.Bool("unwrap")
  18. if len(apiFile) == 0 {
  19. return errors.New("missing -api")
  20. }
  21. if len(dir) == 0 {
  22. return errors.New("missing -dir")
  23. }
  24. api, err := parser.Parse(apiFile)
  25. if err != nil {
  26. fmt.Println(aurora.Red("Failed"))
  27. return err
  28. }
  29. api.Service = api.Service.JoinPrefix()
  30. logx.Must(pathx.MkdirIfNotExist(dir))
  31. logx.Must(genHandler(dir, webAPI, caller, api, unwrapAPI))
  32. logx.Must(genComponents(dir, api))
  33. fmt.Println(aurora.Green("Done."))
  34. return nil
  35. }