-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Switch from Gaze to Chokidar * Remove linter * upgrade packages * Update with absolute path * Add file path test for watcher --------- Co-authored-by: Elliot Braem <[email protected]>
- Loading branch information
1 parent
6f53366
commit fd143f0
Showing
6 changed files
with
1,020 additions
and
979 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,19 @@ | ||
import { Gaze } from "gaze"; | ||
import path from "path" | ||
import chokidar, { FSWatcher } from "chokidar"; | ||
|
||
export function startFileWatcher(watchPaths: string[], callback: Function): Gaze { | ||
const gaze = new Gaze(watchPaths, { debounceDelay: 100 }); | ||
export function startFileWatcher(watchPaths: string[], callback: Function): FSWatcher { | ||
const watcher = chokidar.watch(watchPaths, { | ||
ignoreInitial: true, | ||
awaitWriteFinish: { | ||
stabilityThreshold: 100, | ||
pollInterval: 100 | ||
} | ||
}); | ||
|
||
// @ts-ignore | ||
gaze.on("all", callback); | ||
watcher.on('all', (event, relativePath) => { | ||
const absolutePath = path.resolve(relativePath) | ||
callback(event, absolutePath); | ||
}); | ||
|
||
return gaze; | ||
return watcher; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { startFileWatcher } from "@/lib/watcher"; | ||
import path from "path"; | ||
import fs from "fs"; | ||
|
||
describe("File Watcher Tests", () => { | ||
let watcher; | ||
const tempDirPath = path.join(__dirname, "temp-test"); | ||
|
||
it("should call the file watcher callback with the correct attributes", async () => { | ||
const mockCallback = jest.fn(); | ||
|
||
fs.mkdirSync(tempDirPath, { recursive: true }); | ||
|
||
const tempFilePath = path.join(tempDirPath, "temp-file.txt"); | ||
|
||
const relativeTempFilePath = path.relative("./", tempFilePath) | ||
|
||
fs.writeFileSync(tempFilePath, "initial content"); | ||
|
||
watcher = startFileWatcher([relativeTempFilePath], mockCallback); | ||
|
||
await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
|
||
fs.appendFileSync(tempFilePath, "\nadded content"); | ||
|
||
await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
|
||
expect(mockCallback).toHaveBeenCalled(); | ||
expect(mockCallback.mock.calls[0][0]).toBe("change"); | ||
expect(mockCallback.mock.calls[0][1]).toBe(path.resolve(tempFilePath)); | ||
}); | ||
|
||
afterAll(async () => { | ||
if (watcher) await watcher.close(); | ||
|
||
if (fs.existsSync(tempDirPath)) { | ||
fs.rmSync(tempDirPath, { recursive: true, force: true }); | ||
} | ||
}); | ||
}); |
Oops, something went wrong.