Skip to content

Commit

Permalink
better progress printing
Browse files Browse the repository at this point in the history
  • Loading branch information
thdxr committed Nov 23, 2023
1 parent e668051 commit 4f59b45
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 76 deletions.
75 changes: 7 additions & 68 deletions cmd/sst/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ import (
"log/slog"
"os"
"strings"
"time"

"github.com/briandowns/spinner"
"github.com/fatih/color"
"github.com/pulumi/pulumi/sdk/v3/go/common/apitype"
"github.com/sst/ion/pkg/global"
"github.com/sst/ion/pkg/project"

Expand Down Expand Up @@ -46,7 +43,7 @@ func main() {
return err
}
}
color.New(color.FgCyan, color.Bold).Print("SST ❍ Ion " + version + " ")
color.New(color.FgCyan, color.Bold).Print("SST ❍ ion " + version + " ")
color.New(color.FgHiBlack).Print("ready!\n")
return nil
},
Expand All @@ -61,74 +58,11 @@ func main() {
}
printHeader(p)

s := spinner.New(spinner.CharSets[14], 100*time.Millisecond)
s.Suffix = " Deploying..."
s.Start()
events, err := p.Stack.Deploy()
if err != nil {
return err
}

timing := make(map[string]time.Time)
outputs := make(map[string]interface{})
for evt := range events {
if evt.ResourcePreEvent != nil {
if evt.ResourcePreEvent.Metadata.Type == "pulumi:pulumi:Stack" {
continue
}
if evt.ResourcePreEvent.Metadata.Op == apitype.OpSame {
s.Disable()
color.New(color.FgHiBlack, color.Bold).Print("| ")
color.New(color.FgHiBlack).Println("Skipping ", prettyResourceName(evt.ResourcePreEvent.Metadata.URN))
continue
}

timing[evt.ResourcePreEvent.Metadata.URN] = time.Now()
if evt.ResourcePreEvent.Metadata.Op == apitype.OpCreate {
s.Disable()
color.New(color.FgYellow, color.Bold).Print("| ")
color.New(color.FgHiBlack).Println("Creating ", prettyResourceName(evt.ResourcePreEvent.Metadata.URN))
continue
}

if evt.ResourcePreEvent.Metadata.Op == apitype.OpUpdate {
s.Disable()
color.New(color.FgYellow, color.Bold).Print("| ")
color.New(color.FgHiBlack).Println("Updating ", prettyResourceName(evt.ResourcePreEvent.Metadata.URN))
continue
}
}

if evt.ResOutputsEvent != nil {
if evt.ResOutputsEvent.Metadata.Type == "pulumi:pulumi:Stack" {
outputs = evt.ResOutputsEvent.Metadata.New.Outputs
continue
}
if evt.ResOutputsEvent.Metadata.Op == apitype.OpSame {
continue
}
duration := time.Since(timing[evt.ResOutputsEvent.Metadata.URN]).Milliseconds()
if evt.ResOutputsEvent.Metadata.Op == apitype.OpCreate {
s.Disable()
color.New(color.FgGreen, color.Bold).Print("| ")
color.New(color.FgHiBlack).Println("Created ", prettyResourceName(evt.ResOutputsEvent.Metadata.URN), " in ", duration, "ms")
}
if evt.ResOutputsEvent.Metadata.Op == apitype.OpUpdate {
s.Disable()
color.New(color.Bold, color.FgGreen).Print("| ")
color.New(color.FgHiBlack).Println("Updated ", prettyResourceName(evt.ResOutputsEvent.Metadata.URN), " in ", duration, "ms")
}
}
s.Enable()
}
s.Stop()
color.New(color.FgGreen, color.Bold).Print("\n✔")
color.New(color.FgWhite, color.Bold).Println(" Deployed:")
for k, v := range outputs {
color.New(color.FgHiBlack).Print(" ")
color.New(color.FgHiBlack, color.Bold).Print(k + ": ")
color.New(color.FgWhite).Println(v)
}
progress(events)

return nil
},
Expand All @@ -141,10 +75,13 @@ func main() {
if err != nil {
return err
}
printHeader(p)

events, err := p.Stack.Remove()
if err != nil {
return err
}
progress(events)

for evt := range events {
if evt.ResourcePreEvent != nil {
Expand All @@ -162,6 +99,8 @@ func main() {
if err != nil {
return err
}
printHeader(p)

events, err := p.Stack.Refresh()
if err != nil {
return err
Expand Down
144 changes: 144 additions & 0 deletions cmd/sst/progress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package main

import (
"fmt"
"regexp"
"strings"
"time"

"github.com/briandowns/spinner"
"github.com/fatih/color"
"github.com/pulumi/pulumi/sdk/v3/go/common/apitype"
"github.com/sst/ion/pkg/project"
)

var urnRegex = regexp.MustCompile(`\/[^:]+`)

type Progress struct {
Color color.Attribute
Label string
URN string
time.Duration
}

func progress(events project.StackEventStream) {
spin := spinner.New(spinner.CharSets[14], 100*time.Millisecond)
spin.Suffix = " Deploying..."
spin.Start()

formatURN := func(urn string) string {
splits := strings.Split(urn, "::")[2:]
return urnRegex.ReplaceAllString(splits[0], "") + "::" + splits[1]
}

printProgress := func(progress Progress) {
spin.Disable()
color.New(progress.Color, color.Bold).Print("| ")
color.New(color.FgHiBlack).Print(progress.Label, " ", formatURN(progress.URN))
if progress.Duration != 0 {
color.New(color.FgHiBlack).Printf(" (%s)", progress.Duration)
}
fmt.Println()
spin.Enable()
}

timing := make(map[string]time.Time)
outputs := make(map[string]interface{})

for evt := range events {
if evt.StdOutEvent != nil {
spin.Disable()
fmt.Println(evt.StdOutEvent.Text)
spin.Enable()
continue
}
if evt.ResourcePreEvent != nil {
timing[evt.ResourcePreEvent.Metadata.URN] = time.Now()
if evt.ResourcePreEvent.Metadata.Type == "pulumi:pulumi:Stack" {
continue
}
if evt.ResourcePreEvent.Metadata.Op == apitype.OpSame {
printProgress(Progress{
Color: color.FgHiBlack,
Label: "Skipped ",
URN: evt.ResourcePreEvent.Metadata.URN,
})
continue
}

if evt.ResourcePreEvent.Metadata.Op == apitype.OpCreate {
printProgress(Progress{
Color: color.FgYellow,
Label: "Creating",
URN: evt.ResourcePreEvent.Metadata.URN,
})
continue
}

if evt.ResourcePreEvent.Metadata.Op == apitype.OpUpdate {
printProgress(Progress{
Color: color.FgYellow,
Label: "Updating",
URN: evt.ResourcePreEvent.Metadata.URN,
})

continue
}

if evt.ResourcePreEvent.Metadata.Op == apitype.OpDelete {
printProgress(Progress{
Color: color.FgYellow,
Label: "Deleting",
URN: evt.ResourcePreEvent.Metadata.URN,
})
continue
}
}

if evt.ResOutputsEvent != nil {
if evt.ResOutputsEvent.Metadata.Type == "pulumi:pulumi:Stack" && evt.ResOutputsEvent.Metadata.Op != apitype.OpDelete {
outputs = evt.ResOutputsEvent.Metadata.New.Outputs
continue
}
if evt.ResOutputsEvent.Metadata.Op == apitype.OpSame {
continue
}
duration := time.Since(timing[evt.ResOutputsEvent.Metadata.URN]).Round(time.Millisecond)
if evt.ResOutputsEvent.Metadata.Op == apitype.OpCreate {
printProgress(Progress{
Color: color.FgGreen,
Label: "Created ",
URN: evt.ResOutputsEvent.Metadata.URN,
Duration: duration,
})
}
if evt.ResOutputsEvent.Metadata.Op == apitype.OpUpdate {
printProgress(Progress{
Color: color.FgGreen,
Label: "Updated ",
URN: evt.ResOutputsEvent.Metadata.URN,
Duration: duration,
})
}
if evt.ResOutputsEvent.Metadata.Op == apitype.OpDelete {
printProgress(Progress{
Color: color.FgRed,
Label: "Deleted ",
URN: evt.ResOutputsEvent.Metadata.URN,
Duration: duration,
})
}
}
}

spin.Stop()
color.New(color.FgGreen, color.Bold).Print("\n✔")
color.New(color.FgWhite, color.Bold).Println(" Deployed:")

for k, v := range outputs {
color.New(color.FgHiBlack).Print(" ")
color.New(color.FgHiBlack, color.Bold).Print(k + ": ")
color.New(color.FgWhite).Println(v)
}

}
2 changes: 1 addition & 1 deletion internal/components/src/components/ssr-site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class SsrSite extends pulumi.ComponentResource {

// TODO uncomment build
const now = Date.now();
//buildApp();
buildApp();
console.log(`open-next: ${Date.now() - now}ms`);
const access = createCloudFrontOriginAccessIdentity();
const bucket = createS3Bucket();
Expand Down
4 changes: 3 additions & 1 deletion pkg/js/js.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ const LOOP = `
if (msg.type === "eval") {
try {
const result = await import(msg.module)
} catch(ex) {
console.error(ex)
} finally {
// await fs.rm(msg.module)
await fs.rm(msg.module)
console.log("~d")
}
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/project/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ func (p *Project) InstallDeps() error {
return err
}

if p.version != "dev" {
slog.Info("removing node_modules")
os.RemoveAll(filepath.Join(p.PathTemp(), "node_modules"))
if p.version == "dev" {
slog.Info("dev mode skipping node_module install")
return nil
}

os.RemoveAll(filepath.Join(p.PathTemp(), "node_modules"))

cmd := exec.Command("npm", "install")
cmd.Dir = p.PathTemp()
cmd.Stderr = os.Stderr
Expand Down
20 changes: 17 additions & 3 deletions pkg/project/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,16 @@ func (s *stack) runtime() (string, error) {
), nil
}

type StackEventStream = chan apitype.EngineEvent
type StackEvent struct {
apitype.EngineEvent
StdOutEvent *StdOutEvent
}

type StdOutEvent struct {
Text string
}

type StackEventStream = chan StackEvent

func (s *stack) run(cmd string) (StackEventStream, error) {
stack, err := s.runtime()
Expand Down Expand Up @@ -123,7 +132,7 @@ func (s *stack) run(cmd string) (StackEventStream, error) {
continue
}
if strings.HasPrefix(line, "~e") {
var evt apitype.EngineEvent
var evt StackEvent
err := json.Unmarshal([]byte(line[2:]), &evt)
if err != nil {
continue
Expand All @@ -133,7 +142,12 @@ func (s *stack) run(cmd string) (StackEventStream, error) {

continue
}
fmt.Println(line)

out <- StackEvent{
StdOutEvent: &StdOutEvent{
Text: line,
},
}
}
close(out)
}()
Expand Down

0 comments on commit 4f59b45

Please sign in to comment.