-
Notifications
You must be signed in to change notification settings - Fork 532
/
generate_typescript_client.go
80 lines (61 loc) · 1.93 KB
/
generate_typescript_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package ignitecmd
import (
"github.com/spf13/cobra"
"github.com/ignite/cli/v29/ignite/pkg/cliui"
"github.com/ignite/cli/v29/ignite/pkg/cliui/icons"
"github.com/ignite/cli/v29/ignite/services/chain"
)
const (
flagUseCache = "use-cache"
)
func NewGenerateTSClient() *cobra.Command {
c := &cobra.Command{
Use: "ts-client",
Short: "TypeScript frontend client",
Long: `Generate a framework agnostic TypeScript client for your blockchain project.
By default the TypeScript client is generated in the "ts-client/" directory. You
can customize the output directory in config.yml:
client:
typescript:
path: new-path
Output can also be customized by using a flag:
ignite generate ts-client --output new-path
TypeScript client code can be automatically regenerated on reset or source code
changes when the blockchain is started with a flag:
ignite chain serve --generate-clients
`,
RunE: generateTSClientHandler,
}
c.Flags().AddFlagSet(flagSetYes())
c.Flags().StringP(flagOutput, "o", "", "TypeScript client output path")
c.Flags().Bool(flagUseCache, false, "use build cache to speed-up generation")
return c
}
func generateTSClientHandler(cmd *cobra.Command, _ []string) error {
session := cliui.New(cliui.StartSpinnerWithText(statusGenerating))
defer session.End()
c, err := chain.NewWithHomeFlags(
cmd,
chain.WithOutputer(session),
chain.CollectEvents(session.EventBus()),
chain.PrintGeneratedPaths(),
)
if err != nil {
return err
}
cacheStorage, err := newCache(cmd)
if err != nil {
return err
}
output, _ := cmd.Flags().GetString(flagOutput)
useCache, _ := cmd.Flags().GetBool(flagUseCache)
var opts []chain.GenerateTarget
if flagGetEnableProtoVendor(cmd) {
opts = append(opts, chain.GenerateProtoVendor())
}
err = c.Generate(cmd.Context(), cacheStorage, chain.GenerateTSClient(output, useCache), opts...)
if err != nil {
return err
}
return session.Println(icons.OK, "Generated Typescript Client")
}