chore: improve development workflow

This commit is contained in:
Gustavo "Guz" L. de Mello
2024-06-17 16:28:19 -03:00
parent e7332da3f1
commit df5e2a26cb
4 changed files with 35 additions and 48 deletions

1
.gitignore vendored
View File

@@ -3,6 +3,7 @@ Session.vim
.direnv
.vercel
*_templ.go
*_templ.txt
dist
tmp
bin

View File

@@ -21,7 +21,6 @@
{
devShells.default = pkgs.mkShell {
buildInputs = with pkgs; [
air
go
golangci-lint
templ

View File

@@ -18,6 +18,7 @@ func main() {
staticDir := flag.String("s", "./static", "the directory to copy static files from")
port := flag.Int("p", 8080, "the port to run the server")
dev := flag.Bool("d", false, "if the server is in development mode")
cache := flag.Bool("c", true, "if the static files are cached")
flag.Parse()
@@ -66,7 +67,7 @@ func main() {
logger.Printf("Running server at port: %v", *port)
middleware := internals.NewMiddleware(mux, *dev, log.Default())
middleware := internals.NewMiddleware(mux, *dev, !*cache, log.Default())
err := http.ListenAndServe(fmt.Sprintf(":%v", *port), middleware)
if err != nil {
logger.Fatalf("Server crashed due to:\n%s", err)

View File

@@ -1,58 +1,44 @@
PORT?=8080
all: run
build:
pnpm unocss
templ generate
go build -o bin/www
dev:
$(MAKE) -j 2 dev-unocss dev-air
dev/templ:
templ generate --watch \
--proxy=http://localhost:$(PORT) \
--open-browser=false
dev-unocss:
pnpm unocss -w
dev-air:
air -build.pre_cmd 'make templ' \
-build.include_ext 'templ,css,go' \
-build.exclude_dir 'node_modules' \
-proxy.enabled true \
-proxy.app_port $(PORT) \
-proxy.proxy_port $$(($(PORT) + 1)) \
dev/server:
go run github.com/air-verse/air@v1.52.2 \
--build.cmd "go build -o tmp/bin/main" \
--build.bin "tmp/bin/main" \
--build.exclude_dir "node_modules" \
--build.include_ext "go" \
--build.stop_on_error "false" \
--misc.clean_on_exit true \
-- -p $(PORT) -d
dev-vercel:
air -build.pre_cmd 'make build-vercel' \
-build.include_ext 'templ,css' \
-build.exclude_dir 'node_modules' \
-build.cmd 'make build-vercel' \
-build.bin './bin/vercel' \
-proxy.enabled true \
-proxy.app_port $(PORT) \
-proxy.proxy_port $$(($(PORT) + 1)) \
-- -p $(PORT)
dev/unocss:
pnpm unocss -w
run: bin/www
dev/sync_assets:
go run github.com/air-verse/air@v1.52.2 \
--build.cmd "templ generate --notify-proxy" \
--build.bin "true" \
--build.delay "100" \
--build.exclude_dir "" \
--build.include_dir "static" \
--build.include_ext "js,css"
dev:
make -j4 dev/templ dev/server dev/unocss dev/sync_assets
run: build
./bin/www
run-vercel: bin/vercel
./bin/vercel
build-static: templ
pnpm unocss
go run ./cmd/build/main.go
build-vercel: bin/vercel build-static
bin/www: main.go templ
go build -o ./bin/www ./main.go
bin/vercel: cmd/vercel/main.go templ
go build -o ./bin/vercel ./cmd/vercel/main.go
# For some reason "templ generate" does not detect the files, so this is a
# workaround.
TEMPL_FILES=$(patsubst %.templ, %_templ.go, $(wildcard **/*.templ))
templ: $(TEMPL_FILES)
@echo Generating templ files
%_templ.go: %.templ
templ generate -f $^ > /dev/null
all: build
clean:
if [[ -d "dist" ]]; then rm -r ./dist; fi