Skip to content

Commit 3124b65

Browse files
julienrbrtPantani
andauthored
fix(cosmosgen): fallback to local generation when possible (#4780)
* fix(cosmosgen): fallback to local generation when possible * cl * feedback * Update generate_typescript.go --------- Co-authored-by: Danilo Pantani <[email protected]>
1 parent 1b161f5 commit 3124b65

File tree

4 files changed

+82
-24
lines changed

4 files changed

+82
-24
lines changed

changelog.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
## Unreleased
44

5+
## [`v29.3.0`](https://github.com/ignite/cli/releases/tag/v29.3.0)
6+
57
### Features
68

79
- [#4786](https://github.com/ignite/cli/pull/4786) Add all types to the documentation and disclaimer for multiple coin types.
810

911
### Changes
1012

13+
- [#4780](https://github.com/ignite/cli/pull/4780) Fallback to local generation when possible in `generate ts-client` command.
1114
- [#4779](https://github.com/ignite/cli/pull/4779) Do not re-gen openapi spec each time the `ts-client` or the `composables` are generated.
1215
- [#4784](https://github.com/ignite/cli/pull/4784) Remove unused message initialization.
1316

@@ -910,7 +913,6 @@ Our new name is **Ignite CLI**!
910913
in [PR 1971](https://github.com/ignite/cli/pull/1971/files)
911914

912915
Updates are required if your chain uses these packages:
913-
914916
- `spm/ibckeeper` is now `pkg/cosmosibckeeper`
915917
- `spm/cosmoscmd` is now `pkg/cosmoscmd`
916918
- `spm/openapiconsole` is now `pkg/openapiconsole`

docs/docs/04-clients/02-typescript.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
description: Information about the generated TypeScript client code.
33
---
44

5-
# TypeScript frontend
5+
# TypeScript library
66

77
Ignite offers powerful functionality for generating client-side code for your
88
blockchain. Think of this as a one-click client SDK generation tailored
@@ -35,6 +35,17 @@ SDK modules:
3535
ignite generate ts-client --clear-cache
3636
```
3737

38+
:::tip
39+
In order to not rely on the remote `buf.build` service, you can install the
40+
`protoc-gen-ts_proto` binary locally and Ignite will use it instead of the remote plugin.
41+
42+
```sh
43+
npm install -g ts-proto
44+
```
45+
46+
Learn more at <https://github.com/stephenh/ts-proto>
47+
:::
48+
3849
Run a command to start your blockchain node:
3950

4051
```

ignite/cmd/generate_typescript_client.go

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,10 @@ import (
55

66
"github.com/ignite/cli/v29/ignite/pkg/cliui"
77
"github.com/ignite/cli/v29/ignite/pkg/cliui/icons"
8-
"github.com/ignite/cli/v29/ignite/pkg/errors"
98
"github.com/ignite/cli/v29/ignite/services/chain"
109
)
1110

12-
const (
13-
flagUseCache = "use-cache"
14-
msgBufAuth = "Generate ts-client uses a 'buf.build' remote plugin. Buf is begin limiting remote plugin requests from unauthenticated users on 'buf.build'. Intensively using this function will get you rate limited. Authenticate with 'buf registry login' to avoid this (https://buf.build/docs/generate/auth-required)."
15-
)
11+
const flagDisableCache = "disable-cache"
1612

1713
func NewGenerateTSClient() *cobra.Command {
1814
c := &cobra.Command{
@@ -41,7 +37,7 @@ changes when the blockchain is started with a flag:
4137

4238
c.Flags().AddFlagSet(flagSetYes())
4339
c.Flags().StringP(flagOutput, "o", "", "TypeScript client output path")
44-
c.Flags().Bool(flagUseCache, false, "use build cache to speed-up generation")
40+
c.Flags().Bool(flagDisableCache, false, "disable build cache")
4541

4642
return c
4743
}
@@ -53,16 +49,6 @@ func generateTSClientHandler(cmd *cobra.Command, _ []string) error {
5349
)
5450
defer session.End()
5551

56-
if !getYes(cmd) {
57-
if err := session.AskConfirm(msgBufAuth); err != nil {
58-
if errors.Is(err, cliui.ErrAbort) {
59-
return errors.New("buf not auth")
60-
}
61-
62-
return err
63-
}
64-
}
65-
6652
c, err := chain.NewWithHomeFlags(
6753
cmd,
6854
chain.WithOutputer(session),
@@ -79,14 +65,14 @@ func generateTSClientHandler(cmd *cobra.Command, _ []string) error {
7965
}
8066

8167
output, _ := cmd.Flags().GetString(flagOutput)
82-
useCache, _ := cmd.Flags().GetBool(flagUseCache)
68+
disableCache, _ := cmd.Flags().GetBool(flagDisableCache)
8369

8470
var opts []chain.GenerateTarget
8571
if flagGetEnableProtoVendor(cmd) {
8672
opts = append(opts, chain.GenerateProtoVendor())
8773
}
8874

89-
err = c.Generate(cmd.Context(), cacheStorage, chain.GenerateTSClient(output, useCache), opts...)
75+
err = c.Generate(cmd.Context(), cacheStorage, chain.GenerateTSClient(output, !disableCache), opts...)
9076
if err != nil {
9177
return err
9278
}

ignite/pkg/cosmosgen/generate_typescript.go

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"log"
66
"os"
7+
"os/exec"
78
"path/filepath"
89
"sort"
910
"strings"
@@ -17,10 +18,28 @@ import (
1718
"github.com/ignite/cli/v29/ignite/pkg/gomodulepath"
1819
)
1920

20-
var dirchangeCacheNamespace = "generate.typescript.dirchange"
21+
var (
22+
dirchangeCacheNamespace = "generate.typescript.dirchange"
23+
24+
protocGenTSProtoBin = "protoc-gen-ts_proto"
25+
26+
msgBufAuth = "Note: Buf is limits remote plugin requests from unauthenticated users on 'buf.build'. Intensively using this function will get you rate limited. Authenticate with 'buf registry login' to avoid this (https://buf.build/docs/generate/auth-required)."
27+
)
28+
29+
const localTSProtoTmpl = `version: v1
30+
plugins:
31+
- plugin: ts_proto
32+
out: .
33+
opt:
34+
- "esModuleInterop=true"
35+
- "forceLong=long"
36+
- "useOptionals=true"
37+
`
2138

2239
type tsGenerator struct {
23-
g *generator
40+
g *generator
41+
tsTemplateFile string
42+
isLocalProto bool
2443
}
2544

2645
type generatePayload struct {
@@ -29,7 +48,41 @@ type generatePayload struct {
2948
}
3049

3150
func newTSGenerator(g *generator) *tsGenerator {
32-
return &tsGenerator{g}
51+
tsg := &tsGenerator{g: g}
52+
if _, err := exec.LookPath(protocGenTSProtoBin); err == nil {
53+
tsg.isLocalProto = true
54+
}
55+
56+
if !tsg.isLocalProto {
57+
log.Printf("No '%s' binary found in PATH, using remote buf plugin for Typescript generation. %s\n", protocGenTSProtoBin, msgBufAuth)
58+
}
59+
60+
return tsg
61+
}
62+
63+
func (g *tsGenerator) tsTemplate() (string, error) {
64+
if !g.isLocalProto {
65+
return g.g.tsTemplate(), nil
66+
}
67+
if g.tsTemplateFile != "" {
68+
return g.tsTemplateFile, nil
69+
}
70+
f, err := os.CreateTemp("", "buf-gen-ts-*.yaml")
71+
if err != nil {
72+
return "", err
73+
}
74+
defer f.Close()
75+
if _, err := f.WriteString(localTSProtoTmpl); err != nil {
76+
return "", err
77+
}
78+
g.tsTemplateFile = f.Name()
79+
return g.tsTemplateFile, nil
80+
}
81+
82+
func (g *tsGenerator) cleanup() {
83+
if g.tsTemplateFile != "" {
84+
os.Remove(g.tsTemplateFile)
85+
}
3386
}
3487

3588
func (g *generator) tsTemplate() string {
@@ -56,6 +109,7 @@ func (g *generator) generateTS(ctx context.Context) error {
56109
})
57110

58111
tsg := newTSGenerator(g)
112+
defer tsg.cleanup()
59113
if err := tsg.generateModuleTemplates(ctx); err != nil {
60114
return err
61115
}
@@ -154,12 +208,17 @@ func (g *tsGenerator) generateModuleTemplate(
154208
}
155209
}
156210

211+
tsTemplate, err := g.tsTemplate()
212+
if err != nil {
213+
return err
214+
}
215+
157216
// code generate for each module.
158217
if err := g.g.buf.Generate(
159218
ctx,
160219
protoPath,
161220
typesOut,
162-
g.g.tsTemplate(),
221+
tsTemplate,
163222
cosmosbuf.IncludeWKT(),
164223
cosmosbuf.WithModuleName(m.Pkg.Name),
165224
); err != nil {

0 commit comments

Comments
 (0)