Skip to content

Commit

Permalink
fix: add isDirectory property (#18)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
	- Added support for detecting directory changes in file system events.
	- Introduced a new endpoint `/app-hasDir` to check directory existence.

- **Tests**
- Enhanced test suite with a new test case to verify directory change
detection.

These updates improve the application's ability to handle file system
events and provide users with information about directory modifications.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
fengmk2 authored Jan 11, 2025
1 parent 17b20df commit 1aaa983
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/lib/event-sources/development.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,12 @@ export default class DevelopmentEventSource extends BaseEventSource {
// mtime: Wed Jun 17 2015 00:08:38 GMT+0800 (CST),
// ctime: Wed Jun 17 2015 00:08:38 GMT+0800 (CST),
// birthtime: Tue Jun 16 2015 23:19:13 GMT+0800 (CST) } }
const stat = fs.statSync(file, { throwIfNoEntry: false });
const info = {
path: file,
event,
stat: fs.statSync(file, { throwIfNoEntry: false }),
stat,
isDirectory: stat?.isDirectory(),
} as ChangeInfo;
this.emit('change', info);
}
Expand Down
3 changes: 3 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ export interface ChangeInfo extends Record<string, any> {
*/
stat?: Stats;
path: string;
isDirectory?: boolean;
}

declare module '@eggjs/core' {
interface EggCore {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
watcher: Watcher;
}

Expand Down
8 changes: 8 additions & 0 deletions test/development.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ describe('test/development.test.ts', () => {
count = parseInt(res.text);
assert(count > lastCount, `count: ${count}, lastCount: ${lastCount}`);

await app.httpRequest()
.get('/app-hasDir')
.expect(200)
.expect({
// work on windows
hasDir: process.platform === 'win32',
});

/*
// TODO wait unsubscribe implementation of cluster-client
await request(server)
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/apps/watcher-development-app/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ const dir_path = path.join(__dirname, '../tmp');

module.exports = function(app) {
let fileChangeCount = 0;
let hasDir = false;

function callback(info) {
console.log('got change %j', info);
fileChangeCount++;
if (info.isDirectory) {
hasDir = true;
}
}

app.get('/app-watch', async ctx => {
Expand All @@ -25,6 +29,12 @@ module.exports = function(app) {
ctx.body = fileChangeCount;
});

app.get('/app-hasDir', async ctx => {
ctx.body = {
hasDir,
};
});

app.get('/agent-watch', async ctx => {
app.messenger.broadcast('agent-watch');
ctx.body = await new Promise(function(resolve) {
Expand Down

0 comments on commit 1aaa983

Please sign in to comment.