Skip to content

Commit

Permalink
fix: add isDirectory property
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Jan 11, 2025
1 parent 17b20df commit ff45ffe
Show file tree
Hide file tree
Showing 4 changed files with 21 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
1 change: 1 addition & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface ChangeInfo extends Record<string, any> {
*/
stat?: Stats;
path: string;
isDirectory?: boolean;
}

declare module '@eggjs/core' {
Expand Down
7 changes: 7 additions & 0 deletions test/development.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ 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({
hasDir: false,
});

/*
// 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 ff45ffe

Please sign in to comment.