Skip to content

Commit

Permalink
feat: support vercel dev
Browse files Browse the repository at this point in the history
  • Loading branch information
macrat committed Jan 26, 2024
1 parent 890c766 commit 1891ccd
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 56 deletions.
21 changes: 0 additions & 21 deletions Makefile

This file was deleted.

27 changes: 16 additions & 11 deletions builder/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ type Article struct {
URL string `yaml:"-"`
Path string `yaml:"-"`

Title string `yaml:"title"`
Image []string `yaml:"image"`
Description string `yaml:"description"`
Tags []string `yaml:"tags"`
Published time.Time `yaml:"pubtime"`
Modified time.Time `yaml:"modtime"`
FAQ []FAQItem `yaml:"faq"`
HowTo *HowTo `yaml:"howto"`
BreadCrumb []BreadCrumbItem `yaml:"breadcrumb"`
Layout string `yaml:"layout"`
Hidden bool `yaml:"hidden"`
Title string `yaml:"title"`
Image []string `yaml:"image"`
Description string `yaml:"description"`
Tags []string `yaml:"tags"`
Published time.Time `yaml:"pubtime"`
Modified time.Time `yaml:"modtime"`
FAQ []FAQItem `yaml:"faq"`
HowTo *HowTo `yaml:"howto"`
BreadCrumb []BreadCrumbItem `yaml:"breadcrumb"`
Layout string `yaml:"layout"`
Hidden bool `yaml:"hidden"`
Headers map[string]string `yaml:"headers"`

Markdown []byte `yaml:"-"`
Content template.HTML `yaml:"-"`
Expand Down Expand Up @@ -102,6 +103,10 @@ func (l *ArticleLoader) Load(externalPath string, raw []byte) (Article, error) {
article.Image = []string{"/images" + externalPath + ".png"}
}

if len(article.Headers) == 0 {
article.Headers = make(map[string]string)
}

var buf strings.Builder
if err := l.md.Convert(&buf, article.Markdown); err != nil {
return Article{}, err
Expand Down
37 changes: 30 additions & 7 deletions builder/autoindex.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package main

import (
"fmt"
"encoding/json"
"fmt"
"regexp"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -558,19 +559,41 @@ func (g *IndexGenerator) generateSitemap(dst fs.Writable, articles ArticleList,
return result, output.Close()
}

func (g *IndexGenerator) generateConfig(dst fs.Writable, articles ArticleList, conf Config) (ArtifactList, error) {
output, err := CreateOutput(dst, "config.json", "application/json")
func (g *IndexGenerator) generateConfig(dst fs.Writable, as ArticleList, conf Config) (ArtifactList, error) {
targetPath := "config.json"

if fs.ModTime(dst, targetPath).After(as.ModTime()) {
return nil, nil
}

output, err := CreateOutput(dst, targetPath, "application/json")
if err != nil {
return nil, err
}
defer output.Close()

type Route struct {
Src string `json:"src"`
Headers map[string]string `json:"headers"`
}

routes := make([]Route, 0, len(as))

for _, a := range as {
if len(a.Headers) > 0 {
routes = append(routes, Route{
Src: fmt.Sprintf("^%s$", regexp.QuoteMeta(a.Path)),
Headers: a.Headers,
})
}
}

err = json.NewEncoder(output).Encode(map[string]any{
"version": 3,
"routes": routes,
})
as := ArtifactList{Index{
return ArtifactList{Index{
name: "config.json",
sources: articles.Sources(),
}}
return as, err
sources: as.Sources(),
}}, err
}
31 changes: 15 additions & 16 deletions builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func PreviewServer(fs_ fs.Readable) error {
return
}

path := r.URL.Path[1:]
path := filepath.Join("static", r.URL.Path)

stat, err := fs.Stat(fs_, path)
if err == nil && stat.IsDir() {
Expand All @@ -95,8 +95,13 @@ func PreviewServer(fs_ fs.Readable) error {
http.ServeContent(w, r, path, fs.ModTime(fs_, path), f.(io.ReadSeeker))
})

log.Println("Listening on :3000")
return http.ListenAndServe(":3000", nil)
addr := ":3000"
if len(os.Args) > 2 {
addr = ":" + os.Args[2]
}

log.Printf("Listening on %s\n", addr)
return http.ListenAndServe(addr, nil)
}

type ContinuousBuilder struct {
Expand Down Expand Up @@ -239,27 +244,21 @@ func (b *ContinuousBuilder) StartWatching(sourceDir string) error {
return nil
}

func serve(path string) {
http.Handle("/", http.FileServer(http.Dir(path)))
log.Println("Listening on :3000")
log.Fatal(http.ListenAndServe(":3000", nil))
}

func main() {
stopProfiler := startProfiler()
defer stopProfiler()

preview := len(os.Args) > 1 && os.Args[1] == "preview"

if len(os.Args) > 1 && os.Args[1] == "serve" {
serve("../.vercel/output/static")
}
dev := len(os.Args) > 1 && os.Args[1] == "dev"

sourceDir := "../pages"
src := fs.NewOnDisk(sourceDir)
var dst fs.Writable = fs.NewOnDisk("../.vercel/output")

if preview {
if len(os.Args) > 1 && os.Args[1] == "serve" {
log.Fatal(PreviewServer(dst))
}

if dev {
dst = fs.NewInMemory()
}

Expand Down Expand Up @@ -310,7 +309,7 @@ func main() {
log.Println("Failed to tidy cache:", err)
}

if len(os.Args) > 1 && os.Args[1] == "preview" {
if dev {
if err = builder.StartWatching(sourceDir); err != nil {
log.Fatal(err)
}
Expand Down
24 changes: 24 additions & 0 deletions make
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/sh

case "$1" in
'' | build)
cd builder && go run .
;;
rebuild)
rm -rf .vercel/output
cd builder && go run .
;;
serve)
cd builder && go run . serve $2
;;
dev)
cd builder && go run . dev $2
;;
prepare)
git worktree add pages/photos photos
;;
clean)
rm -rf .vercel/output .vercel/cache
git worktree remove pages/photos
;;
esac
3 changes: 2 additions & 1 deletion vercel.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"buildCommand": "make build",
"buildCommand": "./make build",
"devCommand": "./make dev $PORT",
"trailingSlash": false,
"headers": [
{
Expand Down

0 comments on commit 1891ccd

Please sign in to comment.