From 1aaa98388edbdb4a327bbf3bd18f6760e3bfd262 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Sat, 11 Jan 2025 10:06:17 +0800 Subject: [PATCH] fix: add isDirectory property (#18) ## 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. --- src/lib/event-sources/development.ts | 4 +++- src/lib/types.ts | 3 +++ test/development.test.ts | 8 ++++++++ .../apps/watcher-development-app/app/router.js | 10 ++++++++++ 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/lib/event-sources/development.ts b/src/lib/event-sources/development.ts index b499ee6..fd15c1e 100644 --- a/src/lib/event-sources/development.ts +++ b/src/lib/event-sources/development.ts @@ -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); } diff --git a/src/lib/types.ts b/src/lib/types.ts index 9da6b19..77df3ef 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -21,10 +21,13 @@ export interface ChangeInfo extends Record { */ 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; } diff --git a/test/development.test.ts b/test/development.test.ts index 1dd8f5e..91f4c65 100644 --- a/test/development.test.ts +++ b/test/development.test.ts @@ -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) diff --git a/test/fixtures/apps/watcher-development-app/app/router.js b/test/fixtures/apps/watcher-development-app/app/router.js index 3aed8db..30e89e2 100644 --- a/test/fixtures/apps/watcher-development-app/app/router.js +++ b/test/fixtures/apps/watcher-development-app/app/router.js @@ -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 => { @@ -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) {