You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/tools/goctl/api/tsgen/gen.go

60 lines
1.3 KiB
Go

4 years ago
package tsgen
import (
"errors"
"fmt"
"github.com/logrusorgru/aurora"
"github.com/spf13/cobra"
"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"
4 years ago
)
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
)
// TsCommand provides the entry to generate typescript codes
func TsCommand(_ *cobra.Command, _ []string) error {
apiFile := VarStringAPI
dir := VarStringDir
webAPI := VarStringWebAPI
caller := VarStringCaller
unwrapAPI := VarBoolUnWrap
4 years ago
if len(apiFile) == 0 {
return errors.New("missing -api")
}
4 years ago
if len(dir) == 0 {
return errors.New("missing -dir")
}
api, err := parser.Parse(apiFile)
4 years ago
if err != nil {
fmt.Println(aurora.Red("Failed"))
return err
}
if err := api.Validate(); err != nil {
return err
}
api.Service = api.Service.JoinPrefix()
logx.Must(pathx.MkdirIfNotExist(dir))
logx.Must(genHandler(dir, webAPI, caller, api, unwrapAPI))
logx.Must(genComponents(dir, api))
4 years ago
fmt.Println(aurora.Green("Done."))
return nil
}