Skip to content

Commit ff6370d

Browse files
committed
chore: configurable watch paths
1 parent fff15a3 commit ff6370d

4 files changed

Lines changed: 99 additions & 83 deletions

File tree

api/openapi.yaml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,18 @@ components:
9696

9797
WatchModeRequest:
9898
type: object
99+
required: [watchPaths]
99100
properties:
100-
hotReloadCommands:
101+
watchPaths:
101102
type: array
102103
items:
103104
type: string
104-
description: Commands to run when files change
105-
buildCommands:
105+
description: Directories to watch
106+
hotReloadCommands:
106107
type: array
107108
items:
108109
type: string
109-
description: Build commands to run before hot reload
110+
description: Commands to run when files change
110111

111112
# Response Types
112113
TokenResponse:

internal/api/generated.go

Lines changed: 69 additions & 69 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/handler/watch_handler.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package handler
22

33
import (
44
"encoding/json"
5-
"path/filepath"
65
"time"
76

87
"github.com/gofiber/contrib/websocket"
@@ -35,7 +34,6 @@ func (udh *WatchHandler) EnableWatch(c *fiber.Ctx) error {
3534
return c.JSON(response)
3635
}
3736

38-
var watchPaths []string
3937
// Get current scroll to determine watch paths
4038
scrollDir := udh.scrollService.GetDir()
4139
if scrollDir == "" {
@@ -62,7 +60,14 @@ func (udh *WatchHandler) EnableWatch(c *fiber.Ctx) error {
6260
}
6361
}
6462

65-
watchPaths = append(watchPaths, filepath.Join(scrollDir, "public/src"), filepath.Join(scrollDir, "private/src"))
63+
watchPaths := requestBody.WatchPaths
64+
65+
if len(watchPaths) == 0 {
66+
return c.Status(400).JSON(api.ErrorResponse{
67+
Status: "error",
68+
Error: "At least one watch path must be specified",
69+
})
70+
}
6671

6772
// Start file watching with scroll directory as base path
6873
err = udh.uiWatchService.StartWatching(scrollDir, watchPaths...)

internal/handler/watch_handler_test.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,15 @@ func TestWatchHandler_Enable_Success(t *testing.T) {
5555

5656
tc.WatchService.EXPECT().IsWatching().Return(false)
5757
tc.ScrollService.EXPECT().GetDir().Return("/path/to/scroll")
58-
// When request has no body or empty body, BodyParser fails, so SetHotReloadCommands is NOT called
59-
tc.WatchService.EXPECT().StartWatching("/path/to/scroll", gomock.Any(), gomock.Any()).Return(nil)
58+
tc.WatchService.EXPECT().StartWatching("/path/to/scroll", "src", "config").Return(nil)
6059
tc.WatchService.EXPECT().IsWatching().Return(true)
6160

62-
req := httptest.NewRequest("POST", "/api/v1/watch/enable", nil)
61+
requestBody := api.WatchModeRequest{
62+
WatchPaths: []string{"src", "config"},
63+
}
64+
bodyBytes, _ := json.Marshal(requestBody)
65+
66+
req := httptest.NewRequest("POST", "/api/v1/watch/enable", bytes.NewReader(bodyBytes))
6367
req.Header.Set("Content-Type", "application/json")
6468
resp, err := tc.App.Test(req)
6569
if err != nil {
@@ -143,10 +147,15 @@ func TestWatchHandler_Enable_StartWatchingError(t *testing.T) {
143147

144148
tc.WatchService.EXPECT().IsWatching().Return(false)
145149
tc.ScrollService.EXPECT().GetDir().Return("/path/to/scroll")
146-
// When request has no body, BodyParser fails, so SetHotReloadCommands is NOT called
147-
tc.WatchService.EXPECT().StartWatching("/path/to/scroll", gomock.Any(), gomock.Any()).Return(fiber.NewError(500, "watcher error"))
150+
tc.WatchService.EXPECT().StartWatching("/path/to/scroll", "src").Return(fiber.NewError(500, "watcher error"))
148151

149-
req := httptest.NewRequest("POST", "/api/v1/watch/enable", nil)
152+
requestBody := api.WatchModeRequest{
153+
WatchPaths: []string{"src"},
154+
}
155+
bodyBytes, _ := json.Marshal(requestBody)
156+
157+
req := httptest.NewRequest("POST", "/api/v1/watch/enable", bytes.NewReader(bodyBytes))
158+
req.Header.Set("Content-Type", "application/json")
150159
resp, err := tc.App.Test(req)
151160
if err != nil {
152161
t.Fatalf("Failed to execute request: %v", err)
@@ -165,12 +174,13 @@ func TestWatchHandler_Enable_WithCommands(t *testing.T) {
165174
tc.WatchService.EXPECT().IsWatching().Return(false)
166175
tc.ScrollService.EXPECT().GetDir().Return("/path/to/scroll")
167176
tc.WatchService.EXPECT().SetHotReloadCommands([]string{"npm run dev"})
168-
tc.WatchService.EXPECT().StartWatching("/path/to/scroll", gomock.Any(), gomock.Any()).Return(nil)
177+
tc.WatchService.EXPECT().StartWatching("/path/to/scroll", "src", "lib").Return(nil)
169178
tc.WatchService.EXPECT().IsWatching().Return(true)
170179

171180
hotReloadCmds := []string{"npm run dev"}
172181
requestBody := api.WatchModeRequest{
173182
HotReloadCommands: &hotReloadCmds,
183+
WatchPaths: []string{"src", "lib"},
174184
}
175185
bodyBytes, _ := json.Marshal(requestBody)
176186

0 commit comments

Comments
 (0)