-
Notifications
You must be signed in to change notification settings - Fork 400
feat: 增强组件管理功能 #1304
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
base: develop
Are you sure you want to change the base?
feat: 增强组件管理功能 #1304
Conversation
1. 添加组件数据比较和更新功能 2. 从materials/packages.json读取最新物料包配置 3. 修复组件验证和关联方法中的问题
WalkthroughThe pull request enhances two scripts. In the build process, Changes
Sequence Diagram(s)sequenceDiagram
participant GM as generateComponents
participant MI as Material Info Reader
participant PK as packages.json Data
GM->>MI: Read packages.json
MI-->>GM: Return parsed data
GM->>PK: Check if material.npm.package exists
PK-->>GM: Return matching package information
GM->>GM: Merge package properties into material.npm
sequenceDiagram
participant IC as initDB
participant DB as Database
participant ICD as isComponentDifferent
participant NC as normalizeJsonValue
participant UC as updateComponent
IC->>DB: Query existing component
DB-->>IC: Return component data
IC->>ICD: Compare new vs. existing component
ICD->>NC: Standardize JSON values for comparison
ICD-->>IC: Return comparison result
alt Components differ
IC->>UC: Call updateComponent
else No differences
IC->>IC: Continue processing
end
Poem
Tip ⚡💬 Agentic Chat (Pro Plan, General Availability)
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 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 (3)
scripts/buildMaterials.mjs (1)
120-129
: Good implementation of dynamic package configuration update.The code correctly checks for package references in the material's npm configuration and updates them with the latest information from the centralized packages.json file. This enables maintaining package versions in a single location.
However, consider adding error handling in case the material has a package that doesn't exist in materialInfo.packages.
if (materialPackageInfo?.package) { const latestPackageInfo = materialInfo.packages.find((item) => item.package === materialPackageInfo.package) if (latestPackageInfo) { const { name: _name, ...rest } = latestPackageInfo material.npm = Object.assign(material.npm, rest) + } else { + logger.warn(`Package "${materialPackageInfo.package}" referenced in component but not found in packages.json`) } }scripts/connection.mjs (2)
135-184
: Good implementation of component comparison logic.The method thoroughly compares component fields, handling both primitive and object types appropriately. The normalization of JSON values ensures accurate comparison regardless of the serialization format.
There are two minor issues that could be improved:
- There's a redundant assignment of
dbField
at line 171 (it was already assigned at line 152)- Consider using the existing logger instead of console.log for consistency
} else { // 对于基本类型,直接比较 - const dbField = this.fieldTransform(field) const newValue = String(newComponent[field]) const dbValue = String(existingComponent[dbField]) if (newValue !== dbValue) { - console.log(`${newComponent.component}字段 ${field} 需要更新: 新值: ${newValue} 旧值: ${dbValue}`) + logger.info(`${newComponent.component}字段 ${field} 需要更新: 新值: ${newValue} 旧值: ${dbValue}`) return true } }
456-463
: Enhanced component update logic.The code now intelligently determines if a component needs updating by comparing its fields with the database version, avoiding unnecessary updates. This improves performance and ensures that only changed components are updated.
Consider adding a debug log when a component doesn't need updating to provide better visibility into the process.
const needUpdate = this.isComponentDifferent(component, existingComponent) if (needUpdate) { this.updateComponent(component) +} else { + logger.debug(`${component.component} is up to date, no update needed`) }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
scripts/buildMaterials.mjs
(2 hunks)scripts/connection.mjs
(4 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
- GitHub Check: push-check
🔇 Additional comments (3)
scripts/buildMaterials.mjs (1)
20-21
: Added feature to read package configurations from a central file.This enhancement allows the application to read material package configurations from a centralized
packages.json
file.scripts/connection.mjs (2)
97-133
: Well-implemented JSON value normalization method.This utility method effectively handles multiple JSON parsing scenarios, including nested JSON strings and different data types. The code is well-commented and provides a robust foundation for standardizing JSON values before comparison.
196-196
: Improved error logging with fallback for unknown files.This change enhances error reporting by handling cases where the file path is unknown, providing a clearer error message.
const parsed = JSON.parse(value) | ||
|
||
// 如果解析结果仍然是字符串,且看起来像JSON,再次尝试解析 | ||
if (typeof parsed === 'string' && parsed.startsWith('{') && parsed.endsWith('}')) { |
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.
什么场景需要解析两遍?能举个示例吗?
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.
像 npm、snippets、configure 这几个字段,他原来是对象,从数据库获取后,大概会变成 '{"\name": "xxx"}'
如果不进一步对他们进行解析,无法获取最终的对象
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.
那应该是对子属性进一步解析吧?应该没有字符串是需要两遍 JSON.parse
才能变成最终对象的?
|
||
// 检查每个字段是否有差异 | ||
for (const field of fieldsToCompare) { | ||
if (!newComponent[field] && !existingComponent[this.fieldTransform(field)]) { |
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.
这里如果新值是 false
,旧值是 undefined,那么是不是也不会触发更新?
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.
是的,你这么一说,这里应该优化一下
English | 简体中文
PR
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
Background and solution
What is the current behavior?
Issue Number: N/A
What is the new behavior?
materials/packages.json 下更新依赖信息后,可通过 buildMaterial 指令同步更新到数据库
Does this PR introduce a breaking change?
Other information
Summary by CodeRabbit
New Features
Bug Fixes