@yivan-lab/pretty-please 1.0.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +381 -28
- package/bin/pls.tsx +1138 -109
- package/dist/bin/pls.d.ts +1 -1
- package/dist/bin/pls.js +994 -91
- package/dist/package.json +80 -0
- package/dist/src/ai.d.ts +1 -41
- package/dist/src/ai.js +9 -190
- package/dist/src/alias.d.ts +41 -0
- package/dist/src/alias.js +240 -0
- package/dist/src/builtin-detector.d.ts +14 -8
- package/dist/src/builtin-detector.js +36 -16
- package/dist/src/chat-history.d.ts +16 -11
- package/dist/src/chat-history.js +35 -4
- package/dist/src/components/Chat.js +5 -4
- package/dist/src/components/CodeColorizer.js +26 -20
- package/dist/src/components/CommandBox.js +3 -17
- package/dist/src/components/ConfirmationPrompt.d.ts +2 -1
- package/dist/src/components/ConfirmationPrompt.js +9 -4
- package/dist/src/components/Duration.js +2 -1
- package/dist/src/components/InlineRenderer.js +2 -1
- package/dist/src/components/MarkdownDisplay.js +2 -1
- package/dist/src/components/MultiStepCommandGenerator.d.ts +5 -1
- package/dist/src/components/MultiStepCommandGenerator.js +127 -14
- package/dist/src/components/TableRenderer.js +2 -1
- package/dist/src/config.d.ts +59 -9
- package/dist/src/config.js +147 -48
- package/dist/src/history.d.ts +19 -5
- package/dist/src/history.js +26 -11
- package/dist/src/mastra-agent.d.ts +0 -1
- package/dist/src/mastra-agent.js +3 -4
- package/dist/src/mastra-chat.d.ts +28 -0
- package/dist/src/mastra-chat.js +93 -0
- package/dist/src/multi-step.d.ts +23 -7
- package/dist/src/multi-step.js +29 -6
- package/dist/src/prompts.d.ts +11 -0
- package/dist/src/prompts.js +140 -0
- package/dist/src/remote-history.d.ts +63 -0
- package/dist/src/remote-history.js +315 -0
- package/dist/src/remote.d.ts +113 -0
- package/dist/src/remote.js +634 -0
- package/dist/src/shell-hook.d.ts +87 -12
- package/dist/src/shell-hook.js +315 -17
- package/dist/src/sysinfo.d.ts +9 -5
- package/dist/src/sysinfo.js +2 -2
- package/dist/src/ui/theme.d.ts +27 -24
- package/dist/src/ui/theme.js +71 -21
- package/dist/src/upgrade.d.ts +41 -0
- package/dist/src/upgrade.js +348 -0
- package/dist/src/utils/console.d.ts +11 -11
- package/dist/src/utils/console.js +26 -17
- package/package.json +11 -9
- package/src/alias.ts +301 -0
- package/src/builtin-detector.ts +126 -0
- package/src/chat-history.ts +140 -0
- package/src/components/Chat.tsx +6 -5
- package/src/components/CodeColorizer.tsx +27 -19
- package/src/components/CommandBox.tsx +3 -17
- package/src/components/ConfirmationPrompt.tsx +11 -3
- package/src/components/Duration.tsx +2 -1
- package/src/components/InlineRenderer.tsx +2 -1
- package/src/components/MarkdownDisplay.tsx +2 -1
- package/src/components/MultiStepCommandGenerator.tsx +167 -16
- package/src/components/TableRenderer.tsx +2 -1
- package/src/config.ts +394 -0
- package/src/history.ts +160 -0
- package/src/mastra-agent.ts +3 -4
- package/src/mastra-chat.ts +124 -0
- package/src/multi-step.ts +45 -8
- package/src/prompts.ts +154 -0
- package/src/remote-history.ts +390 -0
- package/src/remote.ts +800 -0
- package/src/shell-hook.ts +754 -0
- package/src/{sysinfo.js → sysinfo.ts} +28 -16
- package/src/ui/theme.ts +101 -24
- package/src/upgrade.ts +397 -0
- package/src/utils/{console.js → console.ts} +36 -27
- package/bin/pls.js +0 -681
- package/src/ai.js +0 -324
- package/src/builtin-detector.js +0 -98
- package/src/chat-history.js +0 -94
- package/src/components/ChatStatus.tsx +0 -53
- package/src/components/CommandGenerator.tsx +0 -184
- package/src/components/ConfigDisplay.tsx +0 -64
- package/src/components/ConfigWizard.tsx +0 -101
- package/src/components/HistoryDisplay.tsx +0 -69
- package/src/components/HookManager.tsx +0 -150
- package/src/config.js +0 -221
- package/src/history.js +0 -131
- package/src/shell-hook.js +0 -393
|
@@ -1,10 +1,22 @@
|
|
|
1
|
-
import os from 'os'
|
|
2
|
-
import { execSync } from 'child_process'
|
|
1
|
+
import os from 'os'
|
|
2
|
+
import { execSync } from 'child_process'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 系统信息
|
|
6
|
+
*/
|
|
7
|
+
export interface SystemInfo {
|
|
8
|
+
os: NodeJS.Platform
|
|
9
|
+
arch: string
|
|
10
|
+
shell: string
|
|
11
|
+
packageManager: string
|
|
12
|
+
cwd: string
|
|
13
|
+
user: string
|
|
14
|
+
}
|
|
3
15
|
|
|
4
16
|
/**
|
|
5
17
|
* 检测包管理器
|
|
6
18
|
*/
|
|
7
|
-
function detectPackageManager() {
|
|
19
|
+
function detectPackageManager(): string {
|
|
8
20
|
const managers = [
|
|
9
21
|
{ name: 'brew', command: 'brew' },
|
|
10
22
|
{ name: 'apt', command: 'apt-get' },
|
|
@@ -12,46 +24,46 @@ function detectPackageManager() {
|
|
|
12
24
|
{ name: 'yum', command: 'yum' },
|
|
13
25
|
{ name: 'pacman', command: 'pacman' },
|
|
14
26
|
{ name: 'zypper', command: 'zypper' },
|
|
15
|
-
{ name: 'apk', command: 'apk' }
|
|
16
|
-
]
|
|
27
|
+
{ name: 'apk', command: 'apk' },
|
|
28
|
+
]
|
|
17
29
|
|
|
18
30
|
for (const mgr of managers) {
|
|
19
31
|
try {
|
|
20
|
-
execSync(`which ${mgr.command}`, { stdio: 'ignore' })
|
|
21
|
-
return mgr.name
|
|
32
|
+
execSync(`which ${mgr.command}`, { stdio: 'ignore' })
|
|
33
|
+
return mgr.name
|
|
22
34
|
} catch {
|
|
23
35
|
// 继续检测下一个
|
|
24
36
|
}
|
|
25
37
|
}
|
|
26
38
|
|
|
27
|
-
return 'unknown'
|
|
39
|
+
return 'unknown'
|
|
28
40
|
}
|
|
29
41
|
|
|
30
42
|
/**
|
|
31
43
|
* 获取当前 Shell
|
|
32
44
|
*/
|
|
33
|
-
function getCurrentShell() {
|
|
34
|
-
return process.env.SHELL || 'unknown'
|
|
45
|
+
function getCurrentShell(): string {
|
|
46
|
+
return process.env.SHELL || 'unknown'
|
|
35
47
|
}
|
|
36
48
|
|
|
37
49
|
/**
|
|
38
50
|
* 收集系统信息
|
|
39
51
|
*/
|
|
40
|
-
export function collectSystemInfo() {
|
|
52
|
+
export function collectSystemInfo(): SystemInfo {
|
|
41
53
|
return {
|
|
42
54
|
os: os.platform(),
|
|
43
55
|
arch: os.arch(),
|
|
44
56
|
shell: getCurrentShell(),
|
|
45
57
|
packageManager: detectPackageManager(),
|
|
46
58
|
cwd: process.cwd(),
|
|
47
|
-
user: os.userInfo().username
|
|
48
|
-
}
|
|
59
|
+
user: os.userInfo().username,
|
|
60
|
+
}
|
|
49
61
|
}
|
|
50
62
|
|
|
51
63
|
/**
|
|
52
64
|
* 将系统信息格式化为字符串(供 AI 使用)
|
|
53
65
|
*/
|
|
54
|
-
export function formatSystemInfo() {
|
|
55
|
-
const info = collectSystemInfo()
|
|
56
|
-
return `OS: ${info.os}, Arch: ${info.arch}, Shell: ${info.shell}, PkgMgr: ${info.packageManager}, CWD: ${info.cwd}
|
|
66
|
+
export function formatSystemInfo(): string {
|
|
67
|
+
const info = collectSystemInfo()
|
|
68
|
+
return `OS: ${info.os}, Arch: ${info.arch}, Shell: ${info.shell}, PkgMgr: ${info.packageManager}, CWD: ${info.cwd}`
|
|
57
69
|
}
|
package/src/ui/theme.ts
CHANGED
|
@@ -1,29 +1,53 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
primary: '#00D9FF', // 青色 - 主要交互元素
|
|
5
|
-
secondary: '#A78BFA', // 紫色 - 次要元素
|
|
6
|
-
accent: '#F472B6', // 粉色 - 强调元素
|
|
1
|
+
import fs from 'fs'
|
|
2
|
+
import path from 'path'
|
|
3
|
+
import os from 'os'
|
|
7
4
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
error: '#EF4444', // 红色 - 错误
|
|
11
|
-
warning: '#F59E0B', // 橙色 - 警告
|
|
12
|
-
info: '#3B82F6', // 蓝色 - 信息
|
|
5
|
+
// 主题类型定义
|
|
6
|
+
export type ThemeName = 'dark' | 'light'
|
|
13
7
|
|
|
14
|
-
|
|
8
|
+
export interface Theme {
|
|
9
|
+
primary: string
|
|
10
|
+
secondary: string
|
|
11
|
+
accent: string
|
|
12
|
+
success: string
|
|
13
|
+
error: string
|
|
14
|
+
warning: string
|
|
15
|
+
info: string
|
|
15
16
|
text: {
|
|
16
|
-
primary:
|
|
17
|
-
secondary:
|
|
18
|
-
muted:
|
|
19
|
-
dim:
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
17
|
+
primary: string
|
|
18
|
+
secondary: string
|
|
19
|
+
muted: string
|
|
20
|
+
dim: string
|
|
21
|
+
}
|
|
22
|
+
border: string
|
|
23
|
+
divider: string
|
|
24
|
+
code: {
|
|
25
|
+
background: string
|
|
26
|
+
text: string
|
|
27
|
+
keyword: string
|
|
28
|
+
string: string
|
|
29
|
+
function: string
|
|
30
|
+
comment: string
|
|
31
|
+
}
|
|
32
|
+
}
|
|
25
33
|
|
|
26
|
-
|
|
34
|
+
// 深色主题(原默认主题)
|
|
35
|
+
const darkTheme: Theme = {
|
|
36
|
+
primary: '#00D9FF',
|
|
37
|
+
secondary: '#A78BFA',
|
|
38
|
+
accent: '#F472B6',
|
|
39
|
+
success: '#10B981',
|
|
40
|
+
error: '#EF4444',
|
|
41
|
+
warning: '#F59E0B',
|
|
42
|
+
info: '#3B82F6',
|
|
43
|
+
text: {
|
|
44
|
+
primary: '#E5E7EB',
|
|
45
|
+
secondary: '#9CA3AF',
|
|
46
|
+
muted: '#6B7280',
|
|
47
|
+
dim: '#4B5563',
|
|
48
|
+
},
|
|
49
|
+
border: '#374151',
|
|
50
|
+
divider: '#1F2937',
|
|
27
51
|
code: {
|
|
28
52
|
background: '#1F2937',
|
|
29
53
|
text: '#E5E7EB',
|
|
@@ -31,7 +55,60 @@ export const theme = {
|
|
|
31
55
|
string: '#98C379',
|
|
32
56
|
function: '#61AFEF',
|
|
33
57
|
comment: '#5C6370',
|
|
58
|
+
},
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// 浅色主题(白色/浅色终端背景)
|
|
62
|
+
// 所有颜色都要在白色背景上清晰可见
|
|
63
|
+
const lightTheme: Theme = {
|
|
64
|
+
primary: '#0369A1', // 深天蓝,在白底上醒目
|
|
65
|
+
secondary: '#6D28D9', // 深紫色
|
|
66
|
+
accent: '#BE185D', // 深粉色
|
|
67
|
+
success: '#047857', // 深绿色
|
|
68
|
+
error: '#B91C1C', // 深红色
|
|
69
|
+
warning: '#B45309', // 深橙色
|
|
70
|
+
info: '#1D4ED8', // 深蓝色
|
|
71
|
+
text: {
|
|
72
|
+
primary: '#111827', // 近黑色,主要文字
|
|
73
|
+
secondary: '#374151', // 深灰色
|
|
74
|
+
muted: '#4B5563', // 中灰色
|
|
75
|
+
dim: '#6B7280', // 浅灰色
|
|
76
|
+
},
|
|
77
|
+
border: '#6B7280', // 边框要明显
|
|
78
|
+
divider: '#9CA3AF',
|
|
79
|
+
code: {
|
|
80
|
+
background: '#F3F4F6',
|
|
81
|
+
text: '#111827',
|
|
82
|
+
keyword: '#6D28D9',
|
|
83
|
+
string: '#047857',
|
|
84
|
+
function: '#0369A1',
|
|
85
|
+
comment: '#4B5563',
|
|
86
|
+
},
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// 所有主题
|
|
90
|
+
export const themes: Record<ThemeName, Theme> = {
|
|
91
|
+
dark: darkTheme,
|
|
92
|
+
light: lightTheme,
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// 获取当前主题
|
|
96
|
+
export function getCurrentTheme(): Theme {
|
|
97
|
+
// 直接读取配置文件,避免循环依赖
|
|
98
|
+
try {
|
|
99
|
+
const configPath = path.join(os.homedir(), '.please', 'config.json')
|
|
100
|
+
if (fs.existsSync(configPath)) {
|
|
101
|
+
const content = fs.readFileSync(configPath, 'utf-8')
|
|
102
|
+
const config = JSON.parse(content)
|
|
103
|
+
if (config.theme && themes[config.theme as ThemeName]) {
|
|
104
|
+
return themes[config.theme as ThemeName]
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
} catch {
|
|
108
|
+
// 忽略错误,返回默认主题
|
|
34
109
|
}
|
|
35
|
-
|
|
110
|
+
return themes.dark
|
|
111
|
+
}
|
|
36
112
|
|
|
37
|
-
|
|
113
|
+
// 向后兼容:导出默认主题
|
|
114
|
+
export const theme = darkTheme
|
package/src/upgrade.ts
ADDED
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 版本升级模块
|
|
3
|
+
*/
|
|
4
|
+
import fs from 'fs'
|
|
5
|
+
import os from 'os'
|
|
6
|
+
import path from 'path'
|
|
7
|
+
import https from 'https'
|
|
8
|
+
import http from 'http'
|
|
9
|
+
import { execSync, spawn } from 'child_process'
|
|
10
|
+
import chalk from 'chalk'
|
|
11
|
+
import * as console2 from './utils/console.js'
|
|
12
|
+
import { getCurrentTheme } from './ui/theme.js'
|
|
13
|
+
|
|
14
|
+
// 获取主题颜色
|
|
15
|
+
function getColors() {
|
|
16
|
+
const theme = getCurrentTheme()
|
|
17
|
+
return {
|
|
18
|
+
primary: theme.primary,
|
|
19
|
+
success: theme.success,
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const REPO = 'IvanLark/pretty-please'
|
|
24
|
+
const UPDATE_CHECK_FILE = path.join(os.homedir(), '.please', 'update-check.json')
|
|
25
|
+
const CHECK_INTERVAL = 24 * 60 * 60 * 1000 // 24 小时
|
|
26
|
+
|
|
27
|
+
interface UpdateCheckCache {
|
|
28
|
+
lastCheck: number
|
|
29
|
+
latestVersion: string | null
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* 获取最新版本(通过重定向,避免 API 限制)
|
|
34
|
+
* 优先使用 curl(支持代理),fallback 到 https 模块
|
|
35
|
+
*/
|
|
36
|
+
export async function getLatestVersion(): Promise<string | null> {
|
|
37
|
+
// 先尝试用 curl(支持环境变量代理)
|
|
38
|
+
try {
|
|
39
|
+
const result = execSync(
|
|
40
|
+
`curl -fsSI "https://github.com/${REPO}/releases/latest" 2>/dev/null | grep -i "^location:" | head -1`,
|
|
41
|
+
{ timeout: 10000, encoding: 'utf-8' }
|
|
42
|
+
)
|
|
43
|
+
const match = result.match(/\/tag\/([^\s\r\n]+)/)
|
|
44
|
+
if (match) {
|
|
45
|
+
return match[1].trim()
|
|
46
|
+
}
|
|
47
|
+
} catch {
|
|
48
|
+
// curl 失败,尝试 https 模块
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// fallback: 使用 https 模块
|
|
52
|
+
return new Promise((resolve) => {
|
|
53
|
+
const req = https.request(
|
|
54
|
+
`https://github.com/${REPO}/releases/latest`,
|
|
55
|
+
{ method: 'HEAD' },
|
|
56
|
+
(res) => {
|
|
57
|
+
const location = res.headers.location
|
|
58
|
+
if (location) {
|
|
59
|
+
const match = location.match(/\/tag\/([^/]+)$/)
|
|
60
|
+
if (match) {
|
|
61
|
+
resolve(match[1])
|
|
62
|
+
return
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
resolve(null)
|
|
66
|
+
}
|
|
67
|
+
)
|
|
68
|
+
req.on('error', () => resolve(null))
|
|
69
|
+
req.setTimeout(5000, () => {
|
|
70
|
+
req.destroy()
|
|
71
|
+
resolve(null)
|
|
72
|
+
})
|
|
73
|
+
req.end()
|
|
74
|
+
})
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* 比较版本号
|
|
79
|
+
* @returns 1 if v1 > v2, -1 if v1 < v2, 0 if equal
|
|
80
|
+
*/
|
|
81
|
+
export function compareVersions(v1: string, v2: string): number {
|
|
82
|
+
const normalize = (v: string) => v.replace(/^v/, '').split('.').map(Number)
|
|
83
|
+
const parts1 = normalize(v1)
|
|
84
|
+
const parts2 = normalize(v2)
|
|
85
|
+
|
|
86
|
+
for (let i = 0; i < Math.max(parts1.length, parts2.length); i++) {
|
|
87
|
+
const p1 = parts1[i] || 0
|
|
88
|
+
const p2 = parts2[i] || 0
|
|
89
|
+
if (p1 > p2) return 1
|
|
90
|
+
if (p1 < p2) return -1
|
|
91
|
+
}
|
|
92
|
+
return 0
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* 检测当前平台
|
|
97
|
+
*/
|
|
98
|
+
export function detectPlatform(): { os: string; arch: string; artifact: string } | null {
|
|
99
|
+
const platform = os.platform()
|
|
100
|
+
const arch = os.arch()
|
|
101
|
+
|
|
102
|
+
if (platform === 'darwin') {
|
|
103
|
+
if (arch === 'arm64') {
|
|
104
|
+
return { os: 'darwin', arch: 'arm64', artifact: 'pls-darwin-arm64' }
|
|
105
|
+
} else if (arch === 'x64') {
|
|
106
|
+
return { os: 'darwin', arch: 'x64', artifact: 'pls-darwin-x64' }
|
|
107
|
+
}
|
|
108
|
+
} else if (platform === 'linux') {
|
|
109
|
+
if (arch === 'arm64') {
|
|
110
|
+
return { os: 'linux', arch: 'arm64', artifact: 'pls-linux-arm64' }
|
|
111
|
+
} else if (arch === 'x64') {
|
|
112
|
+
return { os: 'linux', arch: 'x64', artifact: 'pls-linux-x64' }
|
|
113
|
+
}
|
|
114
|
+
} else if (platform === 'win32') {
|
|
115
|
+
if (arch === 'x64') {
|
|
116
|
+
return { os: 'windows', arch: 'x64', artifact: 'pls-windows-x64.exe' }
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return null
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* 获取当前可执行文件路径
|
|
125
|
+
*/
|
|
126
|
+
export function getCurrentExecutablePath(): string {
|
|
127
|
+
return process.execPath
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* 读取更新检查缓存
|
|
132
|
+
*/
|
|
133
|
+
function readUpdateCache(): UpdateCheckCache | null {
|
|
134
|
+
try {
|
|
135
|
+
if (fs.existsSync(UPDATE_CHECK_FILE)) {
|
|
136
|
+
const data = fs.readFileSync(UPDATE_CHECK_FILE, 'utf-8')
|
|
137
|
+
return JSON.parse(data)
|
|
138
|
+
}
|
|
139
|
+
} catch {
|
|
140
|
+
// 忽略错误
|
|
141
|
+
}
|
|
142
|
+
return null
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* 写入更新检查缓存
|
|
147
|
+
*/
|
|
148
|
+
function writeUpdateCache(cache: UpdateCheckCache): void {
|
|
149
|
+
try {
|
|
150
|
+
const dir = path.dirname(UPDATE_CHECK_FILE)
|
|
151
|
+
if (!fs.existsSync(dir)) {
|
|
152
|
+
fs.mkdirSync(dir, { recursive: true })
|
|
153
|
+
}
|
|
154
|
+
fs.writeFileSync(UPDATE_CHECK_FILE, JSON.stringify(cache, null, 2))
|
|
155
|
+
} catch {
|
|
156
|
+
// 忽略错误
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* 检查是否有新版本(带缓存)
|
|
162
|
+
*/
|
|
163
|
+
export async function checkForUpdates(
|
|
164
|
+
currentVersion: string,
|
|
165
|
+
force = false
|
|
166
|
+
): Promise<{ hasUpdate: boolean; latestVersion: string | null }> {
|
|
167
|
+
const cache = readUpdateCache()
|
|
168
|
+
const now = Date.now()
|
|
169
|
+
|
|
170
|
+
// 如果不是强制检查,且缓存有效,使用缓存
|
|
171
|
+
if (!force && cache && now - cache.lastCheck < CHECK_INTERVAL) {
|
|
172
|
+
if (cache.latestVersion) {
|
|
173
|
+
const hasUpdate = compareVersions(cache.latestVersion, currentVersion) > 0
|
|
174
|
+
return { hasUpdate, latestVersion: cache.latestVersion }
|
|
175
|
+
}
|
|
176
|
+
return { hasUpdate: false, latestVersion: null }
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// 获取最新版本
|
|
180
|
+
const latestVersion = await getLatestVersion()
|
|
181
|
+
|
|
182
|
+
// 更新缓存
|
|
183
|
+
writeUpdateCache({ lastCheck: now, latestVersion })
|
|
184
|
+
|
|
185
|
+
if (latestVersion) {
|
|
186
|
+
const hasUpdate = compareVersions(latestVersion, currentVersion) > 0
|
|
187
|
+
return { hasUpdate, latestVersion }
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return { hasUpdate: false, latestVersion: null }
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* 显示更新提示
|
|
195
|
+
*/
|
|
196
|
+
export function showUpdateNotice(currentVersion: string, latestVersion: string): void {
|
|
197
|
+
const colors = getColors()
|
|
198
|
+
// 使用简洁的单行提示,避免复杂的对齐问题
|
|
199
|
+
console.log('')
|
|
200
|
+
console2.warning(`发现新版本: ${currentVersion} → ${chalk.hex(colors.success)(latestVersion)},运行 ${chalk.hex(colors.primary)('pls upgrade')} 更新`)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* 下载文件(使用 curl,支持代理)
|
|
205
|
+
*/
|
|
206
|
+
function downloadFile(url: string, dest: string, onProgress?: (percent: number) => void): Promise<void> {
|
|
207
|
+
return new Promise((resolve, reject) => {
|
|
208
|
+
// 使用 curl 下载,支持代理和进度显示
|
|
209
|
+
const args = ['-fSL', '--progress-bar', '-o', dest, url]
|
|
210
|
+
const curl = spawn('curl', args, { stdio: ['ignore', 'pipe', 'pipe'] })
|
|
211
|
+
|
|
212
|
+
let lastPercent = 0
|
|
213
|
+
|
|
214
|
+
// curl 进度输出在 stderr
|
|
215
|
+
curl.stderr?.on('data', (data: Buffer) => {
|
|
216
|
+
const str = data.toString()
|
|
217
|
+
// 解析 curl 进度条输出,格式如: "### 6.2%"
|
|
218
|
+
const match = str.match(/(\d+\.?\d*)%/)
|
|
219
|
+
if (match && onProgress) {
|
|
220
|
+
const percent = Math.round(parseFloat(match[1]))
|
|
221
|
+
if (percent > lastPercent) {
|
|
222
|
+
lastPercent = percent
|
|
223
|
+
onProgress(percent)
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
curl.on('close', (code) => {
|
|
229
|
+
if (code === 0) {
|
|
230
|
+
resolve()
|
|
231
|
+
} else {
|
|
232
|
+
reject(new Error(`curl 退出码: ${code}`))
|
|
233
|
+
}
|
|
234
|
+
})
|
|
235
|
+
|
|
236
|
+
curl.on('error', (err) => {
|
|
237
|
+
reject(err)
|
|
238
|
+
})
|
|
239
|
+
})
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* 检测是否是 Bun 编译的二进制
|
|
244
|
+
*/
|
|
245
|
+
export function isBunBinary(): boolean {
|
|
246
|
+
const execPath = process.execPath.toLowerCase()
|
|
247
|
+
// npm/node 运行时,execPath 会包含 node
|
|
248
|
+
// tsx 开发时,execPath 会包含 node 或 tsx
|
|
249
|
+
// Bun 编译的二进制,execPath 就是程序自己的路径
|
|
250
|
+
return !execPath.includes('node') && !execPath.includes('bun')
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* 执行升级
|
|
255
|
+
*/
|
|
256
|
+
export async function performUpgrade(currentVersion: string): Promise<boolean> {
|
|
257
|
+
console.log('')
|
|
258
|
+
console2.title('🚀 Pretty-Please 升级')
|
|
259
|
+
console2.muted('━'.repeat(40))
|
|
260
|
+
|
|
261
|
+
// 检测平台
|
|
262
|
+
console2.info('检测系统平台...')
|
|
263
|
+
const platform = detectPlatform()
|
|
264
|
+
if (!platform) {
|
|
265
|
+
console2.error('不支持的平台')
|
|
266
|
+
return false
|
|
267
|
+
}
|
|
268
|
+
console2.success(`平台: ${platform.os} ${platform.arch}`)
|
|
269
|
+
|
|
270
|
+
// 获取最新版本
|
|
271
|
+
console2.info('获取最新版本...')
|
|
272
|
+
const latestVersion = await getLatestVersion()
|
|
273
|
+
if (!latestVersion) {
|
|
274
|
+
console2.error('无法获取最新版本')
|
|
275
|
+
return false
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// 比较版本
|
|
279
|
+
if (compareVersions(latestVersion, currentVersion) <= 0) {
|
|
280
|
+
console2.success(`当前已是最新版本 (${currentVersion})`)
|
|
281
|
+
console.log('')
|
|
282
|
+
return true
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
console2.success(`发现新版本: ${currentVersion} → ${latestVersion}`)
|
|
286
|
+
|
|
287
|
+
// 检查安装方式
|
|
288
|
+
if (!isBunBinary()) {
|
|
289
|
+
// 如果是通过 npm/node 运行的,提示使用 npm 更新
|
|
290
|
+
console.log('')
|
|
291
|
+
console2.warning('检测到你是通过 npm 安装的,请使用以下命令更新:')
|
|
292
|
+
console.log('')
|
|
293
|
+
console.log(chalk.hex(getColors().primary)(' npm update -g @yivan-lab/pretty-please'))
|
|
294
|
+
console.log('')
|
|
295
|
+
return false
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// 获取当前可执行文件路径
|
|
299
|
+
const execPath = getCurrentExecutablePath()
|
|
300
|
+
console2.info(`当前程序: ${execPath}`)
|
|
301
|
+
|
|
302
|
+
// 下载新版本
|
|
303
|
+
const downloadUrl = `https://github.com/${REPO}/releases/download/${latestVersion}/${platform.artifact}`
|
|
304
|
+
const tempFile = path.join(os.tmpdir(), `pls-upgrade-${Date.now()}`)
|
|
305
|
+
|
|
306
|
+
console2.info('下载中...')
|
|
307
|
+
|
|
308
|
+
try {
|
|
309
|
+
let lastPercent = 0
|
|
310
|
+
await downloadFile(downloadUrl, tempFile, (percent) => {
|
|
311
|
+
if (percent - lastPercent >= 10 || percent === 100) {
|
|
312
|
+
process.stdout.write(`\r${chalk.hex(getCurrentTheme().primary)('[INFO]')} 下载中... ${percent}%`)
|
|
313
|
+
lastPercent = percent
|
|
314
|
+
}
|
|
315
|
+
})
|
|
316
|
+
console.log('') // 换行
|
|
317
|
+
console2.success('下载完成')
|
|
318
|
+
} catch (err: any) {
|
|
319
|
+
console2.error(`下载失败: ${err.message}`)
|
|
320
|
+
return false
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// 替换当前程序
|
|
324
|
+
console2.info('安装新版本...')
|
|
325
|
+
|
|
326
|
+
try {
|
|
327
|
+
// 设置可执行权限
|
|
328
|
+
fs.chmodSync(tempFile, 0o755)
|
|
329
|
+
|
|
330
|
+
// 备份旧版本
|
|
331
|
+
const backupPath = `${execPath}.backup`
|
|
332
|
+
if (fs.existsSync(backupPath)) {
|
|
333
|
+
fs.unlinkSync(backupPath)
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// Windows 需要特殊处理
|
|
337
|
+
if (platform.os === 'windows') {
|
|
338
|
+
// Windows 上无法替换正在运行的程序,创建一个批处理脚本
|
|
339
|
+
const batchScript = `@echo off
|
|
340
|
+
timeout /t 1 /nobreak >nul
|
|
341
|
+
move /y "${execPath}" "${backupPath}" >nul
|
|
342
|
+
move /y "${tempFile}" "${execPath}" >nul
|
|
343
|
+
del "${backupPath}" >nul 2>&1
|
|
344
|
+
echo.
|
|
345
|
+
echo 升级完成! ${currentVersion} → ${latestVersion}
|
|
346
|
+
echo.
|
|
347
|
+
pause
|
|
348
|
+
`
|
|
349
|
+
const batchPath = path.join(os.tmpdir(), 'pls-upgrade.bat')
|
|
350
|
+
fs.writeFileSync(batchPath, batchScript)
|
|
351
|
+
|
|
352
|
+
console.log('')
|
|
353
|
+
console2.warning('Windows 上需要额外步骤完成升级:')
|
|
354
|
+
console.log('')
|
|
355
|
+
console.log(chalk.hex(getColors().primary)(` 请运行: ${batchPath}`))
|
|
356
|
+
console.log('')
|
|
357
|
+
return true
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// Unix 系统:直接替换
|
|
361
|
+
fs.renameSync(execPath, backupPath)
|
|
362
|
+
fs.renameSync(tempFile, execPath)
|
|
363
|
+
|
|
364
|
+
// 删除备份
|
|
365
|
+
try {
|
|
366
|
+
fs.unlinkSync(backupPath)
|
|
367
|
+
} catch {
|
|
368
|
+
// 忽略删除备份失败
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
console2.muted('━'.repeat(40))
|
|
372
|
+
console2.success(`升级成功: ${currentVersion} → ${latestVersion}`)
|
|
373
|
+
console.log('')
|
|
374
|
+
|
|
375
|
+
return true
|
|
376
|
+
} catch (err: any) {
|
|
377
|
+
console2.error(`安装失败: ${err.message}`)
|
|
378
|
+
|
|
379
|
+
// 尝试清理
|
|
380
|
+
try {
|
|
381
|
+
if (fs.existsSync(tempFile)) {
|
|
382
|
+
fs.unlinkSync(tempFile)
|
|
383
|
+
}
|
|
384
|
+
} catch {}
|
|
385
|
+
|
|
386
|
+
// 如果是权限问题,提示使用 sudo
|
|
387
|
+
if (err.code === 'EACCES' || err.code === 'EPERM') {
|
|
388
|
+
console.log('')
|
|
389
|
+
console2.warning('权限不足,请尝试使用 sudo:')
|
|
390
|
+
console.log('')
|
|
391
|
+
console.log(chalk.hex(getColors().primary)(' sudo pls upgrade'))
|
|
392
|
+
console.log('')
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
return false
|
|
396
|
+
}
|
|
397
|
+
}
|