|
|
@@ -421,13 +421,20 @@ export function registerHandlers(): void {
|
|
|
// 使用 pnpm 或 npm 全局安装 @anthropic-ai/claude-code
|
|
|
// Windows 上执行 .cmd 文件时,使用 shell: true 避免参数解析问题
|
|
|
const isWindows = process.platform === 'win32'
|
|
|
+
|
|
|
+ // 获取刷新后的 PATH,确保新安装的 Node.js 路径在 PATH 中
|
|
|
+ const { getRefreshedPath } = await import('./utils')
|
|
|
+ const refreshedPath = isWindows ? await getRefreshedPath() : process.env.PATH
|
|
|
+
|
|
|
const result = await execa(pkgManagerPath, installArgs, {
|
|
|
encoding: 'utf8',
|
|
|
// 捕获输出以便记录日志
|
|
|
stdout: 'pipe',
|
|
|
stderr: 'pipe',
|
|
|
// Windows 上使用 shell 模式执行 .cmd 文件,避免参数解析问题
|
|
|
- shell: isWindows
|
|
|
+ shell: isWindows,
|
|
|
+ // 使用刷新后的 PATH 环境变量
|
|
|
+ env: { ...process.env, PATH: refreshedPath }
|
|
|
})
|
|
|
|
|
|
// 记录安装输出
|
|
|
@@ -487,15 +494,30 @@ export function registerHandlers(): void {
|
|
|
const execaError = error as { message?: string; stderr?: string; stdout?: string; shortMessage?: string }
|
|
|
let errorMessage = execaError.shortMessage || execaError.message || '未知错误'
|
|
|
|
|
|
+ // 收集所有输出信息
|
|
|
+ const outputs: string[] = []
|
|
|
+
|
|
|
// 如果有 stderr,尝试从中提取有用信息
|
|
|
if (execaError.stderr) {
|
|
|
// 过滤掉乱码字符,只保留可读字符
|
|
|
const stderrClean = execaError.stderr.replace(/[^\x20-\x7E\u4e00-\u9fa5\n\r]/g, '').trim()
|
|
|
if (stderrClean) {
|
|
|
- errorMessage = `${errorMessage}\n${stderrClean}`
|
|
|
+ outputs.push(`stderr: ${stderrClean}`)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果有 stdout,也提取(有时错误信息会输出到 stdout)
|
|
|
+ if (execaError.stdout) {
|
|
|
+ const stdoutClean = execaError.stdout.replace(/[^\x20-\x7E\u4e00-\u9fa5\n\r]/g, '').trim()
|
|
|
+ if (stdoutClean) {
|
|
|
+ outputs.push(`stdout: ${stdoutClean}`)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ if (outputs.length > 0) {
|
|
|
+ errorMessage = `${errorMessage}\n${outputs.join('\n')}`
|
|
|
+ }
|
|
|
+
|
|
|
logger.installError('Claude Code 安装失败', error)
|
|
|
sendToRenderer('install-error', {
|
|
|
software: 'claudeCode',
|