Skip to content

Commit

Permalink
better init command
Browse files Browse the repository at this point in the history
  • Loading branch information
thdxr committed Mar 14, 2024
1 parent f90373d commit 342901a
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 57 deletions.
88 changes: 88 additions & 0 deletions cmd/sst/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package main

import (
"fmt"
"os"

"github.com/fatih/color"
"github.com/manifoldco/promptui"
"github.com/sst/ion/pkg/project"
)

func CmdInit(cli *Cli) error {
color.New(color.FgYellow, color.Bold).Println(` __
__________/ /_
/ ___/ ___/ __/
(__ |__ ) /_
/____/____/\__/`)
// color.New(color.FgYellow, color.Bold).Println(` ______ ______ ______
// /\ ___\ /\ ___\ /\__ _\
// \ \___ \ \ \___ \ \/_/\ \/
// \/\_____\ \/\_____\ \ \_\
// \/_____/ \/_____/ \/_/`)
fmt.Println()
if _, err := os.Stat("sst.config.ts"); err == nil {
color.New(color.FgRed, color.Bold).Print("✗")
color.New(color.FgWhite, color.Bold).Println(" sst.config.ts already exists")
return nil
}

var template string
if _, err := os.Stat("next.config.js"); err == nil {
fmt.Println(" Next.js detected - initializing an sst project will")
fmt.Println(" - create an sst.config.ts file")
fmt.Println(" - add the sst sdk to your package.json")
fmt.Println(" - modify your tsconfig.json")
fmt.Println()
template = "nextjs"
}

if template == "" {
fmt.Println(" Creating a new sst project - this will")
fmt.Println(" - create an sst.config.ts file")
fmt.Println(" - install dependencies")
fmt.Println()
template = "vanilla"
}

p := promptui.Select{
Label: "Do you want to continue?",
HideSelected: true,
Items: []string{"Yes", "No"},
HideHelp: true,
}

_, confirm, err := p.Run()
if err != nil {
return err
}
if confirm == "No" {
return nil
}

color.New(color.FgGreen, color.Bold).Print("✔")
color.New(color.FgWhite).Println(" Using ", template, " template")

p = promptui.Select{
Label: "Where do you want to store the state of your app?",
HideSelected: true,
Items: []string{"aws", "cloudflare"},
HideHelp: true,
}
_, home, err := p.Run()
if err != nil {
return err
}

color.New(color.FgGreen, color.Bold).Print("✔")
color.New(color.FgWhite).Println(" Setting home to " + home)

err = project.Create(template, home)
if err != nil {
return err
}
initProject(cli)
color.New(color.FgGreen, color.Bold).Print("✔")
color.New(color.FgWhite).Println(" Created new project with '", template, "' template")
return nil
}
36 changes: 2 additions & 34 deletions cmd/sst/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/briandowns/spinner"
"github.com/fatih/color"
"github.com/joho/godotenv"
"github.com/manifoldco/promptui"
"github.com/sst/ion/cmd/sst/ui"
"github.com/sst/ion/internal/util"
"github.com/sst/ion/pkg/global"
Expand Down Expand Up @@ -1014,38 +1013,7 @@ Run this to initialize your app in drop-in mode. Currently, supports Next.js app
This will create a ` + "`sst.config.ts`" + ` file and configure the types for your project.
`,
},
Run: func(cli *Cli) error {
if _, err := os.Stat("sst.config.ts"); err == nil {
color.New(color.FgRed, color.Bold).Print("✗")
color.New(color.FgWhite, color.Bold).Println(" sst.config.ts already exists")
return nil
}
template := "vanilla"
if _, err := os.Stat("next.config.js"); err == nil {
p := promptui.Select{
Label: "Next.js detected, would you like to use the Next.js template?",
HideSelected: true,
Items: []string{"Yes", "No"},
HideHelp: true,
}
_, result, err := p.Run()
if err != nil {
return err
}
if result == "Yes" {
template = "nextjs"
}
}
err := project.Create(template)
if err != nil {
return err
}
initProject(cli)
color.New(color.FgGreen, color.Bold).Print("✔")
color.New(color.FgWhite, color.Bold).Println(" Created new project with '", template, "' template")
return nil

},
Run: CmdInit,
},
{
Name: "upgrade",
Expand Down Expand Up @@ -1296,7 +1264,7 @@ func (c CommandPath) PrintHelp() error {
}
next := len(child.Name)
if len(child.Args) > 0 {
next += len(child.Args.String())
next += len(child.Args.String()) + 1
}
if next > maxSubcommand {
maxSubcommand = next
Expand Down
3 changes: 2 additions & 1 deletion examples/nextjs/sst.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ export default $config({
return {
name: "nextjs",
removal: input?.stage === "production" ? "retain" : "remove",
home: "cloudflare",
};
},
async run() {
const site = new sst.aws.Nextjs("Web");
const site = new sst.cloudflare.Nextjs("Web");
},
});
5 changes: 5 additions & 0 deletions examples/nextjs/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
"sst.config.ts",
"sst.config.ts",
"sst.config.ts",
"sst.config.ts",
"sst.config.ts",
"sst.config.ts",
"sst.config.ts",
"sst.config.ts",
"sst.config.ts"
]
}
22 changes: 4 additions & 18 deletions examples/test/sst.config.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
/// <reference path="./.sst/platform/config.d.ts" />

export default $config({
app() {
app(input) {
return {
name: "test",
removal: "retain-all",
removal: input?.stage === "production" ? "retain" : "remove",
home: "aws",
providers: {},
};
},
async run() {
const fn = new sst.aws.Function("MyFunction2", {
handler: "src/index.handler",
url: true,
});
const router = new sst.aws.Router("MyRouter", {
routes: {
"/*": fn.url,
},
});
return {
url: fn.url,
router: router.url,
};
},
async run() {},
});
3 changes: 2 additions & 1 deletion pkg/platform/templates/nextjs/files/sst.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ export default $config({
return {
name: "{{.App}}",
removal: input?.stage === "production" ? "retain" : "remove",
home: "{{.Home}}",
};
},
async run() {
const site = new sst.aws.Nextjs("Web");
const site = new sst.{{.Home}}.Nextjs("Web");
},
});
1 change: 1 addition & 0 deletions pkg/platform/templates/vanilla/files/sst.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default $config({
return {
name: "{{.App}}",
removal: input?.stage === "production" ? "retain" : "remove",
home: "{{.Home}}",
};
},
async run() {},
Expand Down
8 changes: 5 additions & 3 deletions pkg/project/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type preset struct {
Steps []step `json:"steps"`
}

func Create(templateName string) error {
func Create(templateName string, home string) error {
if _, err := os.Stat("sst.config.ts"); err == nil {
return fmt.Errorf("sst.config.ts already exists")
}
Expand Down Expand Up @@ -127,9 +127,11 @@ func Create(templateName string) error {
slog.Info("copying template", "path", path)
tmpl, err := template.New(path).Parse(string(src))
data := struct {
App string
App string
Home string
}{
App: directoryName,
App: directoryName,
Home: home,
}

output, err := os.Create(name)
Expand Down

0 comments on commit 342901a

Please sign in to comment.