Skip to content

Conversation

@pujitm
Copy link
Member

@pujitm pujitm commented Apr 8, 2025

Summary by CodeRabbit

  • New Features

    • Introduced an automated step in the post-build process to copy plugin assets.
    • Enhanced the plugin import process by supporting multiple sourcing options.
    • Adds a demo health query via a workspace plugin.
  • Documentation

    • Added a detailed guide explaining API plugin configuration and local workspace integration.
  • Refactor

    • Improved dependency handling by marking certain workspace plugins as optional.
    • Updated deployment synchronization to ensure destination directories exactly mirror the source.
    • Refined logging levels and type-safety for improved reliability and debugging.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Apr 8, 2025

Walkthrough

This pull request updates multiple components of the API project. It modifies the post-build process by extending the script in api/package.json and adds new peer dependency declarations. A new documentation section for API plugins is introduced. The build script is enhanced with type safety improvements, and a new script is added to copy workspace plugin distributions. Additionally, the plugin import process is made more robust by attempting multiple import sources, and deployment synchronization is refined by adding file deletion capabilities.

Changes

File(s) Change Summary
api/package.json Extended postbuild script to execute node scripts/copy-plugins.js; added peerDependencies and peerDependenciesMeta sections.
api/docs/developer/api-plugins.md Added a documentation section describing API plugin structure, handling of workspace plugins, and configuration for vendoring plugins.
api/scripts/build.ts Removed unused rm import; added ApiPackageJson type alias for enhanced type safety; updated comment for clarity regarding vendored dependencies.
api/scripts/copy-plugins.js New script to copy workspace plugin distribution folders to dist/plugins, building each plugin and handling errors during the copy process.
api/scripts/deploy-dev.sh Updated rsync_command to include the --delete option, ensuring destination reflects the exact state of the source.
api/src/unraid-api/plugin/plugin.module.ts Changed log level from log to debug for a message listing CLI plugins.
api/src/unraid-api/plugin/plugin.service.ts Modified importPlugins to attempt importing from multiple sources using Promise.any; updated listPlugins to check peerDependencies with an adjusted warning message.

Sequence Diagram(s)

sequenceDiagram
    participant Build as Postbuild Script
    participant CP as copy-plugins.js
    participant FS as File System
    participant Plugin as Workspace Plugin

    Build->>CP: Run postbuild (includes executing copy-plugins.js)
    CP->>FS: Read package.json & ensure dist/plugins exists
    CP->>CP: Filter workspace plugins by prefix
    loop For each plugin
        CP->>Plugin: Execute 'pnpm build'
        Plugin-->>CP: Build result
        alt Build successful
            CP->>FS: Copy plugin dist folder to dist/plugins directory
        else Build failed
            CP->>CP: Log error and exit process
        end
    end
    CP->>FS: Log success of all copy operations
Loading
sequenceDiagram
    participant Service as PluginService
    participant Src1 as Primary Package
    participant Src2 as Local Index File
    participant Logger as Debug Logger

    Service->>Service: Identify plugin from peerDependencies
    Service->>Src1: Attempt import using package name
    Service->>Src2: Attempt import using local index path
    par
         Src1-->>Service: Successful import (if available)
         Src2-->>Service: Successful import (if fallback)
    end
    Service->>Logger: Log the count of loaded plugins
Loading

Possibly related PRs

Suggested reviewers

  • elibosley
  • mdatelle

Poem

On build and plugin paths so neat,
Our scripts align in synchronized beat.
From copy tasks to imports refined,
Each change a piece in the design.
Code flows with a joyful gleam,
A merry dance in our developer’s dream!
🚀✨


🪧 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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @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.

@pujitm
Copy link
Member Author

pujitm commented Apr 8, 2025

@coderabbitai pause

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Apr 8, 2025

✅ Actions performed

Reviews paused.

@pujitm pujitm force-pushed the refactor/config-defs branch 2 times, most recently from c9b6b26 to e592486 Compare April 9, 2025 13:25
@pujitm pujitm force-pushed the refactor/config-defs branch from e592486 to c9330be Compare April 9, 2025 19:38
@pujitm
Copy link
Member Author

pujitm commented Apr 10, 2025

@CodeRabbit resume

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Apr 10, 2025

✅ Actions performed

Reviews resumed.

@pujitm pujitm changed the title [WIP] refactor(api): modular config interface chore(api): enable using workspace plugins in production Apr 10, 2025
@pujitm pujitm marked this pull request as ready for review April 10, 2025 16:22
@pujitm pujitm requested a review from elibosley as a code owner April 10, 2025 16:22
@github-actions
Copy link
Contributor

This plugin has been deployed to Cloudflare R2 and is available for testing.
Download it at this URL:

https://preview.dl.unraid.net/unraid-api/tag/PR1343/dynamix.unraid.net.plg

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

🧹 Nitpick comments (4)
api/docs/developer/api-plugins.md (2)

5-5: Minor spelling suggestion.

Consider using "bidirectional" (one word) instead of "bi-directional" for consistency with standard technical terminology.

-peer dependencies are installed by default as of npm v7, it supports bi-directional plugin functionality,
+peer dependencies are installed by default as of npm v7, it supports bidirectional plugin functionality,
🧰 Tools
🪛 LanguageTool

[misspelling] ~5-~5: This word is normally spelled as one.
Context: ...ed by default as of npm v7, it supports bi-directional plugin functionality, where the API pro...

(EN_COMPOUNDS_BI_DIRECTIONAL)


13-13: Consider rewording for clarity.

The phrase "we vendor them" might be clearer as "we include them" or "we package them".

-To solve this, we vendor them inside `dist/plugins`. To prevent the build from breaking, however,
+To solve this, we include them inside `dist/plugins`. To prevent the build from breaking, however,
🧰 Tools
🪛 LanguageTool

[grammar] ~13-~13: Check that the noun ‘vendor’ after the pronoun ‘we’ is correct. It’s possible that you may need to switch to a possessive pronoun, or use another part of speech.
Context: ...a npm during production. To solve this, we vendor them inside dist/plugins. To prevent ...

(PRP_VB)

api/scripts/copy-plugins.js (2)

1-60: Well-structured plugin build and copy script

This script effectively handles building and copying workspace plugins to the distribution folder. The implementation includes proper error handling, verification of build success, and clear logging.

Consider adding a cleanup step to remove old plugin distributions before copying new ones:

// Before line 31 (after finding workspace plugins)
+// Clean up existing plugin directories
+console.log('Cleaning up existing plugin directories...');
+for (const pkgName of workspacePlugins) {
+    const targetPath = path.resolve(pluginsDir, pkgName);
+    if (fs.existsSync(targetPath)) {
+        console.log(`Removing existing ${pkgName} directory...`);
+        fs.rmSync(targetPath, { recursive: true, force: true });
+    }
+}

39-47: Consider handling process termination more gracefully

While the error handling for build failures is good, a sudden process exit might not be ideal in all contexts.

} catch (error) {
    console.error(`Failed to build ${pkgName}:`, error.message);
-    process.exit(1);
+    throw new Error(`Build process for ${pkgName} failed`);
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro (Legacy)

📥 Commits

Reviewing files that changed from the base of the PR and between 680c7e2 and c25b8fc.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (7)
  • api/docs/developer/api-plugins.md (1 hunks)
  • api/package.json (2 hunks)
  • api/scripts/build.ts (2 hunks)
  • api/scripts/copy-plugins.js (1 hunks)
  • api/scripts/deploy-dev.sh (1 hunks)
  • api/src/unraid-api/plugin/plugin.module.ts (1 hunks)
  • api/src/unraid-api/plugin/plugin.service.ts (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (3)
api/src/unraid-api/plugin/plugin.service.ts (2)
api/scripts/copy-plugins.js (1)
  • pluginPrefix (26-26)
api/src/environment.ts (1)
  • getPackageJson (36-42)
api/scripts/build.ts (2)
api/scripts/copy-plugins.js (1)
  • packageJson (17-17)
api/scripts/get-deployment-version.ts (1)
  • getDeploymentVersion (13-34)
api/scripts/copy-plugins.js (1)
web/scripts/sync-webgui-repo.js (1)
  • path (2-2)
🪛 LanguageTool
api/docs/developer/api-plugins.md

[misspelling] ~5-~5: This word is normally spelled as one.
Context: ...ed by default as of npm v7, it supports bi-directional plugin functionality, where the API pro...

(EN_COMPOUNDS_BI_DIRECTIONAL)


[grammar] ~13-~13: Check that the noun ‘vendor’ after the pronoun ‘we’ is correct. It’s possible that you may need to switch to a possessive pronoun, or use another part of speech.
Context: ...a npm during production. To solve this, we vendor them inside dist/plugins. To prevent ...

(PRP_VB)

⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Build and Deploy Plugin / Build and Deploy Plugin
  • GitHub Check: Cloudflare Pages
🔇 Additional comments (15)
api/src/unraid-api/plugin/plugin.module.ts (1)

40-40: Change from log to debug level is appropriate.

Reducing the log level from log to debug for plugin discovery is a good practice. This change ensures that detailed plugin information is only shown in debug mode, while keeping production logs cleaner and more focused on important messages.

api/scripts/deploy-dev.sh (1)

32-32: Added --delete flag improves deployment synchronization.

Adding the --delete flag to rsync ensures that the destination directory is an exact mirror of the source, removing any obsolete files that are no longer part of the build. This is particularly valuable for plugin management, as it will automatically clean up old plugin files during deployment.

api/docs/developer/api-plugins.md (4)

1-7: Clear documentation on API plugin integration.

The introduction effectively explains how API plugins are implemented as npm peerDependencies, providing a solid foundation for understanding the plugin architecture.

🧰 Tools
🪛 LanguageTool

[misspelling] ~5-~5: This word is normally spelled as one.
Context: ...ed by default as of npm v7, it supports bi-directional plugin functionality, where the API pro...

(EN_COMPOUNDS_BI_DIRECTIONAL)


8-14: Clear explanation of workspace plugin challenges.

This section effectively outlines the problem with local workspace plugins in production and introduces the solution approach.

🧰 Tools
🪛 LanguageTool

[grammar] ~13-~13: Check that the noun ‘vendor’ after the pronoun ‘we’ is correct. It’s possible that you may need to switch to a possessive pronoun, or use another part of speech.
Context: ...a npm during production. To solve this, we vendor them inside dist/plugins. To prevent ...

(PRP_VB)


16-27: Excellent configuration example.

The JSON example clearly demonstrates how to properly configure workspace dependencies as optional peer dependencies.


29-31: Good explanation of optional dependency behavior.

This explains why marking the dependency as optional solves the potential build-time and run-time issues.

api/package.json (2)

23-23: Enhanced postbuild script supports plugin copying.

Adding the plugin copying step to the postbuild script ensures that workspace plugins are properly included in the distribution.


139-146: Well-structured peer dependency configuration.

The implementation of unraid-api-plugin-connect as an optional peer dependency follows the pattern described in the documentation, making the code consistent with the guidance provided.

api/src/unraid-api/plugin/plugin.service.ts (3)

51-65: Robust plugin import strategy with fallback mechanisms

The implementation now attempts to import plugins from multiple sources using Promise.any, providing better resilience when handling both standard and workspace plugins. The detailed comments effectively explain the rationale behind this approach.


76-76: Good addition of logging for successful plugin count

Adding this log statement improves observability of the plugin loading process, which will be helpful for monitoring and debugging.


84-89:

❓ Verification inconclusive

Architectural shift from dependencies to peerDependencies

Changing from dependencies to peerDependencies is aligned with modern plugin architecture best practices, making plugins optional and avoiding dependency duplication.


🏁 Script executed:

#!/bin/bash
# Check if any plugin references still use dependencies instead of peerDependencies
rg -A 3 -B 3 "\.dependencies" --type ts | grep -v "peerDependencies"

Length of output: 68


Action: Verify Peer Dependency Shift in Plugin Service

The update in api/src/unraid-api/plugin/plugin.service.ts (lines 84–89) correctly shifts from using dependencies to peerDependencies, aligning with modern plugin architecture best practices. The change ensures that plugins are optional and prevents dependency duplication.

  • The code now extracts peerDependencies from getPackageJson() and logs a warning if they are missing.
  • The automated script did not produce output, so please manually verify that no outdated references to dependencies remain in the repository.
api/scripts/build.ts (4)

2-2: Simplified imports

Removing unused imports keeps the code clean and focused.


5-5: Improved type safety with custom ApiPackageJson type

The new type alias enhances type safety by explicitly defining required properties of the package.json.

Also applies to: 10-13


28-28: Type assertion updated for package.json parsing

This change properly applies the new ApiPackageJson type to the parsed JSON.


33-33: Clarified comment about dependency handling

The updated comment provides better context about the purpose of emptying devDependencies.

@pujitm pujitm merged commit 8bb9efc into main Apr 10, 2025
9 checks passed
@pujitm pujitm deleted the refactor/config-defs branch April 10, 2025 18:17
mdatelle pushed a commit that referenced this pull request Apr 14, 2025
## Summary by CodeRabbit

- **New Features**
- Introduced an automated step in the post-build process to copy plugin
assets.
- Enhanced the plugin import process by supporting multiple sourcing
options.
  - Adds a demo `health` query via a workspace plugin.

- **Documentation**
- Added a detailed guide explaining API plugin configuration and local
workspace integration.

- **Refactor**
- Improved dependency handling by marking certain workspace plugins as
optional.
- Updated deployment synchronization to ensure destination directories
exactly mirror the source.
- Refined logging levels and type-safety for improved reliability and
debugging.
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.

3 participants