Skip to content

Commit ae757d7

Browse files
committed
feat: add dev-ui make target for live JS reloading
Add LRC_STATIC_DEV_DIR env var support to the staticserve package. When set, all three serving paths (GetStaticHandler, ReadFile, RenderPreactHTML) read from the filesystem instead of the embedded FS. Add dev-ui Makefile target that sets LRC_STATIC_DEV_DIR to the static source directory and runs the fake review with a 5s wait. Editing any JS file in internal/staticserve/static/ is visible on browser refresh without rebuilding the binary.
1 parent 28b5a2f commit ae757d7

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

Makefile

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: build build-win build-all build-local build-local-test run run-fake-review bump release release-internal release-gh clean test testall test-pkg upload-secrets download-secrets security-govulncheck security-govulncheck-json security-osv security-triage security-gitleaks security-b2-audit security-b2-cleanup-plan security-b2-cleanup-apply security-publish-release-manifest security-secret-regression security-sbom security-sbom-cyclonedx security-sbom-spdx security-sbom-validate release-notes-init release-notes-check release-preflight
1+
.PHONY: build build-win build-all build-local build-local-test run run-fake-review dev-ui bump release release-internal release-gh clean test testall test-pkg upload-secrets download-secrets security-govulncheck security-govulncheck-json security-osv security-triage security-gitleaks security-b2-audit security-b2-cleanup-plan security-b2-cleanup-apply security-publish-release-manifest security-secret-regression security-sbom security-sbom-cyclonedx security-sbom-spdx security-sbom-validate release-notes-init release-notes-check release-preflight
22

33
# Go parameters
44
GOENV=env -u GOROOT
@@ -70,6 +70,14 @@ run: build-local
7070
run-fake-review: build-local-test
7171
@WAIT=$${WAIT:-30s} TMP_REPO=$${TMP_REPO:-/tmp/lrc-fake-review-repo} scripts/fake_review.sh $(ARGS)
7272

73+
# Run fake review with live JS reloading — edit files in internal/staticserve/static/, refresh browser
74+
# No rebuild needed after JS changes: just edit and refresh the browser tab.
75+
dev-ui: build-local-test
76+
@LRC_STATIC_DEV_DIR=$(CURDIR)/internal/staticserve/static \
77+
WAIT=$${WAIT:-5s} \
78+
TMP_REPO=$${TMP_REPO:-/tmp/lrc-fake-review-repo} \
79+
scripts/fake_review.sh $(ARGS)
80+
7381
# Bump lrc version by editing appVersion in main.go
7482
# Prompts for version bump type (patch/minor/major)
7583
bump:

internal/staticserve/static_serve.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ package staticserve
22

33
import (
44
"embed"
5+
"encoding/json"
6+
"mime"
57
"net/http"
8+
"os"
9+
"path/filepath"
10+
"strings"
611

712
"github.com/HexmosTech/git-lrc/result"
813
)
@@ -16,13 +21,38 @@ type JSONHunkData = result.JSONHunkData
1621
type JSONLineData = result.JSONLineData
1722
type JSONCommentData = result.JSONCommentData
1823

24+
func devStaticDir() string {
25+
return os.Getenv("LRC_STATIC_DEV_DIR")
26+
}
27+
1928
// RenderPreactHTML renders the Preact-based HTML with embedded JSON data.
2029
func RenderPreactHTML(data *result.HTMLTemplateData) (string, error) {
30+
if dir := devStaticDir(); dir != "" {
31+
jsonData := result.ConvertToJSONData(data)
32+
jsonBytes, err := json.Marshal(jsonData)
33+
if err != nil {
34+
return "", err
35+
}
36+
htmlBytes, err := os.ReadFile(filepath.Join(dir, "index.html"))
37+
if err != nil {
38+
return "", err
39+
}
40+
html := strings.Replace(string(htmlBytes), "{{.JSONData}}", string(jsonBytes), 1)
41+
if data.FriendlyName != "" {
42+
html = strings.Replace(html, "<title>LiveReview Results</title>",
43+
"<title>LiveReview Results — "+data.FriendlyName+"</title>", 1)
44+
}
45+
return html, nil
46+
}
2147
return result.RenderPreactHTML(data, staticFiles)
2248
}
2349

2450
// GetStaticHandler returns an HTTP handler for serving static files.
2551
func GetStaticHandler() http.Handler {
52+
if dir := devStaticDir(); dir != "" {
53+
_ = mime.AddExtensionType(".mjs", "application/javascript; charset=utf-8")
54+
return http.FileServer(http.Dir(dir))
55+
}
2656
return result.GetStaticHandler(staticFiles)
2757
}
2858

@@ -31,7 +61,10 @@ func ServeStaticFile(w http.ResponseWriter, r *http.Request, filename string) er
3161
return result.ServeStaticFile(w, filename, staticFiles)
3262
}
3363

34-
// ReadFile reads a file from the embedded static directory.
64+
// ReadFile reads a file from the embedded static directory (or filesystem in dev mode).
3565
func ReadFile(name string) ([]byte, error) {
66+
if dir := devStaticDir(); dir != "" {
67+
return os.ReadFile(filepath.Join(dir, name))
68+
}
3669
return staticFiles.ReadFile("static/" + name)
3770
}

0 commit comments

Comments
 (0)