Skip to content
Merged
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
30 changes: 29 additions & 1 deletion packages/taro-cli/src/presets/commands/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,36 @@ export default (ctx: IPluginContext) => {
})
}

// 单独function获取@jdtaro相关的包
function getJdtaroPackages(ctx) {
try {
const { appPath } = ctx.paths
const fs = ctx.helper.fs
const packageJsonPath = path.join(appPath, 'package.json')

if (fs.existsSync(packageJsonPath)) {
const packageJsonContent = fs.readFileSync(packageJsonPath, 'utf8')
const packageJson = JSON.parse(packageJsonContent)
const dependencies = Object.assign({}, packageJson.dependencies || {}, packageJson.devDependencies || {})

// 筛选出@jdtaro相关的包
return Object.keys(dependencies).filter(pkg => pkg.startsWith('@jdtaro/'))
}
} catch (error) {
// 记录错误但不中断程序执行(添加trycatch)
console.error('读取或解析package.json时发生错误:', error.message)
}

return []
}
Comment on lines +38 to +59
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

添加类型注解并改进函数实现

新增的 getJdtaroPackages 函数存在以下问题:

  1. 缺少参数和返回值的 TypeScript 类型注解
  2. 函数命名和注释风格与项目不一致
  3. 可以使用更简洁的实现方式

应用以下差异来改进函数实现:

-// 单独function获取@jdtaro相关的包
-function getJdtaroPackages(ctx) {
+/**
+ * 获取项目中所有 @jdtaro 相关的依赖包
+ */
+function getJdtaroPackages(ctx: IPluginContext): string[] {
   try {
-    const { appPath } = ctx.paths
-    const fs = ctx.helper.fs
+    const { fs } = ctx.helper
+    const { appPath } = ctx.paths
     const packageJsonPath = path.join(appPath, 'package.json')

     if (fs.existsSync(packageJsonPath)) {
       const packageJsonContent = fs.readFileSync(packageJsonPath, 'utf8')
       const packageJson = JSON.parse(packageJsonContent)
-      const dependencies = Object.assign({}, packageJson.dependencies || {}, packageJson.devDependencies || {})
+      const dependencies = { ...packageJson.dependencies, ...packageJson.devDependencies }

-      // 筛选出@jdtaro相关的包
       return Object.keys(dependencies).filter(pkg => pkg.startsWith('@jdtaro/'))
     }
   } catch (error) {
-    // 记录错误但不中断程序执行(添加trycatch)
-    console.error('读取或解析package.json时发生错误:', error.message)
+    console.error('读取或解析 package.json 时发生错误:', error.message)
   }

   return []
 }

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In packages/taro-cli/src/presets/commands/info.ts around lines 38 to 59, the
getJdtaroPackages function lacks TypeScript type annotations for its parameter
and return value, uses inconsistent naming and comment style, and can be
simplified. Add appropriate TypeScript types for the ctx parameter and the
returned array of strings, rename the function and comments to match the
project's naming conventions and style, and refactor the implementation to be
more concise, such as using optional chaining and direct filtering without
intermediate variables.


async function info (options, ctx) {
const npmPackages = ctx.helper.UPDATE_PACKAGE_LIST.concat(['react', 'react-native', 'expo', 'taro-ui'])
let npmPackages = ctx.helper.UPDATE_PACKAGE_LIST.concat(['react', 'react-native', 'expo', 'taro-ui'])

// 调用新函数获取@jdtaro相关包
const jdtaroPackages = getJdtaroPackages(ctx)
npmPackages = npmPackages.concat(jdtaroPackages)

const info = await envinfo.run(Object.assign({}, {
System: ['OS', 'Shell'],
Binaries: ['Node', 'Yarn', 'npm'],
Expand Down