Skip to content

feat(repl): add CLI option to specify custom zenstack load path #1823

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

Merged
merged 1 commit into from
Nov 1, 2024

Conversation

ymc9
Copy link
Member

@ymc9 ymc9 commented Nov 1, 2024

fixes #1768

@ymc9 ymc9 marked this pull request as ready for review November 1, 2024 00:51
Copy link
Contributor

coderabbitai bot commented Nov 1, 2024

📝 Walkthrough

Walkthrough

The changes in this pull request enhance the REPL command in the CLI by introducing a new optional parameter loadPath for dynamic loading of the enhance module. The command's functionality is improved to handle user assignments and top-level await more effectively. Additionally, the CLI options for the repl command have been updated to include a --load-path <path> option, allowing users to specify the path for loading modules, alongside a modification to the --prisma-client option for clarity.

Changes

File Path Change Summary
packages/schema/src/cli/actions/repl.ts Updated repl function signature to include loadPath. Refined user assignment handling. Modified command evaluation for top-level await. Expanded help text for .auth command.
packages/schema/src/cli/index.ts Updated repl command options: added --load-path <path> and modified --prisma-client <module> to --prisma-client <path>.

Assessment against linked issues

Objective Addressed Explanation
Allow to specify zenstack generate output folder in repl CLI (#1768)

Possibly related PRs


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:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • 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 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
Contributor

@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: 1

🧹 Outside diff range and nitpick comments (4)
packages/schema/src/cli/index.ts (1)

136-137: LGTM! Consider enhancing the help text.

The new CLI options effectively address issue #1768 by allowing users to specify custom paths for ZenStack modules and Prisma client. The implementation is clean and follows CLI conventions.

Consider enhancing the help text with examples:

-.option('--load-path <path>', 'path to load modules generated by ZenStack')
-.option('--prisma-client <path>', 'path to Prisma client module')
+.option('--load-path <path>', 'path to load modules generated by ZenStack (e.g., "./generated")')
+.option('--prisma-client <path>', 'path to Prisma client module (e.g., "./generated/client")')
packages/schema/src/cli/actions/repl.ts (3)

12-14: Add path validation for loadPath option.

While the path handling is implemented correctly, consider adding validation for the loadPath option to ensure it exists and is accessible before attempting to require the module. This would provide better error messages to users.

 const { enhance } = options.loadPath
-    ? require(path.join(path.resolve(options.loadPath), 'enhance'))
+    ? (() => {
+        const resolvedPath = path.resolve(options.loadPath!);
+        if (!fs.existsSync(resolvedPath)) {
+            throw new Error(`Load path ${options.loadPath} does not exist`);
+        }
+        return require(path.join(resolvedPath, 'enhance'));
+    })()
     : require('@zenstackhq/runtime');

Also applies to: 55-57


151-162: Enhance error handling in auth command.

The auth command implementation is good, but the error handling could be more specific to help users understand what went wrong.

-            } catch (err: any) {
-                console.error('Unable to set auth user:', err.message);
+            } catch (err: any) {
+                if (err instanceof SyntaxError) {
+                    console.error('Invalid user object syntax. Example: .auth { id: "123" }');
+                } else if (err instanceof TypeError) {
+                    console.error('Invalid user object structure. Must be a valid object with required fields.');
+                } else {
+                    console.error('Unable to set auth user:', err.message);
+                }

12-15: Add JSDoc documentation for the new loadPath option.

The new loadPath option is a significant feature addition that deserves proper documentation.

 /**
  * CLI action for starting a REPL session
+ * @param projectPath - Path to the project root
+ * @param options - Configuration options
+ * @param options.loadPath - Optional path to load ZenStack generated modules from
+ * @param options.prismaClient - Optional path to Prisma client module
+ * @param options.debug - Enable debug mode
+ * @param options.table - Enable table output format
  */
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 00ecb2a and 730b3e9.

📒 Files selected for processing (2)
  • packages/schema/src/cli/actions/repl.ts (4 hunks)
  • packages/schema/src/cli/index.ts (1 hunks)
🧰 Additional context used
🪛 Biome
packages/schema/src/cli/actions/repl.ts

[error] 75-75: eval() exposes to security risks and performance issues.

See the MDN web docs for more details.
Refactor the code so that it doesn't need to call eval().

(lint/security/noGlobalEval)

🔇 Additional comments (1)
packages/schema/src/cli/index.ts (1)

136-137: Verify integration with repl action implementation.

Let's ensure the new options are properly handled in the repl action implementation.

✅ Verification successful

Both new options are properly integrated in the REPL implementation

The verification shows that both loadPath and prismaClient options are correctly handled in the REPL implementation:

  • loadPath is used to load the enhancement module (lines 56-58)
  • prismaClient is used to load the Prisma client module (lines 35-47)
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that the repl action properly handles the new options

# Test: Check if repl action implementation accepts these options
ast-grep --pattern 'export const repl = async ($params) => {
  $$$
}'

# Test: Look for option usage in the repl implementation
rg -l "loadPath|prisma-client" --type typescript

Length of output: 157


Script:

#!/bin/bash
# Let's try different approaches to find the repl implementation and option usage

# Find files containing 'repl' function or command
rg -l "repl" --type ts

# Look for option usage with different patterns
rg -l "load-path|prismaClient|loadPath" --type ts

# Try to find the repl implementation using ast-grep with a more specific pattern
ast-grep --pattern 'export const repl = async (options: $_) => {
  $$$
}'

# Also search for command registration pattern
ast-grep --pattern 'program
  .command("repl")
  $$$'

Length of output: 2846


Script:

#!/bin/bash
# Let's examine the repl action implementation in packages/schema/src/cli/actions/repl.ts

# Show the repl implementation to check option handling
cat packages/schema/src/cli/actions/repl.ts

# Also check the types/interfaces for the options
ast-grep --pattern 'interface $_ {
  $$$
  loadPath?: $_
  $$$
}'

ast-grep --pattern 'interface $_ {
  $$$
  prismaClient?: $_
  $$$
}'

Length of output: 9495

@ymc9 ymc9 merged commit d985a73 into dev Nov 1, 2024
13 checks passed
@ymc9 ymc9 deleted the fix/repl-load-path branch November 1, 2024 01:30
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