Skip to content

Commit

Permalink
feat: 完成fix功能mac开发
Browse files Browse the repository at this point in the history
  • Loading branch information
winchesHe committed Oct 22, 2023
1 parent eaad6f6 commit 729550e
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 27 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

## 自动识别并修复指令

目前支持 `Win`
目前支持 `Win、Mac`

![](./image/gpt-cli-fix.gif)
Binary file added image/gpt-cli-fix.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
"consola": "^3.2.3",
"execa": "^8.0.1",
"inquirer": "^9.1.4",
"ora": "^7.0.1"
"ora": "^7.0.1",
"shell-history": "^2.0.0"
},
"devDependencies": {
"@antfu/eslint-config": "^0.40.0",
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 40 additions & 22 deletions src/command/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
/* eslint-disable no-console */
import { platform } from 'node:os'
import { readFileSync } from 'node:fs'
import { printColorLogs, printErrorLogs, scanDirFile } from '@winches/utils'
import { consola } from 'consola'
import { oraPromise } from 'ora'

// @ts-expect-error 包不存在类型声明
import { shellHistory } from 'shell-history'

import { getIsExecuteSelect, getQuestion, getReloadSelect } from '../inquirer'
import { fetchQuestion } from '../utils'
import { normalizeDesc, normalizeOutput } from '../utils/log'
import { convertCmd } from '../utils/transform'
import { exec, execFn } from '../utils/execa'
import { readEnv, writeEnv } from '../utils/env'
import type { AnswerArr } from '../types'
import { platform } from 'os'
import { excludeList } from '../const'
import { readFileSync } from 'fs'
import { oraPromise } from 'ora'

const defaultBanner = '欢迎使用 cli-gpt 智能终端应用'
const gradientBanner = printColorLogs(defaultBanner)
Expand All @@ -38,33 +42,47 @@ export async function start(question?: ({} & string) | 'fix') {

// 执行修复功能
if (question === 'fix') {
// 验证是否有PowerShell记录
if (isWin && !powerShellHistory) {
powerShellHistory = scanDirFile('C:/Users', ['.txt'], excludeList).filter(item => item.includes('ConsoleHost_history'))?.[0]
let errorInfo = ''

if (isWin) {
// 验证是否有PowerShell记录
if (!powerShellHistory) {
printErrorLogs('无法找到终端历史记录文件')
process.exit(1)
powerShellHistory = scanDirFile('C:/Users', ['.txt'], excludeList).filter(item => item.includes('ConsoleHost_history'))?.[0]

if (!powerShellHistory) {
printErrorLogs('无法找到终端历史记录文件')
process.exit(1)
}

// 写入env
writeEnv({
powerShellHistory,
})
}

// 写入env
writeEnv({
powerShellHistory
})
}
const historyList = readFileSync(powerShellHistory, 'utf8').split('\n').filter(i => i)
const lastHistory = historyList[historyList.length - 2]
const transformHistory = lastHistory.replace(/\r/g, '')

const historyList = readFileSync(powerShellHistory, 'utf8').split('\n').filter(i => i)
const lastHistory = historyList[historyList.length - 2]
const transformHistory = lastHistory.replace(/\r/g, '')
let errorInfo = ''
await runCmd(transformHistory)

await runCmd(transformHistory)
await fetchQuestion(question, [transformHistory, errorInfo])
process.exit(0)
}
else {
// mac
const historyList = shellHistory()
const lastHistory = historyList[historyList.length - 2]
const transformHistory = lastHistory.replace(/\r/g, '')

await fetchAnswer(question, [transformHistory, errorInfo])
process.exit(1)
await runCmd(transformHistory)

await generateAnswer(question, [transformHistory, errorInfo])
process.exit(0)
}

async function runCmd(cmd: string) {
return new Promise(resolve => {
return new Promise((resolve) => {
execFn(cmd, (err: any, stdout: string) => {
errorInfo = `Error: ${err?.message}\n${stdout || ''}`
resolve(true)
Expand Down Expand Up @@ -123,7 +141,7 @@ export async function start(question?: ({} & string) | 'fix') {
for (const cmd of cmdList) {
await oraPromise(exec(cmd), {
text: `正在运行 ${cmd}...`,
successText: `✨ ${cmd} 运行成功!`
successText: `✨ ${cmd} 运行成功!`,
})
}
}
Expand Down
15 changes: 12 additions & 3 deletions src/utils/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,29 @@ export async function fetchQuestion(question: string, errorInfo: string[] = [])

const isError = !!errorInfo.length
const [cmd, info] = errorInfo
const errorPrompt = `运行\`${cmd}\`指令的时候报错了,我要怎么修复它,下面三个反引号圈起来的是错误信息,你可以根据错误信息思考出对应的解决办法。
const errorPrompt = `运行\`${cmd}\`指令的时候报错了,我要怎么修复它,下面三个反引号圈起来的是错误信息,你可以根据错误信息思考出对应的解决办法,并且按照之前要求的JSON格式回复
错误信息:
\`\`\`
${info}
\`\`\``
\`\`\`
回答直接输出JSON类型,若有其他可能,也放到一个数组里输出:
[
{
execute: true,
desc: '示例的描述',
cmd: 'git branch'
}
]`

const res = await oraPromise(_cmdRunner.cmdRunner(isError ? errorPrompt : question), {
text: isError ? `正在分析错误(${cmd})...` : question,
})

try {
const text = res.text
const matchReg = /\[.*\]/ms
const matchReg = /\[.*?\]/ms
const matchJson = text.match(matchReg)?.[0]

if (!matchJson) {
Expand Down

0 comments on commit 729550e

Please sign in to comment.