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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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. 这样可以提供更好的类型安全性和代码提示

}

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) { }

Comment on lines +130 to +137
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. 在try-catch块中静默忽略所有错误,这可能使调试变得困难
  2. 没有对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.

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

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]': '扩展脚手架的导航步骤',
'-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