-
Notifications
You must be signed in to change notification settings - Fork 268
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
refactor: new components are generated automatically #2940
Conversation
变更概述演练此次变更主要涉及 变更
建议标签
建议评审人
诗歌
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: 1
🧹 Nitpick comments (2)
scripts/demo.js (1)
1-1
: 建议:为函数添加参数校验在第1行,
demoModel
函数新增了cName
和desc
参数。建议在函数中添加对这两个参数的有效性校验,确保其类型和内容符合预期,防止后续使用时出现错误。scripts/create-component-mode.js (1)
236-242
: 提示:统一错误处理在第236-242行的
createNew
函数中,对Promise.all(tasks)
使用了try...catch
,这很好地捕获了异步操作的错误。为了保持代码风格的一致性,建议在其他异步函数中也添加错误处理,例如在createReact
和createDemo
等函数中。
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
package.json
(1 hunks)scripts/create-component-mode.js
(1 hunks)scripts/demo.js
(1 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
scripts/create-component-mode.js
[error] 33-35: Change to an optional chain.
Unsafe fix: Change to an optional chain.
(lint/complexity/useOptionalChain)
🔇 Additional comments (3)
scripts/demo.js (2)
90-90
: 确认:类型导出的正确性第90行,导出类型
export type { ${name}Props } from './types'
。请确保./types
文件中正确定义了${name}Props
类型,并且路径无误。
153-153
:⚠️ Potential issue修正:模板字符串中的变量名
第153行的
doc
模板中,import { name } from '@nutui/nutui-react'
可能存在错误,name
应为模板字符串变量${name}
。建议修改为:
-import { name } from '@nutui/nutui-react' +import { ${name} } from '@nutui/nutui-react'Likely invalid or redundant comment.
package.json (1)
156-156
: 确认:新依赖包的版本和兼容性在第156行,添加了
"@inquirer/prompts":"^7.2.3"
,同时移除了"inquirer":"^10.1.4"
。请确保@inquirer/prompts
版本7.2.3
与项目其他依赖兼容,并且在代码中已正确替换inquirer
的用法。
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/create-component-mode.js (3)
18-19
: 为新属性添加注释说明新增的
v15
和dd
属性缺少注释说明,这可能会影响其他开发者对这些属性的理解和正确使用。建议添加注释说明这些属性的用途:
const newCpt = { version: '3.0.0', name: '', type: '', cName: '', desc: '', sort: '', show: true, taro: true, + // 是否兼容 v1.5 版本 v15: false, + // 是否支持 DD 小程序 dd: true, author: '', }
96-105
: 优化:提取重复的文件创建逻辑多个函数中存在相似的文件创建逻辑,建议提取为一个通用的辅助函数以减少代码重复。
建议创建一个通用的文件创建函数:
async function createFiles(dirPath: string, files: Array<{path: string, content: string}>) { for (const file of files) { const filePath = path.join(dirPath, file.path) await createFile(filePath, file.content) } }然后在各个函数中复用:
async function createReact() { const nameLc = newCpt.name.toLowerCase() const name = newCpt.name const dirPath = path.join(__dirname, `../src/packages/${nameLc}/`) await ensureDir(dirPath) const files = [ { path: `${nameLc}.tsx`, content: demoModel(name).react }, { path: `index.ts`, content: demoModel(name).index }, { path: `types.ts`, content: demoModel(name).types }, ] - for (const file of files) { - const filePath = path.join(dirPath, file.path) - await createFile(filePath, file.content) - } + await createFiles(dirPath, files) }Also applies to: 114-122, 134-141, 153-166
233-240
: 优化:统一错误处理的语言并优化进程退出错误处理中混合使用了中英文,且进程退出可以更优雅。
建议修改为:
try { await Promise.all(tasks) - console.log('组件模板生成完毕,请开始你的表演~') + console.log('✨ 组件模板生成完毕!') process.exit() } catch (err) { - console.error('Error during component creation:', err) + console.error('❌ 组件创建失败:', err) process.exit(1) }另外建议添加进程清理:
+ process.on('SIGINT', () => { + console.log('\n🛑 组件创建已取消') + process.exit(0) + })
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
scripts/create-component-mode.js
(1 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
scripts/create-component-mode.js
[error] 33-35: Change to an optional chain.
Unsafe fix: Change to an optional chain.
(lint/complexity/useOptionalChain)
🔇 Additional comments (1)
scripts/create-component-mode.js (1)
28-30
: 优化:避免可能的空值引用在使用
nav.some()
和category.packages.some()
时,如果nav
或category.packages
为undefined
,可能会导致运行时错误。建议修改为:
- const repeat = nav.some((category) => - category.packages.some((pkg) => pkg.name === value) - ) + const repeat = nav?.some((category) => + category.packages?.some((pkg) => pkg.name === value) + )
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## feat_v3.x #2940 +/- ##
==========================================
Coverage 85.58% 85.58%
==========================================
Files 277 277
Lines 18185 18185
Branches 2763 2763
==========================================
Hits 15563 15563
Misses 2617 2617
Partials 5 5 ☔ View full report in Codecov by Sentry. |
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
🧹 Nitpick comments (2)
scripts/create-component-mode.js (2)
9-21
: 需要为新属性添加注释说明
newCpt
对象新增了v15
和dd
属性,但缺少相关说明。建议添加注释来解释这些属性的用途。建议添加如下注释:
const newCpt = { version: '3.0.0', name: '', type: '', cName: '', desc: '', sort: '', show: true, taro: true, + // 是否支持 Vue 1.5 版本 v15: false, + // 是否支持按需引入 dd: true, author: '', }
220-241
: 建议优化任务执行顺序当前使用
Promise.all
并行执行所有任务,但某些任务可能存在依赖关系。建议将任务分组,按照依赖顺序执行。建议重构如下:
async function createNew() { try { // 第一组:基础文件创建 await Promise.all([ createIndexJs(), createScss(), createDoc(), ]); // 第二组:组件文件创建 await Promise.all([ createReact(), createReactTaro(), ]); // 第三组:示例和测试文件创建 await Promise.all([ createDemo(), createTaroDemo(), createTest(), ]); // 最后:更新配置 await addToPackageJson(); console.log('组件模板生成完毕,请开始你的表演~'); process.exit(); } catch (err) { console.error('Error during component creation:', err); process.exit(1); } }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
pnpm-lock.yaml
is excluded by!**/pnpm-lock.yaml
📒 Files selected for processing (1)
scripts/create-component-mode.js
(1 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
scripts/create-component-mode.js
[error] 33-35: Change to an optional chain.
Unsafe fix: Change to an optional chain.
(lint/complexity/useOptionalChain)
⏰ Context from checks skipped due to timeout of 90000ms (2)
- GitHub Check: build
- GitHub Check: test
🔇 Additional comments (1)
scripts/create-component-mode.js (1)
64-82
: 实现完善,错误处理得当!
ensureDir
和createFile
函数实现了良好的错误处理机制,并提供了清晰的错误日志。
async function createIndexJs() { | ||
const destPath = path.join('src/packages', newCpt.name.toLowerCase()) |
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.
安全警告:路径构造可能存在安全隐患
在构造文件路径时直接使用了用户输入(newCpt.name
),这可能导致路径遍历漏洞。建议添加路径验证。
建议添加路径验证函数:
function validateComponentName(name) {
// 只允许字母、数字和连字符
const validPattern = /^[A-Za-z0-9-]+$/;
if (!validPattern.test(name)) {
throw new Error('组件名称只能包含字母、数字和连字符');
}
// 防止路径遍历
if (name.includes('.') || name.includes('/') || name.includes('\\')) {
throw new Error('组件名称包含非法字符');
}
}
Also applies to: 90-94
🤔 这个变动的性质是?
🔗 相关 Issue
💡 需求背景和解决方案
☑️ 请求合并前的自查清单
Summary by CodeRabbit
依赖变更
@inquirer/prompts@^7.2.3
inquirer@^10.1.4
脚本更新
演示代码生成