Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add isDirectory property #18

Merged
merged 3 commits into from
Jan 11, 2025
Merged

fix: add isDirectory property #18

merged 3 commits into from
Jan 11, 2025

Conversation

fengmk2
Copy link
Member

@fengmk2 fengmk2 commented Jan 11, 2025

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.

Copy link

coderabbitai bot commented Jan 11, 2025

Warning

Rate limit exceeded

@fengmk2 has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 24 minutes and 19 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between a7d1968 and 1f72a69.

📒 Files selected for processing (1)
  • test/development.test.ts (1 hunks)

Walkthrough

The pull request introduces enhancements to the file system event handling mechanism in the development event source. The changes include adding an isDirectory property to the ChangeInfo interface, modifying the DevelopmentEventSource class to populate this property, and updating a test fixture application to track directory changes. These modifications improve the ability to distinguish between file and directory events in the application's watcher functionality.

Changes

File Change Summary
src/lib/types.ts Added optional isDirectory?: boolean to ChangeInfo interface
src/lib/event-sources/development.ts Updated #onFsWatchChange method to store stat and add isDirectory to info object
test/development.test.ts Added new test case to verify directory existence endpoint
test/fixtures/apps/watcher-development-app/app/router.js Added hasDir variable and /app-hasDir endpoint to track directory changes

Possibly related PRs

  • fix: export watcher type on eggjs/core namespace #17: The changes in the main PR involve modifications to the ChangeInfo interface in src/lib/types.ts, which is also referenced in the retrieved PR that updates the EggCore interface to include a watcher property, indicating a direct relationship between the two.

Poem

🐰 A rabbit's watch, so keen and bright
Directories and files now in sight
With isDirectory we can tell
Which path rings its own special bell
Watching changes, swift and true
Our code dances in morning's dew! 🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (3)
src/lib/event-sources/development.ts (1)

92-97: Consider caching stat result to avoid duplicate fs calls

The implementation correctly adds the isDirectory property, but there's a potential performance optimization.

The stat is already computed in the watch method for directories. Consider passing it to #onFsWatchChange to avoid an extra fs call:

- const stat = fs.statSync(file, { throwIfNoEntry: false });
+ // Reuse stat from watch method for directories
+ const stat = existingStat ?? fs.statSync(file, { throwIfNoEntry: false });
test/fixtures/apps/watcher-development-app/app/router.js (2)

8-8: Consider adding reset capability for hasDir flag

The hasDir flag is set to true when a directory change is detected but there's no way to reset it to false. This could lead to stale state in long-running tests.

Consider adding a reset endpoint or mechanism:

 let hasDir = false;

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

+app.get('/app-reset', async ctx => {
+  hasDir = false;
+  fileChangeCount = 0;
+  ctx.body = 'reset success';
+});

Also applies to: 13-15


32-36: Add error handling to /app-hasDir endpoint

The endpoint implementation looks good but could benefit from error handling.

Consider wrapping the response in a try-catch:

 app.get('/app-hasDir', async ctx => {
+  try {
     ctx.body = {
       hasDir,
     };
+  } catch (error) {
+    ctx.status = 500;
+    ctx.body = { error: error.message };
+  }
 });
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 17b20df and ff45ffe.

📒 Files selected for processing (4)
  • src/lib/event-sources/development.ts (1 hunks)
  • src/lib/types.ts (1 hunks)
  • test/development.test.ts (1 hunks)
  • test/fixtures/apps/watcher-development-app/app/router.js (2 hunks)
🧰 Additional context used
🪛 GitHub Actions: Publish Any Commit
src/lib/types.ts

[error] 29-29: Property 'watcher' has conflicting type declarations. It must be of type 'Watcher', but has a different 'Watcher' type definition. This conflicts with the declaration in node_modules/@eggjs/watcher/dist/esm/lib/types.d.ts:25

⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: Node.js / Test (windows-latest, 23)
  • GitHub Check: Node.js / Test (windows-latest, 22)
  • GitHub Check: Node.js / Test (windows-latest, 20)
  • GitHub Check: Node.js / Test (windows-latest, 18)

@@ -21,6 +21,7 @@ export interface ChangeInfo extends Record<string, any> {
*/
stat?: Stats;
path: string;
isDirectory?: boolean;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix type conflict with @eggjs/watcher package

The pipeline is failing due to conflicting Watcher type declarations between this package and @eggjs/watcher. This needs to be resolved to ensure type compatibility.

Consider one of these approaches:

  1. If this package is meant to extend @eggjs/watcher, use interface merging:
import type { ChangeInfo as BaseChangeInfo } from '@eggjs/watcher';
export interface ChangeInfo extends BaseChangeInfo {
  isDirectory?: boolean;
}
  1. If this is a standalone package, remove the dependency on @eggjs/watcher types.

Comment on lines 62 to 67
await app.httpRequest()
.get('/app-hasDir')
.expect(200)
.expect({
hasDir: false,
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance test coverage for directory changes

The test only verifies the initial state where hasDir is false. Consider adding test cases for directory changes.

Add test cases to verify hasDir becomes true after directory operations:

// Test directory creation
fs.mkdirSync(file_path4, { recursive: true });
await scheduler.wait(100);
await app.httpRequest()
  .get('/app-hasDir')
  .expect(200)
  .expect({
    hasDir: true,
  });

// Optional: Test directory deletion if it should trigger the flag
fs.rmdirSync(file_path4);
await scheduler.wait(100);
await app.httpRequest()
  .get('/app-hasDir')
  .expect(200)
  .expect({
    hasDir: true, // Verify if deletion should also set hasDir
  });

Copy link

pkg-pr-new bot commented Jan 11, 2025

Open in Stackblitz

npm i https://pkg.pr.new/eggjs/watcher/@eggjs/watcher@18

commit: 1f72a69

Copy link

codecov bot commented Jan 11, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 93.46%. Comparing base (12e7fbd) to head (1f72a69).
Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #18      +/-   ##
==========================================
+ Coverage   93.37%   93.46%   +0.09%     
==========================================
  Files          13       13              
  Lines         362      367       +5     
  Branches       45       46       +1     
==========================================
+ Hits          338      343       +5     
  Misses         24       24              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@fengmk2 fengmk2 merged commit 1aaa983 into master Jan 11, 2025
24 checks passed
@fengmk2 fengmk2 deleted the fix-isDirectory branch January 11, 2025 02:06
fengmk2 pushed a commit that referenced this pull request Jan 11, 2025
[skip ci]

## [4.0.4](v4.0.3...v4.0.4) (2025-01-11)

### Bug Fixes

* add isDirectory property ([#18](#18)) ([1aaa983](1aaa983))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant