Skip to content

Commit e81d399

Browse files
feat: replace fsWatch by chokidar (#80)
1 parent 894b06e commit e81d399

File tree

4 files changed

+50
-13
lines changed

4 files changed

+50
-13
lines changed

cli/package-lock.json

+29
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"@junobuild/console": "^0.1.5",
4646
"@junobuild/storage": "^0.1.5",
4747
"atomically": "^2.0.3",
48+
"chokidar": "^4.0.3",
4849
"kleur": "^4.1.5",
4950
"semver": "^7.7.1"
5051
},

cli/src/commands/watch.ts

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {debounce} from '@dfinity/utils';
2+
import chokidar from 'chokidar';
23
import type {FileChangeInfo} from 'fs/promises';
34
import kleur from 'kleur';
45
import {existsSync} from 'node:fs';
@@ -10,7 +11,7 @@ import {buildContext} from '../services/context.services';
1011
import type {CliContext} from '../types/context';
1112
import {watchers} from '../watch/watchers';
1213

13-
const {green} = kleur;
14+
const {green, red} = kleur;
1415

1516
export const watch = async (args?: string[]) => {
1617
await Promise.all([watchDeploy(args), watchConfig(args)]);
@@ -70,12 +71,22 @@ const watchDeploy = async (args?: string[]) => {
7071

7172
const context = await buildContext(args);
7273

73-
const watcher = fsWatch(DEV_DEPLOY_FOLDER);
74-
for await (const $event of watcher) {
74+
const watchOnEvent = async (path: string) => {
7575
await Promise.allSettled(
7676
watchers.map(async (watcher) => {
77-
await watcher.onWatch({$event, context});
77+
await watcher.onWatch({filePath: path, context});
7878
})
7979
);
80-
}
80+
};
81+
82+
chokidar
83+
.watch(DEV_DEPLOY_FOLDER, {
84+
ignoreInitial: true,
85+
awaitWriteFinish: true
86+
})
87+
.on('add', watchOnEvent)
88+
.on('change', watchOnEvent)
89+
.on('error', (err) => {
90+
console.log(red('️‼️ Unexpected error while live reloading:'), err);
91+
});
8192
};

cli/src/watch/services/_watcher.ts

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {debounce} from '@dfinity/utils';
2-
import type {FileChangeInfo} from 'fs/promises';
2+
import {basename} from 'node:path';
33
import type {CliContext} from '../../types/context';
44
import type {WatcherDescription} from '../../types/watcher';
55

@@ -13,13 +13,9 @@ export abstract class Watcher {
1313
this.moduleFileName = moduleFileName;
1414
}
1515

16-
onWatch = async ({
17-
$event: {filename},
18-
context
19-
}: {
20-
$event: FileChangeInfo<string>;
21-
context: CliContext;
22-
}) => {
16+
onWatch = async ({filePath, context}: {filePath: string; context: CliContext}) => {
17+
const filename = basename(filePath);
18+
2319
if (filename !== this.moduleFileName) {
2420
return;
2521
}

0 commit comments

Comments
 (0)