Public Access
feat: scaffold API, client, or monorepo projects with Nuxt/Vue client support
- draft new now offers API, Client, or Full Stack (monorepo) project shapes - draft new client scaffolds a Nuxt or Vue app via the official nuxi/create-vue tooling, with a Draft-maintained auth/API overlay layered on top - draft new monorepo combines a scaffolded API and client under one root with go.work, pnpm-workspace.yaml, and docker-compose wiring - assets/embedded restructured into api/monorepo-root/client-nuxt/client-vue zones, synced via a generalized, job-based sync tool
This commit is contained in:
1 parent
fe48b7caee
commit
29f94539e0
7 files changed
+724
-99
No files matched your search
+169
-1
@@ -5,13 +5,18 @@ import (
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"math/big"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"git.fernandovideira.com/FernandoJVideira/draft/assets"
|
||||
"github.com/manifoldco/promptui"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -22,7 +27,7 @@ func randomString(length int) string {
|
||||
s, r := make([]rune, length), []rune(chars)
|
||||
|
||||
for i := range s {
|
||||
p, _ := rand.Prime(rand.Reader, len(r))
|
||||
p, _ := rand.Int(rand.Reader, big.NewInt(int64(len(r))))
|
||||
x, y := p.Uint64(), uint64(len(r))
|
||||
s[i] = r[x%y]
|
||||
}
|
||||
@@ -98,3 +103,166 @@ func runCommand(dir, name string, args ...string) error {
|
||||
|
||||
return cmd.Run()
|
||||
}
|
||||
|
||||
func commandExists(name string) bool {
|
||||
_, err := exec.LookPath(name)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func requirePnpmAndNpx() error {
|
||||
if !commandExists("pnpm") {
|
||||
return fmt.Errorf("pnpm is required but not found on PATH")
|
||||
}
|
||||
if !commandExists("npx") {
|
||||
return fmt.Errorf("npx is required but not found on PATH")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func promptClientFramework() (isVue bool, resultLabel string, err error) {
|
||||
prompt := promptui.Select{
|
||||
Label: "Select Client Framework",
|
||||
Items: []string{"Nuxt", "Vue"},
|
||||
}
|
||||
_, result, err := prompt.Run()
|
||||
if err != nil {
|
||||
return false, "", err
|
||||
}
|
||||
return result == "Vue", result, nil
|
||||
}
|
||||
|
||||
func pnpmVersion() (string, error) {
|
||||
out, err := exec.Command("pnpm", "--version").Output()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error getting pnpm version: %w", err)
|
||||
}
|
||||
return strings.TrimSpace(string(out)), nil
|
||||
}
|
||||
|
||||
func toPascalCase(s string) string {
|
||||
parts := regexp.MustCompile(`[^a-zA-Z0-9]+`).Split(s, -1)
|
||||
var b strings.Builder
|
||||
for _, p := range parts {
|
||||
if p == "" {
|
||||
continue
|
||||
}
|
||||
b.WriteString(strings.ToUpper(p[:1]) + p[1:])
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func promptProjectName() (moduleName, projectDir, serviceName string, err error) {
|
||||
prompt := promptui.Prompt{Label: "Project Name"}
|
||||
rawInput, err := prompt.Run()
|
||||
if err != nil {
|
||||
return "", "", "", err
|
||||
}
|
||||
moduleName = strings.TrimSpace(rawInput)
|
||||
projectDir = path.Base(strings.TrimSuffix(moduleName, "/"))
|
||||
serviceName = toPascalCase(projectDir)
|
||||
return moduleName, projectDir, serviceName, nil
|
||||
}
|
||||
|
||||
func promptAPIType() (useGRPC bool, resultLabel string, err error) {
|
||||
prompt := promptui.Select{
|
||||
Label: "Select API Type",
|
||||
Items: []string{"HTTP (REST)", "gRPC"},
|
||||
}
|
||||
_, result, err := prompt.Run()
|
||||
if err != nil {
|
||||
return false, "", err
|
||||
}
|
||||
return strings.Contains(result, "gRPC"), result, nil
|
||||
}
|
||||
|
||||
func copyEmbeddedDir(root, destDir string, replacements map[string]string) error {
|
||||
return fs.WalkDir(assets.EmbedFS, root, func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if d.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
relPath, _ := filepath.Rel(root, path)
|
||||
contentBytes, err := assets.EmbedFS.ReadFile(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error reading embedded file %s: %w", path, err)
|
||||
}
|
||||
|
||||
content := string(contentBytes)
|
||||
for old, new := range replacements {
|
||||
content = strings.ReplaceAll(content, old, new)
|
||||
}
|
||||
|
||||
targetPath := filepath.Join(destDir, relPath)
|
||||
if err := os.MkdirAll(filepath.Dir(targetPath), 0755); err != nil {
|
||||
return fmt.Errorf("error creating directory %s: %w", filepath.Dir(targetPath), err)
|
||||
}
|
||||
|
||||
return os.WriteFile(targetPath, []byte(content), 0644)
|
||||
})
|
||||
}
|
||||
|
||||
type scaffoldRecipe struct {
|
||||
label string
|
||||
destSubdir string
|
||||
preScaffold func(projectDir string) error
|
||||
postScaffold func(projectDir string) error
|
||||
}
|
||||
|
||||
func runNewProject(recipe scaffoldRecipe) {
|
||||
moduleName, projectDir, serviceName, err := promptProjectName()
|
||||
if err != nil {
|
||||
fmt.Printf("Prompt failed %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
useGRPC, resultLabel, err := promptAPIType()
|
||||
if err != nil {
|
||||
fmt.Printf("Prompt failed %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
if useGRPC && !commandExists("protoc") {
|
||||
fmt.Println("❌ protoc is required for gRPC projects but wasn't found on PATH.")
|
||||
fmt.Println(" Install it (e.g. `brew install protobuf`) and try again.")
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("🚀 Scaffolding %s '%s' as %s...\n", recipe.label, moduleName, resultLabel)
|
||||
|
||||
if recipe.preScaffold != nil {
|
||||
if err := recipe.preScaffold(projectDir); err != nil {
|
||||
fmt.Printf("error preparing project: %v", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if err := scaffoldAPI(projectDir, recipe.destSubdir, moduleName, serviceName, useGRPC); err != nil {
|
||||
fmt.Printf("error scaffolding project: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("📦 Installing tools...")
|
||||
if err := installTools(useGRPC); err != nil {
|
||||
fmt.Printf("error installing tools: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("🧹 Tidying up Go modules...")
|
||||
destRoot := filepath.Join(projectDir, recipe.destSubdir)
|
||||
if err := runCommand(destRoot, "go", "mod", "tidy"); err != nil {
|
||||
fmt.Printf("error running 'go mod tidy': %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if recipe.postScaffold != nil {
|
||||
if err := recipe.postScaffold(projectDir); err != nil {
|
||||
fmt.Printf("error finishing project: %v", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("✅ Done!")
|
||||
}
|
||||
Reference in new issue
Block a user