From 07f6c619ea772b0de0e31bfaf316260416917d0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jer=C3=B3nimo=20Albi?= Date: Wed, 17 Aug 2022 18:22:47 +0200 Subject: [PATCH] fix: change JS generator to use a single typescript API generator binary (#2747) * fix: change JS generator to use a single typescript API generator binary * chore: simplify swagger typescript API generate call arguments The module name index argument is not being used anywhere so this change removes it from the Generate call arguments to simplify the API. --- ignite/pkg/cosmosgen/generate_javascript.go | 19 +++++-- ignite/pkg/nodetime/programs/sta/sta.go | 58 ++++++++++++++++++--- 2 files changed, 66 insertions(+), 11 deletions(-) diff --git a/ignite/pkg/cosmosgen/generate_javascript.go b/ignite/pkg/cosmosgen/generate_javascript.go index 941a5bda3d..d00b90babe 100644 --- a/ignite/pkg/cosmosgen/generate_javascript.go +++ b/ignite/pkg/cosmosgen/generate_javascript.go @@ -71,6 +71,13 @@ func (g *jsGenerator) generateModules() error { defer cleanupPlugin() + staCmd, cleanupSTA, err := sta.Command() + if err != nil { + return err + } + + defer cleanupSTA() + gg := &errgroup.Group{} dirCache := cache.New[[]byte](g.g.cacheStorage, dirchangeCacheNamespace) @@ -89,7 +96,7 @@ func (g *jsGenerator) generateModules() error { return nil } - err = g.generateModule(g.g.ctx, protocCmd, tsprotoPluginPath, sourcePath, m) + err = g.generateModule(g.g.ctx, protocCmd, staCmd, tsprotoPluginPath, sourcePath, m) if err != nil { return err } @@ -113,7 +120,8 @@ func (g *jsGenerator) generateModules() error { // generateModule generates generates JS code for a module. func (g *jsGenerator) generateModule( ctx context.Context, - cmd protoc.Cmd, + protocCmd protoc.Cmd, + staCmd sta.Cmd, tsprotoPluginPath, appPath string, m module.Module, ) error { @@ -141,7 +149,7 @@ func (g *jsGenerator) generateModule( tsOut, protoc.Plugin(tsprotoPluginPath, "--ts_proto_opt=snakeToCamel=false"), protoc.Env("NODE_OPTIONS="), // unset nodejs options to avoid unexpected issues with vercel "pkg" - protoc.WithCommand(cmd), + protoc.WithCommand(protocCmd), ) if err != nil { return err @@ -160,7 +168,7 @@ func (g *jsGenerator) generateModule( m.Pkg.Path, includePaths, jsOpenAPIOut, - protoc.WithCommand(cmd), + protoc.WithCommand(protocCmd), ) if err != nil { return err @@ -172,7 +180,8 @@ func (g *jsGenerator) generateModule( outREST = filepath.Join(out, "rest.ts") ) - if err := sta.Generate(g.g.ctx, outREST, srcspec, "-1"); err != nil { // -1 removes the route namespace. + err = sta.Generate(g.g.ctx, outREST, srcspec, sta.WithCommand(staCmd)) + if err != nil { return err } diff --git a/ignite/pkg/nodetime/programs/sta/sta.go b/ignite/pkg/nodetime/programs/sta/sta.go index 272e989c41..9e15dc976f 100644 --- a/ignite/pkg/nodetime/programs/sta/sta.go +++ b/ignite/pkg/nodetime/programs/sta/sta.go @@ -9,13 +9,59 @@ import ( "github.com/ignite/cli/ignite/pkg/nodetime" ) +// Option configures Generate configs. +type Option func(*configs) + +// Configs holds Generate configs. +type configs struct { + command Cmd +} + +// WithCommand assigns a typescript API generator command to use for code generation. +// This allows to use a single nodetime STA generator binary in multiple code generation +// calls. Otherwise `Generate` creates a new generator binary each time it is called. +func WithCommand(command Cmd) Option { + return func(c *configs) { + c.command = command + } +} + +// Cmd contains the information necessary to execute the typescript API generator command. +type Cmd struct { + command []string +} + +// Command returns the strings to execute the typescript API generator command. +func (c Cmd) Command() []string { + return c.command +} + +// Command sets the typescript API generator binary up and returns the command needed to execute it. +func Command() (command Cmd, cleanup func(), err error) { + c, cleanup, err := nodetime.Command(nodetime.CommandSTA) + command = Cmd{c} + return +} + // Generate generates client code and TS types to outPath from an OpenAPI spec that resides at specPath. -func Generate(ctx context.Context, outPath, specPath, moduleNameIndex string) error { - command, cleanup, err := nodetime.Command(nodetime.CommandSTA) - if err != nil { - return err +func Generate(ctx context.Context, outPath, specPath string, options ...Option) error { + c := configs{} + + for _, o := range options { + o(&c) + } + + command := c.command.Command() + if command == nil { + cmd, cleanup, err := Command() + if err != nil { + return err + } + + defer cleanup() + + command = cmd.Command() } - defer cleanup() dir := filepath.Dir(outPath) file := filepath.Base(outPath) @@ -23,7 +69,7 @@ func Generate(ctx context.Context, outPath, specPath, moduleNameIndex string) er // command constructs the sta command. command = append(command, []string{ "--module-name-index", - moduleNameIndex, + "-1", // -1 removes the route namespace "-p", specPath, "-o",