about summary refs log tree commit diff stats
path: root/internal/builder
diff options
context:
space:
mode:
authorAlan Pearce2024-06-23 17:37:10 +0200
committerAlan Pearce2024-06-23 19:23:20 +0200
commit8cca5de5d0cd6b18c328a18de286eeb3b2c75b84 (patch)
tree2fdf48a815e81d8b6382e3dd9b3bdf6825d5606a /internal/builder
parentfe4a23b550b7450e6e24cd3c78dfd575122d361d (diff)
downloadwebsite-8cca5de5d0cd6b18c328a18de286eeb3b2c75b84.tar.lz
website-8cca5de5d0cd6b18c328a18de286eeb3b2c75b84.tar.zst
website-8cca5de5d0cd6b18c328a18de286eeb3b2c75b84.zip
pre-compress static files
Diffstat (limited to 'internal/builder')
-rw-r--r--internal/builder/builder.go6
-rw-r--r--internal/builder/files.go29
2 files changed, 30 insertions, 5 deletions
diff --git a/internal/builder/builder.go b/internal/builder/builder.go
index 4f305db..f3e61dc 100644
--- a/internal/builder/builder.go
+++ b/internal/builder/builder.go
@@ -17,7 +17,6 @@ import (
 
 	"github.com/a-h/templ"
 	mapset "github.com/deckarep/golang-set/v2"
-	cp "github.com/otiai10/copy"
 	"github.com/pkg/errors"
 )
 
@@ -102,10 +101,7 @@ func build(outDir string, config config.Config) (*Result, error) {
 		return nil, errors.WithMessage(err, "could not create public directory")
 	}
 
-	err := cp.Copy("static", publicDir, cp.Options{
-		PreserveTimes:     true,
-		PermissionControl: cp.AddPermission(0755),
-	})
+	err := copyRecursive("static", publicDir)
 	if err != nil {
 		return nil, errors.WithMessage(err, "could not copy static files")
 	}
diff --git a/internal/builder/files.go b/internal/builder/files.go
index 57d4d2e..20fedf9 100644
--- a/internal/builder/files.go
+++ b/internal/builder/files.go
@@ -3,7 +3,9 @@ package builder
 import (
 	"compress/gzip"
 	"io"
+	"io/fs"
 	"os"
+	"path/filepath"
 
 	"github.com/andybalholm/brotli"
 )
@@ -81,3 +83,30 @@ func openFileAndVariants(filename string) (io.WriteCloser, error) {
 		return openFileWrite(filename)
 	}
 }
+
+func copyRecursive(src, dst string) error {
+	return filepath.WalkDir(src, func(path string, d fs.DirEntry, err error) error {
+		rel, err := filepath.Rel(src, path)
+		if err != nil {
+			return err
+		}
+		if d.IsDir() {
+			return mkdirp(dst, rel)
+		} else {
+			sf, err := os.Open(path)
+			if err != nil {
+				return err
+			}
+			defer sf.Close()
+			df, err := openFileAndVariants(filepath.Join(dst, rel))
+			if err != nil {
+				return err
+			}
+			defer df.Close()
+			if _, err := io.Copy(df, sf); err != nil {
+				return err
+			}
+		}
+		return nil
+	})
+}