Skip to content

Commit

Permalink
upgrade: sst upgrade command handles package.json upgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
thdxr committed Jul 22, 2024
1 parent bb0a32e commit 847612c
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 23 deletions.
23 changes: 3 additions & 20 deletions cmd/sst/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ var version = "dev"
func main() {
// check if node_modules/.bin/sst exists
nodeModulesBinPath := filepath.Join("node_modules", ".bin", "sst")
if _, err := os.Stat(nodeModulesBinPath); err == nil {
if _, err := os.Stat(nodeModulesBinPath); err == nil && os.Getenv("SST_SKIP_LOCAL") != "true" {
// forward command to node_modules/.bin/sst
fmt.Println(ui.TEXT_WARNING_BOLD.Render("Warning: ") + "You are using a global installation of SST but you also have a local installation specified in your package.json. The local installation will be used but you should typically run it through your package manager.")
cmd := exec.Command(nodeModulesBinPath, os.Args[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "SST_SKIP_LOCAL=true")
if err := cmd.Run(); err != nil {
os.Exit(1)
}
Expand Down Expand Up @@ -1081,25 +1082,7 @@ var root = &cli.Command{
},
},
},
Run: func(cli *cli.Cli) error {
newVersion, err := global.Upgrade(
version,
cli.Positional(0),
)
if err != nil {
return err
}
newVersion = strings.TrimPrefix(newVersion, "v")

color.New(color.FgGreen, color.Bold).Print(ui.IconCheck)
if newVersion == version {
color.New(color.FgWhite).Printf(" Already on latest %s\n", version)
} else {
color.New(color.FgWhite).Printf(" Upgraded %s ➜ ", version)
color.New(color.FgCyan, color.Bold).Println(newVersion)
}
return nil
},
Run: CmdUpgrade,
},
{
Name: "telemetry", Description: cli.Description{
Expand Down
76 changes: 76 additions & 0 deletions cmd/sst/upgrade.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package main

import (
"fmt"
"os"
"os/exec"
"strings"

"github.com/fatih/color"
"github.com/sst/ion/cmd/sst/cli"
"github.com/sst/ion/cmd/sst/mosaic/ui"
"github.com/sst/ion/pkg/global"
)

func CmdUpgrade(c *cli.Cli) error {
if os.Getenv("npm_config_user_agent") != "" {
updated, err := global.UpgradeNode(
version,
c.Positional(0),
)
if err != nil {
return err
}
hasAny := false
for file, newVersion := range updated {
fmt.Print(ui.TEXT_SUCCESS_BOLD.Render(ui.IconCheck) + " ")
fmt.Println(ui.TEXT_NORMAL.Render(file))
fmt.Println(" " + ui.TEXT_DIM.Render(newVersion))
if newVersion != version {
hasAny = true
}
}
if hasAny {
var cmd *exec.Cmd
if _, err := os.Stat("package-lock.json"); err == nil {
cmd = exec.Command("npm", "install")
}
if _, err := os.Stat("yarn.lock"); err == nil {
cmd = exec.Command("yarn", "install")
}
if _, err := os.Stat("pnpm-lock.yaml"); err == nil {
cmd = exec.Command("pnpm", "install")
}
if _, err := os.Stat("bun.lockb"); err == nil {
cmd = exec.Command("bun", "install")
}
if cmd != nil {
fmt.Println()
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
err := cmd.Run()
if err != nil {
return err
}
}
}
return nil
}
newVersion, err := global.Upgrade(
version,
c.Positional(0),
)
if err != nil {
return err
}
newVersion = strings.TrimPrefix(newVersion, "v")
fmt.Print(ui.TEXT_SUCCESS_BOLD.Render(ui.IconCheck))
if newVersion == version {
color.New(color.FgWhite).Printf(" Already on latest %s\n", version)
} else {
color.New(color.FgWhite).Printf(" Upgraded %s ➜ ", version)
color.New(color.FgCyan, color.Bold).Println(newVersion)
}
return nil
}
Empty file.
2 changes: 1 addition & 1 deletion examples/aws-hono/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"@aws-sdk/client-s3": "^3.552.0",
"@aws-sdk/s3-request-presigner": "^3.552.0",
"hono": "^4.1.3",
"sst": "^3.0.2",
"sst": "0.1.11",
"why-is-node-running": "^2.2.2"
}
}
Binary file modified examples/aws-monorepo/bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion examples/aws-monorepo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"typescript": "5.3.3"
},
"dependencies": {
"sst": "^0.1.7"
"sst": "0.1.11"
}
}
2 changes: 1 addition & 1 deletion examples/aws-monorepo/packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@aws-sdk/s3-request-presigner": "^3.540.0",
"astro": "^4.5.9",
"astro-sst": "^2.41.2",
"sst": "^0.1.7",
"sst": "0.1.11",
"typescript": "^5.4.3"
}
}
41 changes: 41 additions & 0 deletions pkg/global/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ import (
"net/http"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"

"github.com/sst/ion/pkg/npm"
)

func Upgrade(existingVersion string, nextVersion string) (string, error) {
Expand Down Expand Up @@ -104,6 +107,44 @@ func Upgrade(existingVersion string, nextVersion string) (string, error) {
return nextVersion, nil
}

func UpgradeNode(existingVersion string, nextVersion string) (map[string]string, error) {
result := make(map[string]string)
if nextVersion == "" {
pkg, err := npm.Get("sst", "ion")
if err != nil {
return result, err
}
nextVersion = pkg.Version
}

files, err := filepath.Glob("**/*/package.json")
files = append(files, "package.json")
if err != nil {
return result, err
}
re := regexp.MustCompile(`"sst": "[^"]+"`)
for _, file := range files {
if strings.HasPrefix(file, ".sst") {
continue
}
data, err := os.ReadFile(file)
if err != nil {
continue
}
matches := re.FindAll(data, -1)
if len(matches) == 0 {
continue
}
data = re.ReplaceAll(data, []byte(`"sst": "`+nextVersion+`"`))
err = os.WriteFile(file, data, 0666)
if err != nil {
return result, err
}
result[file] = nextVersion
}
return result, nil
}

func untar(reader io.Reader, target string) error {
tarReader := tar.NewReader(reader)
for {
Expand Down

1 comment on commit 847612c

@cgcompassion
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this update, I'm now seeing a bunch of warnings about You are using a global installation of SST but you also have a local installation specified in your package.json. The local installation will be used but you should typically run it through your package manager.

Please sign in to comment.