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",