Skip to content

Commit

Permalink
feat: Add better CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
simse committed Sep 13, 2024
1 parent 416c68b commit 656dcf5
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 52 deletions.
3 changes: 2 additions & 1 deletion examples/projects/monorepo/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
baseTypes.ts
baseTypes.ts
introspection.json
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,28 @@ module github.com/simse/faster-graphql-codegen
go 1.23.1

require (
github.com/VividCortex/ewma v1.2.0 // indirect
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d // indirect
github.com/agnivade/levenshtein v1.1.1 // indirect
github.com/briandowns/spinner v1.23.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dlclark/regexp2 v1.11.4 // indirect
github.com/dop251/goja v0.0.0-20240828124009-016eb7256539 // indirect
github.com/fatih/color v1.7.0 // indirect
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect
github.com/google/pprof v0.0.0-20230207041349-798e818bf904 // indirect
github.com/iancoleman/strcase v0.3.0 // indirect
github.com/lmittmann/tint v1.0.5 // indirect
github.com/mattn/go-colorable v0.1.2 // indirect
github.com/mattn/go-isatty v0.0.8 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/vbauerster/mpb/v8 v8.8.3 // indirect
github.com/vektah/gqlparser/v2 v2.5.16 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/term v0.1.0 // indirect
golang.org/x/text v0.18.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
24 changes: 24 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
github.com/VividCortex/ewma v1.2.0 h1:f58SaIzcDXrSy3kWaHNvuJgJ3Nmz59Zji6XoJR/q1ow=
github.com/VividCortex/ewma v1.2.0/go.mod h1:nz4BbCtbLyFDeC9SUHbtcT5644juEuWfUAUnGx7j5l4=
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpHMqeKTCYkitsPqHNxTmd4SNR5r94FGM8=
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d/go.mod h1:asat636LX7Bqt5lYEZ27JNDcqxfjdBQuJ/MM4CN/Lzo=
github.com/agnivade/levenshtein v1.1.1 h1:QY8M92nrzkmr798gCo3kmMyqXFzdQVpxLlGPRBij0P8=
github.com/agnivade/levenshtein v1.1.1/go.mod h1:veldBMzWxcCG2ZvUTKD2kJNRdCk5hVbJomOvKkmgYbo=
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0/go.mod h1:t2tdKJDJF9BV14lnkjHmOQgcvEKgtqs5a1N3LNdJhGE=
github.com/briandowns/spinner v1.23.1 h1:t5fDPmScwUjozhDj4FA46p5acZWIPXYE30qW2Ptu650=
github.com/briandowns/spinner v1.23.1/go.mod h1:LaZeM4wm2Ywy6vO571mvhQNRcWfRUnXOs0RcKV0wYKM=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48/go.mod h1:if7Fbed8SFyPtHLHbg49SI7NAdJiC5WIA09pe59rfAA=
github.com/dlclark/regexp2 v1.11.4 h1:rPYF9/LECdNymJufQKmri9gV604RvvABwgOA8un7yAo=
github.com/dlclark/regexp2 v1.11.4/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/dop251/goja v0.0.0-20240828124009-016eb7256539 h1:YIxvsQAoCLGScK2c9ag+4sFCgiQFpMzywJG6dQZFu9k=
github.com/dop251/goja v0.0.0-20240828124009-016eb7256539/go.mod h1:MxLav0peU43GgvwVgNbLAj1s/bSGboKkhuULvq/7hx4=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/go-sourcemap/sourcemap v2.1.3+incompatible h1:W1iEw64niKVGogNgBN3ePyLFfuisuzeidWPMPWmECqU=
github.com/go-sourcemap/sourcemap v2.1.3+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg=
github.com/google/pprof v0.0.0-20230207041349-798e818bf904 h1:4/hN5RUoecvl+RmJRE2YxKWtnnQls6rQjjW5oV7qg2U=
Expand All @@ -16,12 +24,28 @@ github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSAS
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/lmittmann/tint v1.0.5 h1:NQclAutOfYsqs2F1Lenue6OoWCajs5wJcP3DfWVpePw=
github.com/lmittmann/tint v1.0.5/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE=
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/vbauerster/mpb/v8 v8.8.3 h1:dTOByGoqwaTJYPubhVz3lO5O6MK553XVgUo33LdnNsQ=
github.com/vbauerster/mpb/v8 v8.8.3/go.mod h1:JfCCrtcMsJwP6ZwMn9e5LMnNyp3TVNpUWWkN+nd4EWk=
github.com/vektah/gqlparser/v2 v2.5.16 h1:1gcmLTvs3JLKXckwCwlUagVn/IlV2bwqle0vJ0vy5p8=
github.com/vektah/gqlparser/v2 v2.5.16/go.mod h1:1lz1OeCqgQbQepsGxPVywrjdBHW2T08PUS3pJqepRww=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.1.0 h1:g6Z6vPFA9dYBAF7DWcH6sCcOntplXsDKcliusYijMlw=
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
19 changes: 15 additions & 4 deletions internal/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,19 @@ type Project struct {
config Config
}

func FindProjects(rootDir string, walkDir func(string, fs.WalkDirFunc) error) []Project {
func FindProjects(rootDir string, walkDir func(string, fs.WalkDirFunc) error) ([]Project, error) {
// check if path exists
if _, err := os.Stat(rootDir); err != nil {
return nil, err
}

var projects []Project

err := walkDir(rootDir, func(path string, d fs.DirEntry, err error) error {
if d.IsDir() && d.Name() == "node_modules" {
return fs.SkipDir
}

if strings.HasSuffix(path, "codegen.ts") || strings.HasSuffix(path, "codegen.yml") {
project := Project{
RootDir: filepath.Dir(path),
Expand All @@ -40,10 +49,10 @@ func FindProjects(rootDir string, walkDir func(string, fs.WalkDirFunc) error) []
return nil
})
if err != nil {
panic(err)
return nil, err
}

return projects
return projects, nil
}

/*
Expand Down Expand Up @@ -82,7 +91,7 @@ func (e *ExecutionContext) GetSchema(key string) *ast.Schema {
/*
LoadSchemas will find every project with a unique list of schemas and load those to cache.
*/
func (e *ExecutionContext) LoadSchemas() {
func (e *ExecutionContext) LoadSchemas() int {
// find unique schemas
var uniqueSchemas []string
var projectsToLoad []Project
Expand Down Expand Up @@ -118,6 +127,8 @@ func (e *ExecutionContext) LoadSchemas() {
}

wg.Wait()

return len(uniqueSchemas)
}

func (e *ExecutionContext) Execute() {
Expand Down
110 changes: 63 additions & 47 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,62 +1,78 @@
package main

import (
"github.com/lmittmann/tint"
"errors"
"fmt"
"github.com/briandowns/spinner"
"github.com/simse/faster-graphql-codegen/internal"
"log/slog"
"os"
"path/filepath"
"time"
)

func main() {
// set up coloured logging
w := os.Stderr

// set global logger with custom options
slog.SetDefault(slog.New(
tint.NewHandler(w, &tint.Options{
Level: slog.LevelDebug,
TimeFormat: time.TimeOnly,
}),
))

/*timeStart := time.Now()
schema, err := internal.LoadSchema("examples/github/github.graphql")
if err != nil {
panic(err)
}
loadSchemaTime := time.Since(timeStart)
convertTimeStart := time.Now()
output := strings.Builder{}
internal.ConvertSchema(schema, &output)
convertSchemaTime := time.Since(convertTimeStart)
writeTypesTimeStart := time.Now()
outputFile, openErr := os.Create("output/github/baseTypes.ts")
if openErr != nil {
panic(err)
}
_, writeErr := outputFile.WriteString(output.String())
if writeErr != nil {
panic(err)
}
writeTypesTime := time.Since(writeTypesTimeStart)
outputFile.Close()
slog.Info("finished codegen", "duration", time.Since(timeStart), "load_duration", loadSchemaTime, "convert_duration", convertSchemaTime, "write_duration", writeTypesTime)*/
timeStart := time.Now()
projects := internal.FindProjects("./examples/projects/monorepo", filepath.WalkDir)
slog.Info("search for packages using codegen complete", "found_projects", len(projects))

/*for _, project := range projects {
internal.ExecuteProject(project)
}*/
projects := findProjects()

executionContext := internal.ExecutionContext{}
executionContext.SetProjects(projects)
executionContext.LoadSchemas()
executionContext.Execute()
slog.Info("finished all codegen tasks", "duration", time.Since(timeStart))

loadSchemas(&executionContext)
execute(&executionContext, timeStart)
}

func findProjects() []internal.Project {
// get input folder
searchFolder := "."
argsWithoutProg := os.Args[1:]

if len(argsWithoutProg) > 0 {
searchFolder = argsWithoutProg[0]
}

s := spinner.New(spinner.CharSets[14], 100*time.Millisecond, spinner.WithWriter(os.Stderr))
s.Suffix = " Finding projects using codegen"

s.Start()
projects, err := internal.FindProjects(searchFolder, filepath.WalkDir)

if err != nil {
if errors.Is(err, os.ErrNotExist) {
s.FinalMSG = "✗ Input folder does not exist\n"
s.Stop()
} else {
s.FinalMSG = "✗ Unknown error while searching for projects: " + err.Error() + "\n"
s.Stop()
}

os.Exit(1)
} else {
s.FinalMSG = fmt.Sprintf("✓ Found %d projects\n", len(projects))
s.Stop()
}

return projects
}

func loadSchemas(e *internal.ExecutionContext) {
s := spinner.New(spinner.CharSets[14], 100*time.Millisecond, spinner.WithWriter(os.Stderr))
s.Suffix = " Loading graphql schemas"

s.Start()
schemasLoaded := e.LoadSchemas()

s.FinalMSG = fmt.Sprintf("✓ Loaded %d unique schemas\n", schemasLoaded)
s.Stop()
}

func execute(e *internal.ExecutionContext, timeStart time.Time) {
s := spinner.New(spinner.CharSets[14], 100*time.Millisecond, spinner.WithWriter(os.Stderr))
s.Suffix = " Executing codegen tasks"

s.Start()
e.Execute()

s.FinalMSG = fmt.Sprintf("✓ Codegen completed in %s\n", time.Since(timeStart).String())
s.Stop()
}

0 comments on commit 656dcf5

Please sign in to comment.