Skip to content

Commit

Permalink
Merge pull request #8 from nilotpaul/cli
Browse files Browse the repository at this point in the history
Introduced new customisable stack options and overall performance upgrade
  • Loading branch information
nilotpaul authored Nov 24, 2024
2 parents 5a5c437 + c7f8a9e commit 66a149d
Show file tree
Hide file tree
Showing 17 changed files with 369 additions and 228 deletions.
35 changes: 11 additions & 24 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"os"

"github.com/nilotpaul/gospur/config"
"github.com/nilotpaul/gospur/util"
Expand All @@ -11,37 +10,20 @@ import (

// handleInitCmd handles the `init` command for gospur CLI.
func handleInitCmd(cmd *cobra.Command, args []string) {
// `isEarlyStage` depicts the CLI is still in early stages
// and we don't have enough options for user prompts.
isEarlyStage := os.Getenv("EARLY_STAGE") == "True"

targetPath, err := util.GetProjectPath(args)
if err != nil {
fmt.Println(config.ErrMsg(err))
return
}

// Building the stack config by talking user prompts.
// If isEarlyStage is not true, we use start taking user prompts.
// If isEarlyStage is true, we use a default config.
var cfg util.StackConfig
if !isEarlyStage {
stackCfg, err := util.GetStackConfig()
if err != nil {
fmt.Println(config.ErrMsg(err))
return
}
cfg = *stackCfg
} else {
cfg = util.StackConfig{
WebFramework: config.WebFrameworkOpts[0],
UILibrary: config.UILibraryOpts[0],
Extras: config.ExtraOpts,
}
fmt.Println(config.NormalMsg("\nGo Spur is WIP 🚧, you'll only get default stack for now (More options coming soon).\n"))
stackCfg, err := util.GetStackConfig()
if err != nil {
fmt.Println(config.ErrMsg(err))
return
}
// Not needed for now.
_ = cfg
cfg = *stackCfg

// Asking for the go mod path from user.
goModPath, err := util.GetGoModulePath()
Expand All @@ -59,7 +41,12 @@ func handleInitCmd(cmd *cobra.Command, args []string) {

// Creating the project files in the target directory.
// Passing the go mod path for resolving Go imports.
if err := util.CreateProject(targetPath.Path, map[string]string{"ModPath": goModPath}); err != nil {
err = util.CreateProject(
targetPath.Path,
cfg,
util.MakeProjectCtx(cfg, goModPath),
)
if err != nil {
fmt.Println(config.ErrMsg(err))
return
}
Expand Down
35 changes: 28 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,42 @@ import "github.com/manifoldco/promptui"
// For adding styles to console output.
var (
ErrMsg = promptui.Styler(promptui.FGRed)
SuccessMsg = promptui.Styler(promptui.FGGreen)
SuccessMsg = promptui.Styler(promptui.FGGreen, promptui.FGBold)
NormalMsg = promptui.Styler(promptui.FGWhite)
FaintMsg = promptui.Styler(promptui.FGFaint)
)

// UILibrary represents an UI Library and `DependsOn`
// which means it can depend on any chosen CSS Strategy (framework).
type (
ProjectCtx map[string]any

UILibrary struct {
// Name of the UI Library
Name string

// An UI Library can depend on any chosen CSS Strategy.
// If it's independent, `DependsOn` should be an empty string.
DependsOn string
}
)

// Prompt options.
var (
WebFrameworkOpts = []string{
"Echo",
}
UILibraryOpts = []string{
"Preline (requires tailwind)",
}
ExtraOpts = []string{
"Tailwind",
"HTMX",
}

CssStrategyOpts = []string{
"Tailwind",
"Vanilla CSS",
}
UILibraryOpts = map[string][]string{
"Preline": {"Tailwind"},
}
)

// Project file structure
Expand All @@ -38,9 +57,11 @@ var (
"main.go": "base/main.go.tmpl",
}

// Template path is not required anymore for pages.
// We're processing these as raw files.
ProjectPageFiles = map[string]string{
"web/Home.html": "page/home.html.echo.tmpl",
"web/Error.html": "page/error.html.echo.tmpl",
"web/Home.html": "",
"web/Error.html": "",
}

ProjectAPIFiles = map[string]string{
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ require (
github.com/manifoldco/promptui v0.9.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b // indirect
github.com/yosssi/gohtml v0.0.0-20201013000340-ee4748c638f4 // indirect
golang.org/x/net v0.31.0 // indirect
golang.org/x/sys v0.27.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/yosssi/gohtml v0.0.0-20201013000340-ee4748c638f4 h1:0sw0nJM544SpsihWx1bkXdYLQDlzRflMgFJQ4Yih9ts=
github.com/yosssi/gohtml v0.0.0-20201013000340-ee4748c638f4/go.mod h1:+ccdNT0xMY1dtc5XBxumbYfOUhmduiGudqaDgD2rVRE=
golang.org/x/net v0.31.0 h1:68CPQngjLL0r2AlUKiSxtQFKvzRVbnzLwMUn5SzcLHo=
golang.org/x/net v0.31.0/go.mod h1:P4fl1q7dY2hnZFxEk4pPSkDHF+QqjitcnDjUQyMM+pM=
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b h1:MQE+LT/ABUuuvEZ+YQAMSXindAdUh7slEmAkup74op4=
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
10 changes: 2 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,9 @@ import (
)

func main() {
// This sets a `EARLY_STAGE` env which we use to
// initialize a default project without any options.
// This is done as currently we don't have any extra
// options other than the default template.
os.Setenv("EARLY_STAGE", "True")

// Stars the CLI
// Starts the CLI
if err := Execute(); err != nil {
fmt.Println(config.NormalMsg("Go Spur exited"))
fmt.Println(config.ErrMsg("Go Spur exited"))
os.Exit(1)
}
}
2 changes: 1 addition & 1 deletion template/api/handler.go.echo.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func HTTPErrorHandler(err error, c echo.Context) {

func handleGetHome(c echo.Context) error {
return c.Render(http.StatusOK, "Home", map[string]any{
"Title": "Go + Echo + HTMX",
"Title": "GoSpur Stack",
"Desc": "Best for building Full-Stack Applications with minimal JavaScript",
})
}
16 changes: 14 additions & 2 deletions template/base/esbuild.config.js.tmpl
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
const { build } = require("esbuild");
{{- if .UI.HasTailwind }}
const { tailwindPlugin } = require("esbuild-plugin-tailwindcss");
{{- end }}

const b = () =>
build({
bundle: true,
entryPoints: [
"web/styles/*",
{{- if .UI.HasPreline }}
"node_modules/preline/preline.js",
{{- end }}
{{- if .Extras.HasHTMX }}
"node_modules/htmx.org/dist/htmx.js",
{{- end }}
],
platform: "node",
{{- if .UI.HasTailwind }}
plugins: [tailwindPlugin()],
{{- end }}
entryNames: "[name]",
platform: "browser",
outdir: "public/bundle",
loader: { ".node": "file" },
format: "cjs",
minify: true,
});

Expand Down
19 changes: 15 additions & 4 deletions template/base/globals.css.tmpl
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
{{- if .UI.HasTailwind -}}
@tailwind base;
@tailwind components;
@tailwind utilities;
{{- else -}}
h1 {
color: red;
text-align: center;
}

@layer base {
h1 {
@apply text-4xl my-6;
}
.container {
max-width: 48rem;
margin: 0 auto;
display: flex;
flex-direction: column;
text-align: center;
align-items: center;
justify-content: center;
}
{{- end }}
16 changes: 6 additions & 10 deletions template/base/makefile.tmpl
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
start:
@npx tailwindcss -i ./web/styles/globals.css -o ./public/bundle/styles.css --minify
@node ./esbuild.config.js
@ENVIRONMENT=PRODUCTION go build -tags '!dev' -o bin/build
@ENVIRONMENT=PRODUCTION ./bin/build

dev: build
@ENVIRONMENT=DEVELOPMENT ./bin/build

build:
@npx tailwindcss -i ./web/styles/globals.css -o ./public/bundle/styles.css
@node ./esbuild.config.js
@ENVIRONMENT=DEVELOPMENT go build -tags 'dev' -o bin/build

watch:
dev:
@wgo \
-exit \
-file=.go \
-file=.html \
node ./esbuild.config.js \
:: npx tailwindcss -i ./web/styles/globals.css -o ./public/bundle/styles.css \
:: ENVIRONMENT=DEVELOPMENT go build -tags 'dev' -o bin/build main.go \
-file=.css \
-xdir=public \
ENVIRONMENT=DEVELOPMENT go build -tags 'dev' -o bin/build main.go \
:: ENVIRONMENT=DEVELOPMENT ./bin/build \
:: wgo -debounce 30ms -dir=public npx livereload public
:: wgo -dir=web node ./esbuild.config.js \
:: wgo -dir=node_modules npx livereload -w 1000 web
11 changes: 6 additions & 5 deletions template/base/package.json.tmpl
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"devDependencies": {
"esbuild": "^0.24.0"{{ if .Extras.HasHTMX }},
"htmx.org": "^1.9.12"{{ end }},
"livereload": "^0.9.3"{{ if .UI.HasPreline }},
"preline": "^2.5.1"{{ end }}{{ if .UI.HasTailwind }},
"esbuild-plugin-tailwindcss": "^1.2.1",
"@tailwindcss/forms": "^0.5.9",
"@tailwindcss/typography": "^0.5.15",
"esbuild": "^0.24.0",
"htmx.org": "^1.9.12",
"livereload": "^0.9.3",
"preline": "^2.5.1",
"tailwindcss": "^3.4.15"
"tailwindcss": "^3.4.15"{{ end }}
}
}
7 changes: 0 additions & 7 deletions template/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import (
//go:embed base/*
var base embed.FS

//go:embed page/*
var page embed.FS

//go:embed api/*
var api embed.FS

Expand All @@ -20,10 +17,6 @@ func GetBaseFiles() embed.FS {
return base
}

func GetPageFiles() embed.FS {
return page
}

func GetAPIFiles() embed.FS {
return api
}
Expand Down
20 changes: 0 additions & 20 deletions template/page/error.html.echo.tmpl

This file was deleted.

Loading

0 comments on commit 66a149d

Please sign in to comment.