Skip to content

Commit

Permalink
Many minor changes
Browse files Browse the repository at this point in the history
It:
* Now has support for nested html files
* Reloads any html page on any js or css change
  • Loading branch information
baalimago committed Aug 18, 2024
1 parent a1a635e commit 20d754f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 17 deletions.
7 changes: 6 additions & 1 deletion internal/wsinject/delta_streamer.ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@ function startWebsocket() {
let fileName = window.location.pathname.split('/').pop();
if(fileName === "") {
fileName = "/index.html"
} else {
fileName = "/" + fileName
}
// Reload page if it's detected that the current page has been altered
if(event.data === fileName) {
if(event.data === fileName ||
// Always reload on js and css files since its difficult to know where these are used
event.data.includes(".js") ||
event.data.includes(".css")) {
location.reload();
}
});
Expand Down
16 changes: 11 additions & 5 deletions internal/wsinject/wsinject.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ func NewFileServer(wsPort int, wsPath string) *Fileserver {
}
}

func (fs *Fileserver) mirrorFile(origPath, relativeName string) error {
func (fs *Fileserver) mirrorFile(origPath string) error {
relativePath := strings.Replace(origPath, fs.masterPath, "", -1)
fileB, err := os.ReadFile(origPath)
if err != nil {
return fmt.Errorf("failed to read file on path: '%v', err: %v", origPath, err)
Expand All @@ -63,7 +64,12 @@ func (fs *Fileserver) mirrorFile(origPath, relativeName string) error {
if injected {
ancli.PrintfNotice("injected delta-streamer script loading tag in: '%v'", origPath)
}
mirroredPath := path.Join(fs.mirrorPath, relativeName)
mirroredPath := path.Join(fs.mirrorPath, relativePath)
relativePathDir := path.Dir(mirroredPath)
err = os.MkdirAll(relativePathDir, 0755)
if err != nil {
return fmt.Errorf("failed to create relative dir: '%v', error: %v", relativePathDir, err)
}
err = os.WriteFile(mirroredPath, injectedBytes, 0755)
if err != nil {
return fmt.Errorf("failed to write mirrored file: %w", err)
Expand All @@ -79,7 +85,7 @@ func (fs *Fileserver) mirrorMaker(p string, info os.DirEntry, err error) error {
return nil
}

return fs.mirrorFile(p, info.Name())
return fs.mirrorFile(p)
}

func (fs *Fileserver) writeDeltaStreamerScript() error {
Expand All @@ -92,6 +98,7 @@ func (fs *Fileserver) writeDeltaStreamerScript() error {

func (fs *Fileserver) Setup(pathToMaster string) (string, error) {
ancli.PrintfNotice("mirroring root: '%v'", pathToMaster)
fs.masterPath = pathToMaster
err := wsInjectMaster(pathToMaster, fs.mirrorMaker)
if err != nil {
return "", fmt.Errorf("failed to create websocket injected mirror: %v", err)
Expand All @@ -109,7 +116,6 @@ func (fs *Fileserver) Setup(pathToMaster string) (string, error) {
return "", fmt.Errorf("failed to add path: '%v' to watcher, err: %v", pathToMaster, err)
}
fs.watcher = watcher
fs.masterPath = pathToMaster
return fs.mirrorPath, nil
}

Expand Down Expand Up @@ -142,7 +148,7 @@ func (fs *Fileserver) notifyPageUpdate(fileName string) {
func (fs *Fileserver) handleFileEvent(fsEv fsnotify.Event) {
if fsEv.Has(fsnotify.Write) {
ancli.PrintfNotice("noticed file write in orig file: '%v',", fsEv.Name)
fs.mirrorFile(fsEv.Name, strings.Replace(fsEv.Name, fs.masterPath, "", -1))
fs.mirrorFile(fsEv.Name)
fs.notifyPageUpdate(fsEv.Name)
}
}
Expand Down
45 changes: 34 additions & 11 deletions internal/wsinject/wsinject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"slices"
"strings"
"testing"

"github.com/baalimago/go_away_boilerplate/pkg/ancli"
)

func Test_walkDir(t *testing.T) {
Expand Down Expand Up @@ -58,33 +60,54 @@ const mockHtml = `<!DOCTYPE html>

func Test_Setup(t *testing.T) {
tmpDir := t.TempDir()
ancli.Newline = true
fileName := "t0.html"
os.WriteFile(path.Join(tmpDir, fileName), []byte(mockHtml), 0644)
os.WriteFile(path.Join(tmpDir, fileName), []byte(mockHtml), 0777)
nestedDir := path.Join(tmpDir, "nested")
err := os.MkdirAll(nestedDir, 0777)
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
nestedFile := path.Join(nestedDir, "nested.html")
os.WriteFile(nestedFile, []byte(mockHtml), 0777)
fs := NewFileServer(8080, "/delta-streamer-ws.js")
_, err := fs.Setup(tmpDir)
_, err = fs.Setup(tmpDir)
if err != nil {
t.Fatalf("failed to setup: %v", err)
}
t.Run("it should inject delta-streamer.js source tag", func(t *testing.T) {
mirrorFilePath := path.Join(fs.mirrorPath, fileName)
b, err := os.ReadFile(mirrorFilePath)
checkIfInjected := func(t *testing.T, filePath string) {
b, err := os.ReadFile(filePath)
if err != nil {
t.Fatalf("failed to read mirrored file: %v", err)
}
if !strings.Contains(string(b), "delta-streamer.js") {
t.Fatalf("expected mirrored file: '%v' to have been injected with content-streamer.js", mirrorFilePath)
t.Fatalf("expected mirrored file: '%v' to have been injected with content-streamer.js", filePath)
}
}
t.Run("it should inject delta-streamer.js source tag", func(t *testing.T) {
mirrorFilePath := path.Join(fs.mirrorPath, fileName)
checkIfInjected(t, mirrorFilePath)
})
t.Run("it should write the delta streamer file to root of mirror", func(t *testing.T) {
mirrorFilePath := path.Join(fs.mirrorPath, "delta-streamer.js")
b, err := os.ReadFile(mirrorFilePath)

t.Run("it should inject delta-streamer.js souce tag to nested files", func(t *testing.T) {
mirrorFilePath := path.Join(fs.mirrorPath, "nested", "nested.html")
checkIfInjected(t, mirrorFilePath)
})

checkIfDeltaStreamerExists := func(t *testing.T, filePath string) {
t.Helper()
b, err := os.ReadFile(filePath)
if err != nil {
t.Fatalf("failed to find delta-streamer.js: %v", err)
}
// Whatever happens in the delta streamre source code, it should mention
// wd-40
// Whatever happens in the delta streamre source code, it should mention wd-40
if !strings.Contains(string(b), "wd-40") {
t.Fatal("expected delta-streamer.js file to conain string 'wd-40'")
}
}
t.Run("it should write the delta streamer file to root of mirror", func(t *testing.T) {
mirrorFilePath := path.Join(fs.mirrorPath, "delta-streamer.js")
checkIfDeltaStreamerExists(t, mirrorFilePath)
})

}

0 comments on commit 20d754f

Please sign in to comment.