Skip to content

Commit

Permalink
feat: enable hot reload when only using --watch flag (#42)
Browse files Browse the repository at this point in the history
Closes: WORLD-858

## Overview

Make world cardinal dev only hot reloads when use with --watch flag

## Brief Changelog

- add `--watch` flag to `devCmd`
- ignore root directory when `--watch` flag is false

## Testing and Verifying

running both with `--watch` and without `--watch` flag
  • Loading branch information
zulkhair committed Feb 28, 2024
1 parent f6a7179 commit 441082c
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions cmd/world/cardinal/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import (
const (
CardinalPort = "3333"
RedisPort = "6379"

// flagWatch : Flag for hot reload support
flagWatch = "watch"
)

// StopChan is used to signal graceful shutdown
Expand All @@ -30,16 +33,26 @@ var StopChan = make(chan struct{})
// Cobra Setup //
/////////////////

func init() {
devCmd.Flags().Bool(flagWatch, false, "Dev mode with hot reload support")
}

// devCmd runs Cardinal in development mode
// Usage: `world cardinal dev`
var devCmd = &cobra.Command{
Use: "dev",
Short: "Run Cardinal in development mode",
Long: `Run Cardinal in development mode`,
RunE: func(cmd *cobra.Command, args []string) error {
watch, _ := cmd.Flags().GetBool(flagWatch)
logger.SetDebugMode(cmd)

fmt.Print(style.CLIHeader("Cardinal", "Running Cardinal in dev mode, cardinal supports hot reloading"), "\n")
startingMessage := "Running Cardinal in dev mode"
if watch {
startingMessage += " with hot reload support"
}

fmt.Print(style.CLIHeader("Cardinal", startingMessage), "\n")
fmt.Println(style.BoldText.Render("Press Ctrl+C to stop"))
fmt.Println()
fmt.Println(fmt.Sprintf("Redis: localhost:%s", RedisPort))
Expand Down Expand Up @@ -74,7 +87,7 @@ var devCmd = &cobra.Command{
signal.Notify(signalCh, syscall.SIGINT, syscall.SIGTERM)

// Run Cardinal Preparation
err = runCardinalPrep()
err = runCardinalPrep(watch)
if err != nil {
return err
}
Expand Down Expand Up @@ -133,18 +146,23 @@ func runRedis() error {

// runCardinalPrep preparation for runs cardinal in dev mode.
// We run cardinal without docker to make it easier to debug and skip the docker image build step
func runCardinalPrep() error {
func runCardinalPrep(watch bool) error {
err := os.Chdir("cardinal")
if err != nil {
return errors.New("can't find cardinal directory. Are you in the root of a World Engine project")
}

runnerIgnored := "."
if watch {
runnerIgnored = "assets, tmp, vendor"
}

env := map[string]string{
"REDIS_MODE": "normal",
"CARDINAL_PORT": CardinalPort,
"REDIS_ADDR": fmt.Sprintf("localhost:%s", RedisPort),
"DEPLOY_MODE": "development",
"RUNNER_IGNORED": "assets, tmp, vendor",
"RUNNER_IGNORED": runnerIgnored,
}

for key, value := range env {
Expand Down

0 comments on commit 441082c

Please sign in to comment.