-
Notifications
You must be signed in to change notification settings - Fork 4.8k
taro-cli新增支持可动态扩展ask导航步骤 #17715
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
taro-cli新增支持可动态扩展ask导航步骤 #17715
Conversation
Walkthrough本次更改为项目初始化流程引入了可扩展的自定义交互步骤。通过在项目配置接口中新增可选的 Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant CLI
participant Project
User->>CLI: 执行 init 命令(可带 --ask 参数)
CLI->>Project: 创建 Project 实例(传入 ask 选项)
Project->>User: 交互式模板选择
alt 存在 ask 函数
Project->>ask: 调用 ask(conf, templatePath)
ask-->>Project: 自定义交互逻辑完成
end
Project->>CLI: 初始化流程继续
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
✨ Finishing Touches
🧪 Generate Unit Tests
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:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. 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: 2
🧹 Nitpick comments (2)
packages/taro-cli/src/create/project.ts (2)
133-133
: 未使用的变量从对象中解构出
ask
变量后未使用它。虽然这是为了从other
对象中排除ask
属性,但仍会被静态分析工具标记为未使用变量。你可以考虑使用TypeScript的类型系统来解决这个问题:
- const { ask, ...other } = conf + const other = { ...conf } as Omit<typeof conf, 'ask'> + delete other.ask或者,如果想保持原来的解构方式,可以添加注释来抑制lint警告:
- const { ask, ...other } = conf + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const { ask, ...other } = conf🧰 Tools
🪛 ESLint
[error] 133-133: 'ask' is assigned a value but never used.
(@typescript-eslint/no-unused-vars)
130-137
: 考虑添加文档注释建议为这个新增的导航步骤扩展功能添加JSDoc文档注释,说明这个功能的用途、参数和用法示例,这将有助于其他开发者理解和使用这个功能。
+/** + * 导航步骤扩展 + * 允许外部组件通过ask函数传递自定义提示,实现额外的配置问题 + * 例如:"是否需要编译为ES5?"或"是否开启监控?"等 + * + * @param {Object} config - 当前配置信息 + * @param {string} config.templatePath - 选择的模板路径 + * @returns {Promise<void>|void} + */ try { if (typeof conf.ask === 'function') {🧰 Tools
🪛 ESLint
[error] 133-133: 'ask' is assigned a value but never used.
(@typescript-eslint/no-unused-vars)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
packages/taro-cli/src/create/project.ts
(2 hunks)packages/taro-cli/src/presets/commands/init.ts
(3 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
packages/taro-cli/src/create/project.ts
[error] 46-46: Don't use 'Function' as a type.
Prefer explicitly define the function shape. This type accepts any function-like value, which can be a common source of bugs.
(lint/complexity/noBannedTypes)
🪛 ESLint
packages/taro-cli/src/create/project.ts
[error] 46-46: Don't use Function
as a type. The Function
type accepts any function-like value.
It provides no type safety when calling the function, which can be a common source of bugs.
It also accepts things like class declarations, which will throw at runtime as they will not be called with new
.
If you are expecting the function to accept certain arguments, you should explicitly define the function shape.
(@typescript-eslint/ban-types)
[error] 133-133: 'ask' is assigned a value but never used.
(@typescript-eslint/no-unused-vars)
🔇 Additional comments (4)
packages/taro-cli/src/presets/commands/init.ts (3)
17-17
: 新选项添加正确此处成功添加了
--ask [ask]
命令行选项,并提供了适当的描述"扩展脚手架的导航步骤"。这符合PR的目标,即允许动态扩展初始化过程中的导航步骤。
38-38
: 从选项中正确提取ask参数正确地从命令行选项中提取了ask参数,以便后续传递给Project构造函数。
57-58
: 成功将ask参数传递给Project构造函数成功将css和ask参数传递给Project构造函数,使其可以在项目创建过程中使用。
packages/taro-cli/src/create/project.ts (1)
134-134
:❓ Verification inconclusive
确保调用模板参数是安全的
在调用外部
ask
函数时,确保传递的templatePath
参数是有效的。当templateChoiceAnswer.template
为空或未定义时可能会出现问题。
🏁 Script executed:
#!/bin/bash # 检查 templatePath 的生成是否可能出现空值情况 ast-grep --pattern "this.templatePath($_)" | grep -v "//\|test"Length of output: 213
以下脚本直接定位到
packages/taro-cli/src/create/project.ts
,查看templatePath
方法及其调用,确认是否存在未校验情况:#!/bin/bash # 定位 templatePath 方法定义 rg -n "templatePath" packages/taro-cli/src/create/project.ts # 定位该方法的调用处 rg -n "this\.templatePath" packages/taro-cli/src/create/project.ts
下面运行脚本,定位
templatePath
方法定义及templateChoiceAnswer
的来源,确认是否已对空或未定义的模板值做了校验:#!/bin/bash set -e # 查找 templatePath 方法定义及上下文 rg -n -C3 "templatePath\s*\(" packages/taro-cli/src/create/project.ts # 查找 templateChoiceAnswer 的赋值或来源 rg -n -C3 "templateChoiceAnswer" packages/taro-cli/src/create/project.ts # 查找 ask 调用处,确认传入值是否有预校验 rg -n -C3 "conf\.ask" packages/taro-cli/src/create/project.ts
确保
templatePath
参数已校验
- 文件:packages/taro-cli/src/create/project.ts 第 134 行
- 场景:
await conf.ask({ …other, templatePath: this.templatePath(templateChoiceAnswer.template) })- 风险:若
templateChoiceAnswer.template
为空或未定义,this.templatePath(undefined)
会导致路径拼装出错- 建议:在调用前对
templateChoiceAnswer.template
添加非空校验,例如:if (!templateChoiceAnswer.template) { throw new Error('请选择有效的模板'); } await conf.ask({ …other, templatePath: this.templatePath(templateChoiceAnswer.template) });
@@ -43,6 +43,7 @@ export interface IProjectConf { | |||
hideDefaultTemplate?: boolean | |||
framework: FrameworkType | |||
compiler?: CompilerType | |||
ask?: Function |
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.
不要使用Function
作为类型
避免使用Function
作为类型声明,这无法提供类型安全性。建议定义具体的函数类型,明确指定参数和返回值类型。
- ask?: Function
+ ask?: (config: Omit<IProjectConf, 'ask'> & { templatePath: string }) => Promise<void> | void
📝 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.
ask?: Function | |
ask?: (config: Omit<IProjectConf, 'ask'> & { templatePath: string }) => Promise<void> | void |
🧰 Tools
🪛 Biome (1.9.4)
[error] 46-46: Don't use 'Function' as a type.
Prefer explicitly define the function shape. This type accepts any function-like value, which can be a common source of bugs.
(lint/complexity/noBannedTypes)
🪛 ESLint
[error] 46-46: Don't use Function
as a type. The Function
type accepts any function-like value.
It provides no type safety when calling the function, which can be a common source of bugs.
It also accepts things like class declarations, which will throw at runtime as they will not be called with new
.
If you are expecting the function to accept certain arguments, you should explicitly define the function shape.
(@typescript-eslint/ban-types)
🤖 Prompt for AI Agents
In packages/taro-cli/src/create/project.ts at line 46, replace the use of the
generic `Function` type for the `ask` property with a specific function type
that clearly defines the expected parameters and return type. This will improve
type safety by explicitly specifying what kind of function `ask` should be,
rather than using the broad and unsafe `Function` type.
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.
@Single-Dancer 是的,我将补充具体的函数类型,替换掉泛型的 Function
类型:
ask?: (config: Omit<IProjectConf, 'ask'> & { templatePath: string }) => Promise<void> | void
这个类型定义明确了:
- 函数接收一个参数,包含配置(不包含 ask 属性本身)和模板路径
- 函数可以返回 Promise 或 void,支持异步和同步两种方式
- 这样可以提供更好的类型安全性和代码提示
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.
这个ask是一个回调函数,我需要将自定义ask的执行逻辑从外部组件传入进来。
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.
Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!
@@ -14,6 +14,7 @@ export default (ctx: IPluginContext) => { | |||
'--template [template]': '项目模板', | |||
'--css [css]': 'CSS预处理器(sass/less/stylus/none)', | |||
'--autoInstall': '自动安装依赖', | |||
'--ask [ask]': '扩展脚手架的导航步骤', |
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.
这是命令行参数,这里加--ask没有意义,无法传递function
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.
是的,可以删除掉这里。
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #17715 +/- ##
========================================
Coverage 54.98% 54.98%
========================================
Files 417 417
Lines 21693 21693
Branches 5380 5362 -18
========================================
Hits 11928 11928
- Misses 8091 8217 +126
+ Partials 1674 1548 -126
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
这个 PR 做了什么? (简要描述所做更改)
在使用自定义组件创建项目时,需要对项目在init过程中的导航进行扩展,例如:与"? 是否需要编译为 ES5 ?"类似的 "? 是否开启监控?"等。因此本次对taro-cli进行修改,实现外部组件可通过传参的方式传入自定义ask。
这个 PR 是什么类型? (至少选择一个)
这个 PR 涉及以下平台:
Summary by CodeRabbit
--ask
参数,用于扩展脚手架的导航步骤,支持自定义交互流程。ask
回调函数,允许在模板选择后执行自定义异步操作。