-
Notifications
You must be signed in to change notification settings - Fork 11
cli: add file formatting with prettier and add noIndexer flag #147
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
Conversation
📝 WalkthroughWalkthroughThis 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
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
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
Possibly related PRs
Suggested reviewers
Poem
✨ Finishing Touches
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this 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
startingBlockis0n.Consider adding validation for negative startingBlock values.
While the current changes handle
0nand 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:
- Use asynchronous file operations (
fs.promises)- Add error handling
- 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
⛔ Files ignored due to path filters (1)
pnpm-lock.yamlis 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
formatFilealigns 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
formatFilecall 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
formatFilecalls 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
prettierdependency aligns with the PR objectives of implementing file formatting.
adds file formatting with prettier, replaces
--no-create-indexerflag to--noIndexerflag inapibara initand adds indexer file existence validation in prompt.also adds validation for starting cursor in indexer.