Skip to content

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

Merged
merged 6 commits into from
Jun 30, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/taro-cli/src/create/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export interface IProjectConf {
hideDefaultTemplate?: boolean
framework: FrameworkType
compiler?: CompilerType
ask?: Function
Copy link

@coderabbitai coderabbitai bot May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

不要使用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.

Suggested change
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.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里补齐一下类型

Copy link

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

这个类型定义明确了:

  1. 函数接收一个参数,包含配置(不包含 ask 属性本身)和模板路径
  2. 函数可以返回 Promise 或 void,支持异步和同步两种方式
  3. 这样可以提供更好的类型安全性和代码提示

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个ask是一个回调函数,我需要将自定义ask的执行逻辑从外部组件传入进来。

Copy link

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!

}

type CustomPartial<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
Expand Down Expand Up @@ -126,6 +127,14 @@ export default class Project extends Creator {
await this.askTemplate(conf, prompts, templates)
const templateChoiceAnswer = await inquirer.prompt<IProjectConf>(prompts)

// 导航步骤扩展
try {
if (typeof conf.ask === 'function') {
const { ask, ...other } = conf
await conf.ask({ ...other, templatePath: this.templatePath(templateChoiceAnswer.template) })
}
} catch (e) { }

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

改进错误处理并修复未使用变量的问题

当前实现存在以下问题:

  1. 静默忽略所有错误,不利于调试和问题排查
  2. 解构出的 ask 变量未被使用,触发 ESLint 警告
  3. 应该提供适当的错误信息以帮助开发者排查问题
// 导航步骤扩展
try {
  if (typeof conf.ask === 'function') {
-    const { ask, ...other } = conf
-    await conf.ask({ ...other, templatePath: this.templatePath(templateChoiceAnswer.template) })
+    const { ask: _, ...other } = conf
+    await conf.ask({ ...other, templatePath: this.templatePath(templateChoiceAnswer.template) })
  }
-} catch (e) { }
+} catch (e) {
+  console.log(chalk.yellow('执行导航步骤扩展时发生错误: '), e)
+}

改进说明:

  • 使用 ask: _ 来避免未使用变量警告
  • 添加错误日志输出,方便调试和问题定位
  • 保持了原有的容错机制,不会因扩展失败而中断整个流程
📝 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.

Suggested change
// 导航步骤扩展
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
code silently catches all errors without logging, and the destructured variable
'ask' is unused, causing ESLint warnings. To fix this, rename 'ask' to 'ask: _'
to avoid the unused variable warning, and add a console.error or logger.error
statement inside the catch block to output the caught error for debugging, while
preserving the try-catch to prevent the process from stopping on errors.

return {
...answers,
...compilerAndTemplateSourceAnswer,
Expand Down
5 changes: 4 additions & 1 deletion packages/taro-cli/src/presets/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default (ctx: IPluginContext) => {
'--template [template]': '项目模板',
'--css [css]': 'CSS预处理器(sass/less/stylus/none)',
'--autoInstall': '自动安装依赖',
'--ask [ask]': '扩展脚手架的导航步骤',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这是命令行参数,这里加--ask没有意义,无法传递function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是的,可以删除掉这里。

'-h, --help': 'output usage information'
},
async fn (opts) {
Expand All @@ -34,6 +35,7 @@ export default (ctx: IPluginContext) => {
hideDefaultTemplate,
sourceRoot,
autoInstall,
ask
} = opts.options

const Project = require('../../create/project').default
Expand All @@ -52,7 +54,8 @@ export default (ctx: IPluginContext) => {
compiler,
hideDefaultTemplate,
autoInstall,
css
css,
ask
})

project.create()
Expand Down