-
Notifications
You must be signed in to change notification settings - Fork 54
feat: fix for create-dojo cli #312
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
WalkthroughThe pull request introduces modifications to the Changes
Possibly related PRs
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
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: 1
🧹 Outside diff range and nitpick comments (2)
packages/create-dojo/src/index.ts (2)
Line range hint
11-24
: Consider enhancing error handling in the main CLI function.The main function could benefit from a top-level error handler to provide better user feedback for CLI operations.
Consider wrapping the main logic in a try-catch block:
async function main() { + try { const packageInfo = await getPackageInfo(); const program = new Command() .name("@dojoengine") .description("install a dojo client") .version( packageInfo.version || "1.0.0", "-v, --version", "display the version number" ); program.addCommand(start); program.parse(); + } catch (error) { + console.error('Failed to initialize CLI:', error.message); + process.exit(1); + } }
Line range hint
7-8
: Consider adding error handlers for process signals.While the current signal handlers gracefully exit, they might leave resources in an inconsistent state.
Consider adding cleanup logic:
-process.on("SIGINT", () => process.exit(0)); -process.on("SIGTERM", () => process.exit(0)); +process.on("SIGINT", () => { + // Perform cleanup if necessary + process.exit(0); +}); +process.on("SIGTERM", () => { + // Perform cleanup if necessary + process.exit(0); +});
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (2)
packages/create-dojo/src/index.ts
(1 hunks)packages/create-dojo/src/utils/get-package-info.ts
(1 hunks)
🔇 Additional comments (2)
packages/create-dojo/src/utils/get-package-info.ts (1)
7-13
: Verify the impact of silent failures.
The function now silently continues with a default version when package.json is missing or invalid. Let's verify if this behavior is expected in all CLI contexts.
✅ Verification successful
Let me gather more specific information about how getPackageInfo is used.
Based on the gathered information, I can now provide the final response:
Silent failure handling in getPackageInfo is acceptable
The getPackageInfo function is only used in two scenarios:
- To display the CLI version number through Commander's
.version()
call - As a fallback version when package.json is missing
The default version "1.0.0" is appropriate because:
- For CLI version display: It's a non-critical feature that doesn't affect functionality
- The actual package versions for project dependencies are fetched directly from npm registry via
getLatestVersion()
function, not relying on the local package.json
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Description: Check if there are any CLI commands or features that critically depend on accurate package.json information
# Look for imports/usage of getPackageInfo
rg -l "getPackageInfo" --type ts
# Look for version-dependent logic
rg -i "version|semver" --type ts
Length of output: 2833
Script:
#!/bin/bash
# Check the specific usage of getPackageInfo in index.ts
rg -A 5 "getPackageInfo" packages/create-dojo/src/index.ts
# Check the implementation and usage of start command since it seems to be version-dependent
cat packages/create-dojo/src/commands/start.ts
Length of output: 7010
packages/create-dojo/src/index.ts (1)
12-12
:
Maintain async/await pattern for consistency and error handling.
The synchronous call to getPackageInfo()
within an async function creates inconsistency in the control flow. While the function may handle errors internally, synchronous file operations can block the event loop and impact performance.
Apply this diff to maintain the async pattern:
- const packageInfo = getPackageInfo();
+ const packageInfo = await getPackageInfo();
Let's verify if getPackageInfo
is used asynchronously elsewhere in the codebase:
try { | ||
return fs.readJSONSync(packageJsonPath) as PackageJson; | ||
} catch (error) { | ||
return { | ||
version: "1.0.0", | ||
} as PackageJson; | ||
} |
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.
🛠️ Refactor suggestion
Add error logging and consider project root path.
While the error handling is a good addition, there are a few concerns:
- Silently falling back to a default version could mask real issues
- The hardcoded path might fail when running from different directories
Consider this improved implementation:
export function getPackageInfo() {
- const packageJsonPath = path.join("package.json");
+ const packageJsonPath = path.join(process.cwd(), "package.json");
try {
return fs.readJSONSync(packageJsonPath) as PackageJson;
} catch (error) {
+ console.warn(`Failed to read package.json: ${error.message}`);
+ console.warn('Falling back to default version "1.0.0"');
return {
version: "1.0.0",
} as PackageJson;
}
}
The changes:
- Use
process.cwd()
to ensure we look for package.json in the project root - Add warning logs to help diagnose issues
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
try { | |
return fs.readJSONSync(packageJsonPath) as PackageJson; | |
} catch (error) { | |
return { | |
version: "1.0.0", | |
} as PackageJson; | |
} | |
try { | |
const packageJsonPath = path.join(process.cwd(), "package.json"); | |
return fs.readJSONSync(packageJsonPath) as PackageJson; | |
} catch (error) { | |
console.warn(`Failed to read package.json: ${error.message}`); | |
console.warn('Falling back to default version "1.0.0"'); | |
return { | |
version: "1.0.0", | |
} as PackageJson; | |
} |
cli
Closes #
Introduced changes
This PR fixes the issues where one gets an error
when trying to use the
command
Checklist
Summary by CodeRabbit
New Features
package.json
.Bug Fixes
package.json
to reference the current working directory.Refactor