diff --git a/changelog.md b/changelog.md index 97c799b661..0dce3e6d3e 100644 --- a/changelog.md +++ b/changelog.md @@ -2,12 +2,15 @@ ## Unreleased +## [`v29.3.0`](https://github.com/ignite/cli/releases/tag/v29.3.0) + ### Features - [#4786](https://github.com/ignite/cli/pull/4786) Add all types to the documentation and disclaimer for multiple coin types. ### Changes +- [#4780](https://github.com/ignite/cli/pull/4780) Fallback to local generation when possible in `generate ts-client` command. - [#4779](https://github.com/ignite/cli/pull/4779) Do not re-gen openapi spec each time the `ts-client` or the `composables` are generated. - [#4784](https://github.com/ignite/cli/pull/4784) Remove unused message initialization. @@ -910,7 +913,6 @@ Our new name is **Ignite CLI**! in [PR 1971](https://github.com/ignite/cli/pull/1971/files) Updates are required if your chain uses these packages: - - `spm/ibckeeper` is now `pkg/cosmosibckeeper` - `spm/cosmoscmd` is now `pkg/cosmoscmd` - `spm/openapiconsole` is now `pkg/openapiconsole` diff --git a/docs/docs/04-clients/02-typescript.md b/docs/docs/04-clients/02-typescript.md index 13771fbfde..525cd6b764 100644 --- a/docs/docs/04-clients/02-typescript.md +++ b/docs/docs/04-clients/02-typescript.md @@ -2,7 +2,7 @@ description: Information about the generated TypeScript client code. --- -# TypeScript frontend +# TypeScript library Ignite offers powerful functionality for generating client-side code for your blockchain. Think of this as a one-click client SDK generation tailored @@ -35,6 +35,17 @@ SDK modules: ignite generate ts-client --clear-cache ``` +:::tip +In order to not rely on the remote `buf.build` service, you can install the +`protoc-gen-ts_proto` binary locally and Ignite will use it instead of the remote plugin. + +```sh +npm install -g ts-proto +``` + +Learn more at +::: + Run a command to start your blockchain node: ``` diff --git a/ignite/cmd/generate_typescript_client.go b/ignite/cmd/generate_typescript_client.go index e4efd79502..50c9fcf6fd 100644 --- a/ignite/cmd/generate_typescript_client.go +++ b/ignite/cmd/generate_typescript_client.go @@ -5,14 +5,10 @@ import ( "github.com/ignite/cli/v29/ignite/pkg/cliui" "github.com/ignite/cli/v29/ignite/pkg/cliui/icons" - "github.com/ignite/cli/v29/ignite/pkg/errors" "github.com/ignite/cli/v29/ignite/services/chain" ) -const ( - flagUseCache = "use-cache" - 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)." -) +const flagDisableCache = "disable-cache" func NewGenerateTSClient() *cobra.Command { c := &cobra.Command{ @@ -41,7 +37,7 @@ changes when the blockchain is started with a flag: 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") + c.Flags().Bool(flagDisableCache, false, "disable build cache") return c } @@ -53,16 +49,6 @@ func generateTSClientHandler(cmd *cobra.Command, _ []string) error { ) defer session.End() - if !getYes(cmd) { - if err := session.AskConfirm(msgBufAuth); err != nil { - if errors.Is(err, cliui.ErrAbort) { - return errors.New("buf not auth") - } - - return err - } - } - c, err := chain.NewWithHomeFlags( cmd, chain.WithOutputer(session), @@ -79,14 +65,14 @@ func generateTSClientHandler(cmd *cobra.Command, _ []string) error { } output, _ := cmd.Flags().GetString(flagOutput) - useCache, _ := cmd.Flags().GetBool(flagUseCache) + disableCache, _ := cmd.Flags().GetBool(flagDisableCache) var opts []chain.GenerateTarget if flagGetEnableProtoVendor(cmd) { opts = append(opts, chain.GenerateProtoVendor()) } - err = c.Generate(cmd.Context(), cacheStorage, chain.GenerateTSClient(output, useCache), opts...) + err = c.Generate(cmd.Context(), cacheStorage, chain.GenerateTSClient(output, !disableCache), opts...) if err != nil { return err } diff --git a/ignite/pkg/cosmosgen/generate_typescript.go b/ignite/pkg/cosmosgen/generate_typescript.go index 4dec5b2953..c6919d805a 100644 --- a/ignite/pkg/cosmosgen/generate_typescript.go +++ b/ignite/pkg/cosmosgen/generate_typescript.go @@ -4,6 +4,7 @@ import ( "context" "log" "os" + "os/exec" "path/filepath" "sort" "strings" @@ -17,10 +18,28 @@ import ( "github.com/ignite/cli/v29/ignite/pkg/gomodulepath" ) -var dirchangeCacheNamespace = "generate.typescript.dirchange" +var ( + dirchangeCacheNamespace = "generate.typescript.dirchange" + + protocGenTSProtoBin = "protoc-gen-ts_proto" + + 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)." +) + +const localTSProtoTmpl = `version: v1 +plugins: + - plugin: ts_proto + out: . + opt: + - "esModuleInterop=true" + - "forceLong=long" + - "useOptionals=true" +` type tsGenerator struct { - g *generator + g *generator + tsTemplateFile string + isLocalProto bool } type generatePayload struct { @@ -29,7 +48,41 @@ type generatePayload struct { } func newTSGenerator(g *generator) *tsGenerator { - return &tsGenerator{g} + tsg := &tsGenerator{g: g} + if _, err := exec.LookPath(protocGenTSProtoBin); err == nil { + tsg.isLocalProto = true + } + + if !tsg.isLocalProto { + log.Printf("No '%s' binary found in PATH, using remote buf plugin for Typescript generation. %s\n", protocGenTSProtoBin, msgBufAuth) + } + + return tsg +} + +func (g *tsGenerator) tsTemplate() (string, error) { + if !g.isLocalProto { + return g.g.tsTemplate(), nil + } + if g.tsTemplateFile != "" { + return g.tsTemplateFile, nil + } + f, err := os.CreateTemp("", "buf-gen-ts-*.yaml") + if err != nil { + return "", err + } + defer f.Close() + if _, err := f.WriteString(localTSProtoTmpl); err != nil { + return "", err + } + g.tsTemplateFile = f.Name() + return g.tsTemplateFile, nil +} + +func (g *tsGenerator) cleanup() { + if g.tsTemplateFile != "" { + os.Remove(g.tsTemplateFile) + } } func (g *generator) tsTemplate() string { @@ -56,6 +109,7 @@ func (g *generator) generateTS(ctx context.Context) error { }) tsg := newTSGenerator(g) + defer tsg.cleanup() if err := tsg.generateModuleTemplates(ctx); err != nil { return err } @@ -154,12 +208,17 @@ func (g *tsGenerator) generateModuleTemplate( } } + tsTemplate, err := g.tsTemplate() + if err != nil { + return err + } + // code generate for each module. if err := g.g.buf.Generate( ctx, protoPath, typesOut, - g.g.tsTemplate(), + tsTemplate, cosmosbuf.IncludeWKT(), cosmosbuf.WithModuleName(m.Pkg.Name), ); err != nil {