Skip to content

Commit

Permalink
fix: change JS generator to use a single typescript API generator bin…
Browse files Browse the repository at this point in the history
…ary (ignite#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.
  • Loading branch information
jeronimoalbi authored Aug 17, 2022
1 parent 4a7d6dc commit 07f6c61
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 11 deletions.
19 changes: 14 additions & 5 deletions ignite/pkg/cosmosgen/generate_javascript.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -160,7 +168,7 @@ func (g *jsGenerator) generateModule(
m.Pkg.Path,
includePaths,
jsOpenAPIOut,
protoc.WithCommand(cmd),
protoc.WithCommand(protocCmd),
)
if err != nil {
return err
Expand All @@ -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
}

Expand Down
58 changes: 52 additions & 6 deletions ignite/pkg/nodetime/programs/sta/sta.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,67 @@ 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)

// command constructs the sta command.
command = append(command, []string{
"--module-name-index",
moduleNameIndex,
"-1", // -1 removes the route namespace
"-p",
specPath,
"-o",
Expand Down

0 comments on commit 07f6c61

Please sign in to comment.