MCPcopy
hub / github.com/ZToolsCenter/ZTools / getDefaultFeatureCode

Method getDefaultFeatureCode

src/main/api/renderer/commands.ts:835–883  ·  view source on GitHub ↗

* 获取插件的默认 featureCode(第一个非匹配 feature)

(
    pluginPath: string
  )

Source from the content-addressed store, hash-verified

833 * 获取插件的默认 featureCode(第一个非匹配 feature)
834 */
835 private async getDefaultFeatureCode(
836 pluginPath: string
837 ): Promise<{ success: boolean; featureCode?: string; error?: string }> {
838 try {
839 const pluginJsonPath = path.join(pluginPath, 'plugin.json')
840 const pluginConfig = JSON.parse(await fs.readFile(pluginJsonPath, 'utf-8'))
841
842 if (!pluginConfig.features || pluginConfig.features.length === 0) {
843 return {
844 success: false,
845 error: '该插件没有配置任何功能'
846 }
847 }
848
849 // 查找第一个非匹配 feature
850 for (const feature of pluginConfig.features) {
851 if (!feature.cmds || feature.cmds.length === 0) {
852 // 没有 cmds 的 feature,使用它
853 return { success: true, featureCode: feature.code }
854 }
855
856 // 检查是否有非匹配型命令
857 const hasNonMatchCmd = feature.cmds.some((cmd: any) => {
858 // 如果是字符串,就是文本命令(非匹配)
859 if (typeof cmd === 'string') return true
860 // 如果是对象但没有 type 字段,也算非匹配
861 if (typeof cmd === 'object' && !cmd.type) return true
862 // 否则是匹配型命令(regex 或 over)
863 return false
864 })
865
866 if (hasNonMatchCmd) {
867 return { success: true, featureCode: feature.code }
868 }
869 }
870
871 // 如果都是匹配型 feature,返回错误
872 return {
873 success: false,
874 error: '该插件所有功能都需要通过指令触发,无法直接打开'
875 }
876 } catch (error) {
877 console.error('[Commands] 读取插件配置失败:', error)
878 return {
879 success: false,
880 error: '读取插件配置失败'
881 }
882 }
883 }
884
885 /**
886 * 从历史记录中删除

Callers 1

launchMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected