Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
rework dev mode
Browse files Browse the repository at this point in the history
  • Loading branch information
thdxr committed Jan 24, 2024
1 parent 5d386aa commit 582bf53
Show file tree
Hide file tree
Showing 8 changed files with 1,003 additions and 35 deletions.
89 changes: 69 additions & 20 deletions cmd/sst/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"os/signal"
"os/user"
"path/filepath"
"reflect"
"strings"
"sync"

//"syscall"
"time"
Expand Down Expand Up @@ -225,33 +225,73 @@ func main() {
return err
}

deployWaiter := sync.WaitGroup{}
deployWaiter.Add(1)
restartTarget := make(chan project.Links)
runOnce := false
go func() {
if !hasTarget {
return
}
links := <-restartTarget

if hasTarget {
go func() {
deployWaiter.Wait()
cmd := exec.CommandContext(
ctx,
for {
cmd := exec.Command(
args[0],
args[1:]...,
)

cmd.Env = append(cmd.Env,
os.Environ()...,
)

for resource, value := range links {
jsonValue, _ := json.Marshal(value)
envVar := fmt.Sprintf("SST_RESOURCE_%s=%s", resource, jsonValue)
cmd.Env = append(cmd.Env, envVar)
}

cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Start()
cmd.Wait()
cancel()
}()
}
runOnce = true
processExit := make(chan interface{})

go func() {
cmd.Wait()
processExit <- true
}()

loop:
for {
select {
case <-ctx.Done():
cmd.Process.Signal(os.Interrupt)
return
case <-processExit:
cancel()
case nextLinks := <-restartTarget:
for key, value := range nextLinks {
oldValue := links[key]
if !reflect.DeepEqual(oldValue, value) {
cmd.Process.Signal(os.Interrupt)
cmd.Wait()
fmt.Println("Restarting...")
break loop
}
}
continue
}
}
}
}()

state := &server.State{}
u := ui.New(ui.ProgressModeDev)
err = server.Connect(cli.Context, server.ConnectInput{
CfgPath: cfgPath,
Stage: stage,
OnEvent: func(event server.Event) {
if state.Deployed == false || !hasTarget {
if !hasTarget || !runOnce {
defer u.Trigger(&event.StackEvent)
if event.StackEvent.PreludeEvent != nil {
u.Reset()
Expand All @@ -260,27 +300,36 @@ func main() {
}
}

if event.PreludeEvent != nil && hasTarget && state.Deployed {
fmt.Println()
fmt.Println("🔥 SST is deploying, run sst dev to view progress 🔥")
return
}

if event.CompleteEvent != nil {
if state.Deployed == false {
if !event.CompleteEvent.Finished || len(event.CompleteEvent.Errors) > 0 {
if hasTarget {
if !runOnce && (!event.CompleteEvent.Finished || len(event.CompleteEvent.Errors) > 0) {
cancel()
return
}

restartTarget <- event.CompleteEvent.Links
}
}

if event.StateEvent != nil {
next := event.StateEvent.State
if state.App == "" {
defer func() {
state = next
}()

if state.App == "" && next.App != "" {
u.Header(
version,
next.App,
next.Stage,
)
}
if !state.Deployed && next.Deployed && hasTarget {
deployWaiter.Done()
}
state = event.StateEvent.State
}
},
})
Expand Down
4 changes: 3 additions & 1 deletion examples/nextjs/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export default function Home() {
<p>
Get started by editing&nbsp;
<code className={styles.code}>app/page.tsx</code>
<div>cool {headers().get("host")}-{Date.now()}</div>
<div>
cool {headers().get("host")}-{Date.now()}
</div>
</p>
<div>
<a
Expand Down
7 changes: 4 additions & 3 deletions examples/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@
"lint": "next lint"
},
"dependencies": {
"next": "14.0.3",
"react": "^18",
"react-dom": "^18",
"next": "14.0.3"
"sst": "3.0.1-16"
},
"devDependencies": {
"typescript": "^5",
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"eslint": "^8",
"eslint-config-next": "14.0.3"
"eslint-config-next": "14.0.3",
"typescript": "^5"
}
}
Loading

0 comments on commit 582bf53

Please sign in to comment.