Skip to content

Commit

Permalink
Change cache directory to .gossr, move dir logic to cache pkg, use fr…
Browse files Browse the repository at this point in the history
…ee port for hot reload, simplify config
  • Loading branch information
natewong1313 committed Jan 16, 2024
1 parent 960cd71 commit 7478710
Show file tree
Hide file tree
Showing 19 changed files with 450 additions and 471 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ tmp

.idea

generated.d.ts
generated.d.ts
.gossr
97 changes: 56 additions & 41 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,76 @@ package go_ssr

import (
"fmt"
"github.com/natewong1313/go-react-ssr/internal/utils"
"os"
"path"
"path/filepath"

"github.com/natewong1313/go-react-ssr/internal/utils"
)

// Config is the config for starting the engine
type Config struct {
AppEnv string // "production" or "development"
AssetRoute string // The route to serve assets from, e.g. "/assets"
FrontendDir string // The path to the frontend folder, where your React app lives
GeneratedTypesPath string // The path to the generated types file
PropsStructsPath string // The path to the Go structs file, the structs will be generated to TS types
LayoutFilePath string // The path to the layout file, relative to the frontend dir
LayoutCSSFilePath string // The path to the layout css file, relative to the frontend dir
TailwindConfigPath string // The path to the tailwind config file
HotReloadServerPort int // The port to run the hot reload server on, 3001 by default
AppEnv string // "production" or "development"
AssetRoute string // The route where assets are served from on your server, default: /assets
PropsStructsPath string // The path to the Go structs file, the structs will be generated to TS types
FrontendSrcDir string // The path to the frontend src folder, where your React app lives
LayoutFile string // The path to the layout file, relative to the frontend dir
TailwindEnabled bool
}

// Validate validates the config
func (c *Config) Validate() error {
if !checkPathExists(c.FrontendDir) {
return fmt.Errorf("frontend dir ar %s does not exist", c.FrontendDir)
}
if os.Getenv("APP_ENV") != "production" && !checkPathExists(c.PropsStructsPath) {
return fmt.Errorf("props structs path at %s does not exist", c.PropsStructsPath)
}
if c.LayoutFilePath != "" && !checkPathExists(path.Join(c.FrontendDir, c.LayoutFilePath)) {
return fmt.Errorf("layout css file path at %s/%s does not exist", c.FrontendDir, c.LayoutCSSFilePath)
}
if c.LayoutCSSFilePath != "" && !checkPathExists(path.Join(c.FrontendDir, c.LayoutCSSFilePath)) {
return fmt.Errorf("layout css file path at %s/%s does not exist", c.FrontendDir, c.LayoutCSSFilePath)
}
if c.TailwindConfigPath != "" && c.LayoutCSSFilePath == "" {
return fmt.Errorf("layout css file path must be provided when using tailwind")
type FrontendConfig struct {
Path string // The path to the frontend folder, where your React app lives
GeneratedTypesPath string // The path where the generated types file will be created, relative to the frontend dir
LayoutFile string // The path to the layout file, relative to the frontend dir
TailwindEnabled bool // Whether to use tailwind or not
}

func NewDefaultConfig() Config {
return Config{
AppEnv: "development",
AssetRoute: "/assets",
PropsStructsPath: "./models/props.go",
FrontendSrcDir: "../frontend-tailwind/src",
}
if c.HotReloadServerPort == 0 {
c.HotReloadServerPort = 3001
}

// WithTailwind sets the config to use tailwind
func (c Config) WithTailwind() Config {
c.TailwindEnabled = true
return c
}

// WithLayout sets the layout file path
func (c Config) WithLayout(layoutFileName string) Config {
c.LayoutFile = filepath.Join(c.FrontendSrcDir, layoutFileName)
return c
}

// Validate validates the config
func (c *Config) validate() error {
// if !checkPathExists(c.FrontendDir) {
// return fmt.Errorf("frontend dir ar %s does not exist", c.FrontendDir)
// }
// if os.Getenv("APP_ENV") != "production" && !checkPathExists(c.PropsStructsPath) {
// return fmt.Errorf("props structs path at %s does not exist", c.PropsStructsPath)
// }
// if c.LayoutFilePath != "" && !checkPathExists(path.Join(c.FrontendDir, c.LayoutFilePath)) {
// return fmt.Errorf("layout css file path at %s/%s does not exist", c.FrontendDir, c.LayoutFilePath)
// }
if c.TailwindEnabled && !checkPathExists(path.Join(c.FrontendSrcDir, "../tailwind.config.js")) {
return fmt.Errorf("tailwind config file at %s not found", path.Join(c.FrontendSrcDir, "../tailwind.config.js"))
}
c.setFilePaths()
c.formatFilePaths()
return nil
}

// setFilePaths sets any paths in the config to their absolute paths
func (c *Config) setFilePaths() {
c.FrontendDir = utils.GetFullFilePath(c.FrontendDir)
c.GeneratedTypesPath = utils.GetFullFilePath(c.GeneratedTypesPath)
// formatFilePaths sets any paths in the config to their absolute paths, fixes an issue on windows with paths
func (c *Config) formatFilePaths() {
c.FrontendSrcDir = utils.GetFullFilePath(c.FrontendSrcDir)
// c.GeneratedTypesPath = utils.GetFullFilePath(c.GeneratedTypesPath)
c.PropsStructsPath = utils.GetFullFilePath(c.PropsStructsPath)
if c.LayoutFilePath != "" {
c.LayoutFilePath = path.Join(c.FrontendDir, c.LayoutFilePath)
}
if c.LayoutCSSFilePath != "" {
c.LayoutCSSFilePath = path.Join(c.FrontendDir, c.LayoutCSSFilePath)
}
if c.TailwindConfigPath != "" {
c.TailwindConfigPath = utils.GetFullFilePath(c.TailwindConfigPath)
if c.LayoutFile != "" {
c.LayoutFile = utils.GetFullFilePath(c.LayoutFile)
}
}

Expand Down
155 changes: 0 additions & 155 deletions css.go

This file was deleted.

100 changes: 49 additions & 51 deletions css_test.go
Original file line number Diff line number Diff line change
@@ -1,55 +1,53 @@
package go_ssr

import (
"os"
"testing"
// import (
// "os"
// "testing"

"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
)
// "github.com/rs/zerolog"
// "github.com/stretchr/testify/assert"
// )

func TestEngine_BuildLayoutCSSFile(t *testing.T) {
type test struct {
name string
shouldContain string
config *Config
}
originalContents, err := os.ReadFile("./examples/frontend/src/Home.css")
assert.Nil(t, err, "ReadFile should not return an error")
tests := []test{
{
name: "should clone layout css file",
shouldContain: string(originalContents),
config: &Config{
AppEnv: "production",
FrontendDir: "./examples/frontend/src",
LayoutCSSFilePath: "Home.css",
},
},
{
name: "should build layout css file with tailwind",
shouldContain: "tailwindcss",
config: &Config{
AppEnv: "production",
FrontendDir: "./examples/frontend-tailwind/src",
LayoutCSSFilePath: "Main.css",
TailwindConfigPath: "./examples/frontend-tailwind/tailwind.config.js",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.config.setFilePaths()
engine := &Engine{
Logger: zerolog.New(zerolog.ConsoleWriter{Out: os.Stderr}).With().Timestamp().Logger(),
Config: tt.config,
}
err = engine.BuildLayoutCSSFile()
assert.Nil(t, err, "BuildLayoutCSSFile should not return an error, got %v", err)
assert.NotNilf(t, engine.CachedLayoutCSSFilePath, "CachedLayoutCSSFilePath should not be nil")
contents, err := os.ReadFile(engine.CachedLayoutCSSFilePath)
assert.Nil(t, err, "ReadFile should not return an error, got %v", err)
assert.Contains(t, string(contents), tt.shouldContain)
})
}
}
// func TestEngine_BuildLayoutCSSFile(t *testing.T) {
// type test struct {
// name string
// shouldContain string
// config *Config
// }
// originalContents, err := os.ReadFile("./examples/frontend/src/Home.css")
// assert.Nil(t, err, "ReadFile should not return an error")
// tests := []test{
// {
// name: "should clone layout css file",
// shouldContain: string(originalContents),
// config: &Config{
// AppEnv: "production",
// FrontendDir: "./examples/frontend/src",
// },
// },
// {
// name: "should build layout css file with tailwind",
// shouldContain: "tailwindcss",
// config: &Config{
// AppEnv: "production",
// FrontendDir: "./examples/frontend-tailwind/src",
// TailwindConfigPath: "./examples/frontend-tailwind/tailwind.config.js",
// },
// },
// }
// for _, tt := range tests {
// t.Run(tt.name, func(t *testing.T) {
// tt.config.setFilePaths()
// engine := &Engine{
// Logger: zerolog.New(zerolog.ConsoleWriter{Out: os.Stderr}).With().Timestamp().Logger(),
// Config: tt.config,
// }
// err = engine.BuildLayoutCSSFile()
// assert.Nil(t, err, "BuildLayoutCSSFile should not return an error, got %v", err)
// assert.NotNilf(t, engine.CachedLayoutCSSFilePath, "CachedLayoutCSSFilePath should not be nil")
// contents, err := os.ReadFile(engine.CachedLayoutCSSFilePath)
// assert.Nil(t, err, "ReadFile should not return an error, got %v", err)
// assert.Contains(t, string(contents), tt.shouldContain)
// })
// }
// }
Loading

0 comments on commit 7478710

Please sign in to comment.