Skip to content

Conversation

@jaipaljadeja
Copy link
Member

adds file formatting with prettier, replaces --no-create-indexer flag to --noIndexer flag in apibara init and adds indexer file existence validation in prompt.
also adds validation for starting cursor in indexer.

@jaipaljadeja jaipaljadeja requested a review from fracek February 22, 2025 14:36
@coderabbitai
Copy link

coderabbitai bot commented Feb 22, 2025

📝 Walkthrough

Walkthrough

This PR introduces new JSON configuration files for the packages related to indexer management, marking them as prereleases with patch-level updates and addressing specific issues such as startingBlock validation and CLI flag naming. It adds the "prettier" dependency into the CLI package and refactors several CLI commands and project initialization routines to improve file formatting and asynchronous operations. Additionally, the changes update dependency versions in the constants and adjust control flow in the indexer's run function for proper handling of starting block edge cases.

Changes

File(s) Change Summary
change/@apibara-indexer-*.json, change/apibara-*.json Added new JSON configuration files for prerelease patch updates; one addresses startingBlock validation for @apibara/indexer and the other adjusts CLI flags (prettier formatting and noIndexer).
packages/cli/package.json Added dependency "prettier": "^3.5.2".
packages/cli/src/cli/commands/init.ts Renamed CLI argument from "no-create-indexer" to noIndexer in the command options.
packages/cli/src/create/add.ts, .../init.ts, .../templates.ts, .../utils.ts Introduced a new async formatFile utility, enhanced file creation with await calls, added file existence validation via checkFileExists, and refactored file path handling.
packages/cli/src/create/constants.ts Updated dependency versions (bumping beta versions for several @apibara packages).
packages/indexer/src/indexer.ts Modified logic to compute startingCursor based on the value of startingBlock, handling the special case when startingBlock is exactly 0n.

Sequence Diagram(s)

sequenceDiagram
    participant U as User
    participant CLI as CLI
    participant FS as File System
    participant FMT as Formatter (prettier)

    U->>CLI: Run `init` command
    CLI->>FS: Write config files (package.json, tsconfig.json, apibara.config)
    CLI->>FMT: Format each file (await formatFile)
    CLI->>FS: Check/Create "indexers" directory
    FS-->>CLI: Directory status confirmed
    CLI-->>U: Project initialization complete
Loading
sequenceDiagram
    participant CLI as CLI Command
    participant VAL as Validator
    participant FS as File System

    CLI->>VAL: Validate indexer ID
    VAL-->>CLI: Return validation result
    alt File exists
      CLI->>FS: Check for indexer file existence
      FS-->>CLI: File found → Return error ("indexer already exists")
    else File not exists
      CLI->>FS: Invoke updateApibaraConfigFile & updatePackageJson (awaited)
      FS-->>CLI: Update operations complete
    end
Loading

Possibly related PRs

Suggested reviewers

  • fracek

Poem

I'm a hopping rabbit in a code-filled glade,
Skipping through JSON fields in the upgrade parade.
Prettier sparkles on files, new paths and checks in sight,
Every patch and beta bump makes my day so bright.
With async hops and clean commands, I dance with pure delight,
Cheers from CodeRabbit Inc.—let’s keep the bugs out of our byte!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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: 0

🧹 Nitpick comments (2)
packages/indexer/src/indexer.ts (1)

228-234: LGTM! The changes improve validation of startingBlock.

The new conditional logic properly handles edge cases and prevents potential underflow when startingBlock is 0n.

Consider adding validation for negative startingBlock values.

While the current changes handle 0n and positive values correctly, it would be good to add validation for negative values to ensure robust error handling.

 } else if (indexer.options.startingBlock !== undefined) {
+  if (indexer.options.startingBlock < 0n) {
+    throw new Error("startingBlock cannot be negative");
+  }
   if (indexer.options.startingBlock === 0n) {
     startingCursor = undefined;
   } else if (indexer.options.startingBlock > 0n) {
     startingCursor = {
       orderKey: indexer.options.startingBlock - 1n,
     };
   }
 }
packages/cli/src/create/utils.ts (1)

415-422: Consider enhancing the formatFile function.

While the function works, consider these improvements:

  1. Use asynchronous file operations (fs.promises)
  2. Add error handling
  3. Make prettier options configurable

Here's a suggested implementation:

-export async function formatFile(path: string) {
-  const file = fs.readFileSync(path, "utf8");
-  const formatted = await prettier.format(file, {
-    filepath: path,
-    tabWidth: 2,
-  });
-  fs.writeFileSync(path, formatted);
-}
+export async function formatFile(path: string, options?: prettier.Options) {
+  try {
+    const file = await fs.promises.readFile(path, "utf8");
+    const formatted = await prettier.format(file, {
+      filepath: path,
+      tabWidth: 2,
+      ...options,
+    });
+    await fs.promises.writeFile(path, formatted);
+  } catch (error) {
+    throw new Error(`Failed to format file ${path}: ${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 c7da6df and fee6212.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (10)
  • change/@apibara-indexer-b2bba30d-fe14-4236-830e-49c35d5e973a.json (1 hunks)
  • change/apibara-f4a63aad-bcfe-435c-9522-12e7c8c95a4e.json (1 hunks)
  • packages/cli/package.json (1 hunks)
  • packages/cli/src/cli/commands/init.ts (1 hunks)
  • packages/cli/src/create/add.ts (4 hunks)
  • packages/cli/src/create/constants.ts (1 hunks)
  • packages/cli/src/create/init.ts (2 hunks)
  • packages/cli/src/create/templates.ts (8 hunks)
  • packages/cli/src/create/utils.ts (2 hunks)
  • packages/indexer/src/indexer.ts (1 hunks)
✅ Files skipped from review due to trivial changes (3)
  • change/@apibara-indexer-b2bba30d-fe14-4236-830e-49c35d5e973a.json
  • change/apibara-f4a63aad-bcfe-435c-9522-12e7c8c95a4e.json
  • packages/cli/src/create/constants.ts
🔇 Additional comments (13)
packages/cli/src/cli/commands/init.ts (1)

21-25: LGTM! Flag renamed for better consistency.

The renaming of the flag from "no-create-indexer" to "noIndexer" improves consistency with JavaScript/TypeScript naming conventions while maintaining the same functionality.

packages/cli/src/create/init.ts (4)

124-132: LGTM! Improved code organization and formatting.

Good practice to store file paths in dedicated variables and format the generated files for consistency.


135-141: LGTM! Proper TypeScript configuration handling.

The code correctly handles TypeScript configuration file creation and formatting.


143-148: LGTM! Consistent config file handling.

The code maintains consistency by formatting the Apibara config file similar to other configuration files.


150-155: LGTM! Added indexers directory creation.

Good addition to create the "indexers" directory by default, which aligns with the project structure and improves user experience.

packages/cli/src/create/add.ts (1)

92-105: LGTM! Enhanced indexer validation.

The validation logic has been improved to:

  • Check for valid indexer ID format
  • Verify file existence to prevent overwrites
  • Provide clear error messages
packages/cli/src/create/templates.ts (6)

8-8: LGTM!

The import of formatFile aligns with the PR objectives of implementing file formatting.


81-92: LGTM!

The reordering of imports improves code organization by grouping related imports together and maintaining a consistent import order.


172-173: LGTM!

The formatFile call ensures consistent code formatting after writing the indexer file.


176-176: LGTM!

The function is correctly updated to handle async operations and ensure consistent formatting of the package.json file.

Also applies to: 217-219


222-222: LGTM!

The function is correctly updated to handle async operations and ensure consistent formatting of the config file.

Also applies to: 283-283


325-325: LGTM!

The formatFile calls ensure consistent code formatting after writing each storage-related file.

Also applies to: 367-367, 424-424

packages/cli/package.json (1)

112-112: LGTM!

The addition of the prettier dependency aligns with the PR objectives of implementing file formatting.

@fracek fracek merged commit ff357df into apibara:main Feb 22, 2025
2 checks passed
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.

2 participants