Skip to content

Commit f10db80

Browse files
committed
Switch from Gaze to Chokidar
1 parent 6f53366 commit f10db80

File tree

5 files changed

+63
-110
lines changed

5 files changed

+63
-110
lines changed

lib/dev.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Gaze } from "gaze";
21
import path from "path";
32
import { Server as IoServer } from "socket.io";
43
import { buildApp } from "@/lib/build";
@@ -9,13 +8,14 @@ import { Network } from "@/lib/types";
98
import { loopThroughFiles, readFile, readJson, writeJson } from "@/lib/utils/fs";
109
import { mergeDeep, substractDeep } from "@/lib/utils/objects";
1110
import { startFileWatcher } from "@/lib/watcher";
11+
import { FSWatcher } from "chokidar";
1212

1313
var appSrcs = [], appDists = [];
1414
var appDevJsons = [];
1515
var appDevJsonPath = "bos-loader.json";
1616
var appDevOptions: null | DevOptions = null;
1717
let io: null | IoServer = null;
18-
let fileWatcher: null | Gaze = null;
18+
let fileWatcher: null | FSWatcher = null;
1919

2020
export type DevOptions = {
2121
port?: number; // port to run dev server

lib/watcher.ts

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
import { Gaze } from "gaze";
1+
import chokidar, { FSWatcher } from "chokidar";
22

3-
export function startFileWatcher(watchPaths: string[], callback: Function): Gaze {
4-
const gaze = new Gaze(watchPaths, { debounceDelay: 100 });
3+
export function startFileWatcher(watchPaths: string[], callback: Function): FSWatcher {
4+
const watcher = chokidar.watch(watchPaths, {
5+
ignoreInitial: true,
6+
awaitWriteFinish: {
7+
stabilityThreshold: 100,
8+
pollInterval: 100
9+
}
10+
});
511

6-
// @ts-ignore
7-
gaze.on("all", callback);
8-
9-
return gaze;
12+
watcher.on('all', (event, path) => {
13+
callback();
14+
});
15+
return watcher;
1016
}

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@
3131
"@near-js/types": "^0.2.0",
3232
"axios": "^1.7.2",
3333
"body-parser": "^1.20.2",
34+
"chokidar": "^3.6.0",
3435
"commander": "^11.1.0",
3536
"crypto-js": "^4.2.0",
3637
"express": "^4.18.2",
3738
"fs-extra": "^11.2.0",
38-
"gaze": "^1.1.3",
3939
"glob": "^10.3.10",
4040
"http-proxy": "^1.18.1",
4141
"https": "^1.0.0",
@@ -55,7 +55,6 @@
5555
"@types/crypto-js": "^4.2.2",
5656
"@types/express": "^4.17.21",
5757
"@types/fs-extra": "^11.0.4",
58-
"@types/gaze": "^1.1.5",
5958
"@types/jest": "^29.5.11",
6059
"@types/node": "^20.11.30",
6160
"@types/supertest": "^6.0.2",

tests/unit/dev.ts

+27-17
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { startFileWatcher } from "@/lib/watcher";
88
import http from "http";
99
import path from "path";
1010
import { Server as IoServer } from "socket.io";
11-
import { Gaze } from "gaze";
11+
import chokidar from "chokidar";
1212

1313
import { vol } from 'memfs';
1414
jest.mock('fs', () => require('memfs').fs);
@@ -43,7 +43,6 @@ describe("dev", () => {
4343
(loadConfig as jest.MockedFunction<typeof loadConfig>).mockResolvedValue(mockConfig);
4444
(startDevServer as jest.MockedFunction<typeof startDevServer>).mockReturnValue({} as http.Server);
4545
(startSocket as jest.MockedFunction<typeof startSocket>).mockReturnValue(new IoServer());
46-
(startFileWatcher as jest.MockedFunction<typeof startFileWatcher>).mockReturnValue(new Gaze(mockSrc));
4746
(buildApp as jest.MockedFunction<typeof buildApp>).mockReturnValue({} as Promise<any>);
4847
});
4948

@@ -79,33 +78,44 @@ describe("dev", () => {
7978
it("should call startFileWatcher with correct watch paths", async () => {
8079
await dev(mockSrc, "build", mockOpts);
8180
const expectedWatchPaths = [
82-
path.join(mockSrc, 'widget', '**', '*'),
83-
path.join(mockSrc, 'module', '**', '*'),
84-
path.join(mockSrc, 'ipfs', '**', '*'),
85-
path.join(mockSrc, 'bos.config.json'),
86-
path.join(mockSrc, 'aliases.json'),
81+
path.join(mockSrc, "widget", "**", "*"),
82+
path.join(mockSrc, "module", "**", "*"),
83+
path.join(mockSrc, "ipfs", "**", "*"),
84+
path.join(mockSrc, "bos.config.json"),
85+
path.join(mockSrc, "aliases.json"),
8786
];
88-
expect(startFileWatcher).toHaveBeenCalledWith(expectedWatchPaths, expect.any(Function));
87+
expect(startFileWatcher).toHaveBeenCalledWith(
88+
expectedWatchPaths,
89+
expect.any(Function)
90+
);
8991
});
9092

9193
it("should add correct watch paths after adding apps", async () => {
92-
const mockedGazeAdd = jest.spyOn(Gaze.prototype, 'add');
93-
94+
const mockAdd = jest.fn();
95+
const mockWatch = jest.fn().mockReturnValue({
96+
add: mockAdd,
97+
});
98+
(chokidar.watch as jest.Mock) = mockWatch;
99+
100+
(startFileWatcher as jest.MockedFunction<typeof startFileWatcher>).mockReturnValue(
101+
mockWatch([], {}) as chokidar.FSWatcher
102+
);
103+
94104
const mockOpts: DevOptions = { hot: false };
95105
await dev(mockSrc, "build", mockOpts);
96106

97107
const mockSrc2 = "/app_example_2";
98108
vol.fromJSON(app_example_2, mockSrc2);
99-
const mockDist2 = path.join(mockSrc2, 'build');
109+
const mockDist2 = path.join(mockSrc2, "build");
100110
await addApps([mockSrc2], [mockDist2]);
101111

102112
const expectedWatchPaths = [
103-
path.join(mockSrc2, 'widget', '**', '*'),
104-
path.join(mockSrc2, 'module', '**', '*'),
105-
path.join(mockSrc2, 'ipfs', '**', '*'),
106-
path.join(mockSrc2, 'bos.config.json'),
107-
path.join(mockSrc2, 'aliases.json'),
113+
path.join(mockSrc2, "widget", "**", "*"),
114+
path.join(mockSrc2, "module", "**", "*"),
115+
path.join(mockSrc2, "ipfs", "**", "*"),
116+
path.join(mockSrc2, "bos.config.json"),
117+
path.join(mockSrc2, "aliases.json"),
108118
];
109-
expect(mockedGazeAdd).toHaveBeenCalledWith(expectedWatchPaths);
119+
expect(mockAdd).toHaveBeenCalledWith(expectedWatchPaths);
110120
});
111121
});

yarn.lock

+20-82
Original file line numberDiff line numberDiff line change
@@ -852,11 +852,6 @@
852852
"@types/jsonfile" "*"
853853
"@types/node" "*"
854854

855-
"@types/gaze@^1.1.5":
856-
version "1.1.5"
857-
resolved "https://registry.npmjs.org/@types/gaze/-/gaze-1.1.5.tgz"
858-
integrity sha512-EUsdBWbKIDvlsPBhEPA8LMbpvJaUjr/APU89uPbfPoXdPOw9Q6beEhB3oyYBn621j6s8zUiR5OVhbyJLA/Ij/A==
859-
860855
"@types/graceful-fs@^4.1.3":
861856
version "4.1.9"
862857
resolved "https://registry.npmjs.org/@types/graceful-fs/-/graceful-fs-4.1.9.tgz"
@@ -1383,6 +1378,21 @@ chokidar@^3.5.3:
13831378
optionalDependencies:
13841379
fsevents "~2.3.2"
13851380

1381+
chokidar@^3.6.0:
1382+
version "3.6.0"
1383+
resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-3.6.0.tgz#197c6cc669ef2a8dc5e7b4d97ee4e092c3eb0d5b"
1384+
integrity sha512-7VT13fmjotKpGipCW9JEQAusEPE+Ei8nl6/g4FBAmIm0GOOLMua9NDDo/DWp0ZAxCr3cPq5ZpBqmPAQgDda2Pw==
1385+
dependencies:
1386+
anymatch "~3.1.2"
1387+
braces "~3.0.2"
1388+
glob-parent "~5.1.2"
1389+
is-binary-path "~2.1.0"
1390+
is-glob "~4.0.1"
1391+
normalize-path "~3.0.0"
1392+
readdirp "~3.6.0"
1393+
optionalDependencies:
1394+
fsevents "~2.3.2"
1395+
13861396
ci-info@^3.2.0:
13871397
version "3.9.0"
13881398
resolved "https://registry.npmjs.org/ci-info/-/ci-info-3.9.0.tgz"
@@ -2042,13 +2052,6 @@ function-bind@^1.1.2:
20422052
resolved "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz"
20432053
integrity sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==
20442054

2045-
gaze@^1.1.3:
2046-
version "1.1.3"
2047-
resolved "https://registry.npmjs.org/gaze/-/gaze-1.1.3.tgz"
2048-
integrity sha512-BRdNm8hbWzFzWHERTrejLqwHDfS4GibPoq5wjTPIoJHoBtKGPg3xAFfxmM+9ztbXelxcf2hwQcaz1PtmFeue8g==
2049-
dependencies:
2050-
globule "^1.0.0"
2051-
20522055
gensync@^1.0.0-beta.2:
20532056
version "1.0.0-beta.2"
20542057
resolved "https://registry.npmjs.org/gensync/-/gensync-1.0.0-beta.2.tgz"
@@ -2121,18 +2124,6 @@ glob@^8.1.0:
21212124
minimatch "^5.0.1"
21222125
once "^1.3.0"
21232126

2124-
glob@~7.1.1:
2125-
version "7.1.7"
2126-
resolved "https://registry.npmjs.org/glob/-/glob-7.1.7.tgz"
2127-
integrity sha512-OvD9ENzPLbegENnYP5UUfJIirTg4+XwMWGaQfQTY0JenxNvvIKP3U3/tAQSPIu/lHxXYSZmpXlUHeqAIdKzBLQ==
2128-
dependencies:
2129-
fs.realpath "^1.0.0"
2130-
inflight "^1.0.4"
2131-
inherits "2"
2132-
minimatch "^3.0.4"
2133-
once "^1.3.0"
2134-
path-is-absolute "^1.0.0"
2135-
21362127
globals@^11.1.0:
21372128
version "11.12.0"
21382129
resolved "https://registry.npmjs.org/globals/-/globals-11.12.0.tgz"
@@ -2150,15 +2141,6 @@ globby@^11.0.4:
21502141
merge2 "^1.4.1"
21512142
slash "^3.0.0"
21522143

2153-
globule@^1.0.0:
2154-
version "1.3.4"
2155-
resolved "https://registry.npmjs.org/globule/-/globule-1.3.4.tgz"
2156-
integrity sha512-OPTIfhMBh7JbBYDpa5b+Q5ptmMWKwcNcFSR/0c6t8V4f3ZAVBEsKNY37QdVqmLRYSMhOUGYrY0QhSoEpzGr/Eg==
2157-
dependencies:
2158-
glob "~7.1.1"
2159-
lodash "^4.17.21"
2160-
minimatch "~3.0.2"
2161-
21622144
gopd@^1.0.1:
21632145
version "1.0.1"
21642146
resolved "https://registry.npmjs.org/gopd/-/gopd-1.0.1.tgz"
@@ -2988,11 +2970,6 @@ json-parse-even-better-errors@^2.3.0:
29882970
resolved "https://registry.npmjs.org/json-parse-even-better-errors/-/json-parse-even-better-errors-2.3.1.tgz"
29892971
integrity sha512-xyFwyhro/JEof6Ghe2iz2NcXoj2sloNsWr/XsERDK/oiPCfaNhl5ONfp+jQdAZRQQ0IJWNzH9zIZF7li91kh2w==
29902972

2991-
json-stringify-safe@^5.0.1:
2992-
version "5.0.1"
2993-
resolved "https://registry.npmjs.org/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb"
2994-
integrity sha512-ZClg6AaYvamvYEE82d3Iyd3vSSIjQ+odgjaTzRuO3s7toCdFKczob2i0zCh7JE8kWn17yvAWhUVxvqGwUalsRA==
2995-
29962973
json5@^2.2.3:
29972974
version "2.2.3"
29982975
resolved "https://registry.npmjs.org/json5/-/json5-2.2.3.tgz"
@@ -3039,7 +3016,7 @@ [email protected]:
30393016
resolved "https://registry.npmjs.org/lodash.memoize/-/lodash.memoize-4.1.2.tgz"
30403017
integrity sha512-t7j+NzmgnQzTAYXcsHYLgimltOV1MXHtlOWf6GjL9Kj8GK5FInw5JotxvbOs+IvV1/Dzo04/fCGfLVs7aXb4Ag==
30413018

3042-
lodash@^4.17.11, lodash@^4.17.21:
3019+
lodash@^4.17.11:
30433020
version "4.17.21"
30443021
resolved "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz"
30453022
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
@@ -3154,7 +3131,7 @@ mimic-fn@^2.1.0:
31543131
resolved "https://registry.npmjs.org/mimic-fn/-/mimic-fn-2.1.0.tgz"
31553132
integrity sha512-OqbOk5oEQeAZ8WXWydlu9HJjz9WVdEIvamMCcXmuqUYjTknH/sqsWvhQ3vgwKFRR1HpjvNBKQ37nbJgYzGqGcg==
31563133

3157-
minimatch@^3.0.4, minimatch@~3.0.2:
3134+
minimatch@^3.0.4:
31583135
version "3.0.8"
31593136
resolved "https://registry.npmjs.org/minimatch/-/minimatch-3.0.8.tgz"
31603137
integrity sha512-6FsRAQsxQ61mw+qP1ZzbL9Bc78x2p5OqNgNpnoAFLTrX8n5Kxph0CsnhmKKNXTWjXqU5L0pGPR7hYk+XWZr60Q==
@@ -3251,15 +3228,6 @@ [email protected]:
32513228
resolved "https://registry.npmjs.org/negotiator/-/negotiator-0.6.3.tgz"
32523229
integrity sha512-+EUsqGPLsM+j/zdChZjsnX51g4XrHFOIXwfnCVPGlQk/k5giakcKsuxCObBRu6DSm9opw/O6slWbJdghQM4bBg==
32533230

3254-
nock@^13.5.4:
3255-
version "13.5.4"
3256-
resolved "https://registry.npmjs.org/nock/-/nock-13.5.4.tgz#8918f0addc70a63736170fef7106a9721e0dc479"
3257-
integrity sha512-yAyTfdeNJGGBFxWdzSKCBYxs5FxLbCg5X5Q4ets974hcQzG1+qCxvIyOo4j2Ry6MUlhWVMX4OoYDefAIIwupjw==
3258-
dependencies:
3259-
debug "^4.1.0"
3260-
json-stringify-safe "^5.0.1"
3261-
propagate "^2.0.0"
3262-
32633231
32643232
version "2.6.7"
32653233
resolved "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz"
@@ -3484,11 +3452,6 @@ prompts@^2.0.1, prompts@^2.4.2:
34843452
kleur "^3.0.3"
34853453
sisteransi "^1.0.5"
34863454

3487-
propagate@^2.0.0:
3488-
version "2.0.1"
3489-
resolved "https://registry.npmjs.org/propagate/-/propagate-2.0.1.tgz#40cdedab18085c792334e64f0ac17256d38f9a45"
3490-
integrity sha512-vGrhOavPSTz4QVNuBNdcNXePNdNMaO1xj9yBeH1ScQPjk/rhg9sSlCXPhMkFuaNNW/syTvYqsnbIJxMBfRbbag==
3491-
34923455
proxy-addr@~2.0.7:
34933456
version "2.0.7"
34943457
resolved "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz"
@@ -3851,16 +3814,7 @@ string-length@^4.0.1:
38513814
char-regex "^1.0.2"
38523815
strip-ansi "^6.0.0"
38533816

3854-
"string-width-cjs@npm:string-width@^4.2.0":
3855-
version "4.2.3"
3856-
resolved "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz"
3857-
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
3858-
dependencies:
3859-
emoji-regex "^8.0.0"
3860-
is-fullwidth-code-point "^3.0.0"
3861-
strip-ansi "^6.0.1"
3862-
3863-
string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
3817+
"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
38643818
version "4.2.3"
38653819
resolved "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz"
38663820
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
@@ -3878,14 +3832,7 @@ string-width@^5.0.1, string-width@^5.1.2:
38783832
emoji-regex "^9.2.2"
38793833
strip-ansi "^7.0.1"
38803834

3881-
"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
3882-
version "6.0.1"
3883-
resolved "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz"
3884-
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
3885-
dependencies:
3886-
ansi-regex "^5.0.1"
3887-
3888-
strip-ansi@^6.0.0, strip-ansi@^6.0.1:
3835+
"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
38893836
version "6.0.1"
38903837
resolved "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz"
38913838
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
@@ -4334,16 +4281,7 @@ which@^2.0.1:
43344281
dependencies:
43354282
isexe "^2.0.0"
43364283

4337-
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
4338-
version "7.0.0"
4339-
resolved "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz"
4340-
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
4341-
dependencies:
4342-
ansi-styles "^4.0.0"
4343-
string-width "^4.1.0"
4344-
strip-ansi "^6.0.0"
4345-
4346-
wrap-ansi@^7.0.0:
4284+
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
43474285
version "7.0.0"
43484286
resolved "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz"
43494287
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==

0 commit comments

Comments
 (0)