-
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
base: main
Are you sure you want to change the base?
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
Note ⚡️ AI Code Reviews for VS Code, Cursor, WindsurfCodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback. Note ⚡️ Faster reviews with cachingCodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure ✨ 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:
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.
// 导航步骤扩展 | ||
try { | ||
if (typeof conf.ask === 'function') { | ||
const { ask, ...other } = conf | ||
await conf.ask({ ...other, templatePath: this.templatePath(templateChoiceAnswer.template) }) | ||
} | ||
} catch (e) { } | ||
|
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.
🛠️ Refactor suggestion
改进错误处理并确保类型安全
当前实现中有以下几个问题:
- 在try-catch块中静默忽略所有错误,这可能使调试变得困难
- 没有对
ask
函数的调用结果进行处理
建议改进错误处理并确保类型安全。
// 导航步骤扩展
try {
if (typeof conf.ask === 'function') {
const { ask, ...other } = conf
- await conf.ask({ ...other, templatePath: this.templatePath(templateChoiceAnswer.template) })
+ await conf.ask({ ...other, templatePath: this.templatePath(templateChoiceAnswer.template) })
}
-} catch (e) { }
+} catch (e) {
+ console.log(chalk.yellow('执行导航步骤扩展时发生错误: '), e)
+}
📝 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.
// 导航步骤扩展 | |
try { | |
if (typeof conf.ask === 'function') { | |
const { ask, ...other } = conf | |
await conf.ask({ ...other, templatePath: this.templatePath(templateChoiceAnswer.template) }) | |
} | |
} catch (e) { } | |
// 导航步骤扩展 | |
try { | |
if (typeof conf.ask === 'function') { | |
const { ask, ...other } = conf | |
await conf.ask({ ...other, templatePath: this.templatePath(templateChoiceAnswer.template) }) | |
} | |
} catch (e) { | |
console.log(chalk.yellow('执行导航步骤扩展时发生错误: '), e) | |
} |
🧰 Tools
🪛 ESLint
[error] 133-133: 'ask' is assigned a value but never used.
(@typescript-eslint/no-unused-vars)
🤖 Prompt for AI Agents
In packages/taro-cli/src/create/project.ts around lines 130 to 137, the current
try-catch block silently ignores all errors and does not handle the result of
the ask function. To fix this, add proper error logging or handling inside the
catch block to avoid silent failures, and ensure the call to conf.ask is awaited
and its result is handled or returned as appropriate. Also, add type checks or
assertions to guarantee type safety when calling conf.ask.
这个 PR 做了什么? (简要描述所做更改)
在使用自定义组件创建项目时,需要对项目在init过程中的导航进行扩展,例如:与"? 是否需要编译为 ES5 ?"类似的 "? 是否开启监控?"等。因此本次对taro-cli进行修改,实现外部组件可通过传参的方式传入自定义ask。
这个 PR 是什么类型? (至少选择一个)
这个 PR 涉及以下平台:
Summary by CodeRabbit
--ask
参数,用于扩展脚手架的导航步骤,支持自定义交互流程。