优化性能

This commit is contained in:
user123
2026-01-28 22:35:05 +08:00
parent 589d1ce2b8
commit 46acfcf2fa
3 changed files with 76 additions and 33 deletions

Binary file not shown.

View File

@@ -11,6 +11,14 @@ import (
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"sync"
)
var (
cachedNpmPrefix string
cachedNodePath string
prefixOnce sync.Once
nodePathOnce sync.Once
) )
// Config Structures // Config Structures
@@ -105,18 +113,11 @@ func GetMoltbotPath() (string, error) {
return path, nil return path, nil
} }
npmPath, err := getNpmPath() npmPrefix, err := getNpmPrefix()
if err != nil { if err != nil {
return "", fmt.Errorf("无法定位 npm: %v", err) return "", err
} }
cmd := exec.Command(npmPath, "config", "get", "prefix")
out, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("无法获取 npm prefix: %v", err)
}
npmPrefix := strings.TrimSpace(string(out))
// 检查 clawdbot.cmd // 检查 clawdbot.cmd
possibleClawd := filepath.Join(npmPrefix, "clawdbot.cmd") possibleClawd := filepath.Join(npmPrefix, "clawdbot.cmd")
if _, err := os.Stat(possibleClawd); err == nil { if _, err := os.Stat(possibleClawd); err == nil {
@@ -134,12 +135,24 @@ func GetMoltbotPath() (string, error) {
// GetNodePath 获取 Node.js 可执行文件的绝对路径 // GetNodePath 获取 Node.js 可执行文件的绝对路径
func GetNodePath() (string, error) { func GetNodePath() (string, error) {
if path, err := exec.LookPath("node"); err == nil { var err error
return path, nil nodePathOnce.Do(func() {
if path, e := exec.LookPath("node"); e == nil {
cachedNodePath = path
return
}
defaultPath := `C:\Program Files\nodejs\node.exe`
if _, e := os.Stat(defaultPath); e == nil {
cachedNodePath = defaultPath
return
}
err = fmt.Errorf("未找到 Node.js")
})
if err != nil {
return "", err
} }
defaultPath := `C:\Program Files\nodejs\node.exe` if cachedNodePath != "" {
if _, err := os.Stat(defaultPath); err == nil { return cachedNodePath, nil
return defaultPath, nil
} }
return "", fmt.Errorf("未找到 Node.js") return "", fmt.Errorf("未找到 Node.js")
} }
@@ -162,14 +175,9 @@ func SetupNodeEnv() error {
newPath := nodeDir + string(os.PathListSeparator) + pathEnv newPath := nodeDir + string(os.PathListSeparator) + pathEnv
// 同时确保 npm prefix 在 PATH 中 // 同时确保 npm prefix 在 PATH 中
npmPath, err := getNpmPath() if npmPrefix, err := getNpmPrefix(); err == nil {
if err == nil { if !strings.Contains(strings.ToLower(newPath), strings.ToLower(npmPrefix)) {
cmd := exec.Command(npmPath, "config", "get", "prefix") newPath = npmPrefix + string(os.PathListSeparator) + newPath
if out, err := cmd.Output(); err == nil {
npmPrefix := strings.TrimSpace(string(out))
if !strings.Contains(strings.ToLower(newPath), strings.ToLower(npmPrefix)) {
newPath = npmPrefix + string(os.PathListSeparator) + newPath
}
} }
} }
@@ -242,6 +250,28 @@ func getNpmPath() (string, error) {
return "", fmt.Errorf("未找到 npm请确认 Node.js 安装成功") return "", fmt.Errorf("未找到 npm请确认 Node.js 安装成功")
} }
func getNpmPrefix() (string, error) {
var err error
prefixOnce.Do(func() {
npmPath, e := getNpmPath()
if e != nil {
err = fmt.Errorf("无法定位 npm: %v", e)
return
}
cmd := exec.Command(npmPath, "config", "get", "prefix")
out, e := cmd.Output()
if e != nil {
err = fmt.Errorf("无法获取 npm prefix: %v", e)
return
}
cachedNpmPrefix = strings.TrimSpace(string(out))
})
if err != nil {
return "", err
}
return cachedNpmPrefix, nil
}
// ConfigureNpmMirror 设置 npm 淘宝镜像 // ConfigureNpmMirror 设置 npm 淘宝镜像
func ConfigureNpmMirror() error { func ConfigureNpmMirror() error {
npmPath, err := getNpmPath() npmPath, err := getNpmPath()
@@ -341,18 +371,10 @@ func EnsureOnPath() (bool, error) {
return false, nil // 已存在 return false, nil // 已存在
} }
npmPath, err := getNpmPath() npmPrefix, err := getNpmPrefix()
if err != nil { if err != nil {
return false, err return false, err
} }
// 获取 npm prefix
cmd := exec.Command(npmPath, "config", "get", "prefix")
out, err := cmd.Output()
if err != nil {
return false, err
}
npmPrefix := strings.TrimSpace(string(out))
npmBin := filepath.Join(npmPrefix, "bin") npmBin := filepath.Join(npmPrefix, "bin")
// 查找 clawdbot.cmd 或 moltbot.cmd // 查找 clawdbot.cmd 或 moltbot.cmd

View File

@@ -3,6 +3,7 @@ package ui
import ( import (
"fmt" "fmt"
"math/rand" "math/rand"
"sync"
"time" "time"
"moltbot-installer/internal/style" "moltbot-installer/internal/style"
@@ -464,8 +465,28 @@ func (m Model) View() string {
// Commands // Commands
func checkEnvCmd() tea.Msg { func checkEnvCmd() tea.Msg {
nodeVer, nodeOk := sys.CheckNode() var (
moltbotVer, moltbotInstalled := sys.CheckMoltbot() nodeVer string
nodeOk bool
moltbotVer string
moltbotInstalled bool
wg sync.WaitGroup
)
wg.Add(2)
go func() {
defer wg.Done()
nodeVer, nodeOk = sys.CheckNode()
}()
go func() {
defer wg.Done()
moltbotVer, moltbotInstalled = sys.CheckMoltbot()
}()
wg.Wait()
return checkMsg{ return checkMsg{
nodeVer: nodeVer, nodeVer: nodeVer,
nodeOk: nodeOk, nodeOk: nodeOk,