2020-07-29 17:11:41 +08:00
|
|
|
package tsgen
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/logrusorgru/aurora"
|
2022-05-07 15:40:11 +08:00
|
|
|
"github.com/spf13/cobra"
|
2022-01-25 23:15:07 +08:00
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
"github.com/zeromicro/go-zero/tools/goctl/api/parser"
|
|
|
|
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
2020-07-29 17:11:41 +08:00
|
|
|
)
|
|
|
|
|
2022-05-07 15:40:11 +08:00
|
|
|
var (
|
|
|
|
// VarStringDir describes a directory.
|
|
|
|
VarStringDir string
|
|
|
|
// VarStringAPI describes an API file.
|
|
|
|
VarStringAPI string
|
|
|
|
// VarStringWebAPI describes a web API file.
|
|
|
|
VarStringWebAPI string
|
|
|
|
// VarStringCaller describes a caller.
|
|
|
|
VarStringCaller string
|
|
|
|
// VarBoolUnWrap describes whether wrap or not.
|
|
|
|
VarBoolUnWrap bool
|
|
|
|
)
|
|
|
|
|
2021-03-08 18:23:12 +08:00
|
|
|
// TsCommand provides the entry to generate typescript codes
|
2022-05-07 15:40:11 +08:00
|
|
|
func TsCommand(_ *cobra.Command, _ []string) error {
|
|
|
|
apiFile := VarStringAPI
|
|
|
|
dir := VarStringDir
|
|
|
|
webAPI := VarStringWebAPI
|
|
|
|
caller := VarStringCaller
|
|
|
|
unwrapAPI := VarBoolUnWrap
|
2020-07-29 17:11:41 +08:00
|
|
|
if len(apiFile) == 0 {
|
|
|
|
return errors.New("missing -api")
|
|
|
|
}
|
2021-01-11 15:10:51 +08:00
|
|
|
|
2020-07-29 17:11:41 +08:00
|
|
|
if len(dir) == 0 {
|
|
|
|
return errors.New("missing -dir")
|
|
|
|
}
|
|
|
|
|
2021-01-11 15:10:51 +08:00
|
|
|
api, err := parser.Parse(apiFile)
|
2020-07-29 17:11:41 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(aurora.Red("Failed"))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-21 15:22:43 +08:00
|
|
|
if err := api.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-03 20:57:03 +08:00
|
|
|
api.Service = api.Service.JoinPrefix()
|
2022-01-03 21:32:40 +08:00
|
|
|
logx.Must(pathx.MkdirIfNotExist(dir))
|
2021-02-26 16:11:47 +08:00
|
|
|
logx.Must(genHandler(dir, webAPI, caller, api, unwrapAPI))
|
2020-08-14 15:08:06 +08:00
|
|
|
logx.Must(genComponents(dir, api))
|
2020-07-29 17:11:41 +08:00
|
|
|
|
|
|
|
fmt.Println(aurora.Green("Done."))
|
|
|
|
return nil
|
|
|
|
}
|