@yivan-lab/pretty-please 1.1.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.
Files changed (52) hide show
  1. package/README.md +283 -1
  2. package/bin/pls.tsx +1022 -104
  3. package/dist/bin/pls.js +894 -84
  4. package/dist/package.json +4 -4
  5. package/dist/src/alias.d.ts +41 -0
  6. package/dist/src/alias.js +240 -0
  7. package/dist/src/chat-history.js +10 -1
  8. package/dist/src/components/Chat.js +2 -1
  9. package/dist/src/components/CodeColorizer.js +26 -20
  10. package/dist/src/components/CommandBox.js +2 -1
  11. package/dist/src/components/ConfirmationPrompt.js +2 -1
  12. package/dist/src/components/Duration.js +2 -1
  13. package/dist/src/components/InlineRenderer.js +2 -1
  14. package/dist/src/components/MarkdownDisplay.js +2 -1
  15. package/dist/src/components/MultiStepCommandGenerator.d.ts +3 -1
  16. package/dist/src/components/MultiStepCommandGenerator.js +20 -10
  17. package/dist/src/components/TableRenderer.js +2 -1
  18. package/dist/src/config.d.ts +34 -3
  19. package/dist/src/config.js +71 -31
  20. package/dist/src/multi-step.d.ts +22 -6
  21. package/dist/src/multi-step.js +27 -4
  22. package/dist/src/remote-history.d.ts +63 -0
  23. package/dist/src/remote-history.js +315 -0
  24. package/dist/src/remote.d.ts +113 -0
  25. package/dist/src/remote.js +634 -0
  26. package/dist/src/shell-hook.d.ts +53 -0
  27. package/dist/src/shell-hook.js +242 -19
  28. package/dist/src/ui/theme.d.ts +27 -24
  29. package/dist/src/ui/theme.js +71 -21
  30. package/dist/src/upgrade.d.ts +41 -0
  31. package/dist/src/upgrade.js +348 -0
  32. package/dist/src/utils/console.js +22 -11
  33. package/package.json +4 -4
  34. package/src/alias.ts +301 -0
  35. package/src/chat-history.ts +11 -1
  36. package/src/components/Chat.tsx +2 -1
  37. package/src/components/CodeColorizer.tsx +27 -19
  38. package/src/components/CommandBox.tsx +2 -1
  39. package/src/components/ConfirmationPrompt.tsx +2 -1
  40. package/src/components/Duration.tsx +2 -1
  41. package/src/components/InlineRenderer.tsx +2 -1
  42. package/src/components/MarkdownDisplay.tsx +2 -1
  43. package/src/components/MultiStepCommandGenerator.tsx +25 -11
  44. package/src/components/TableRenderer.tsx +2 -1
  45. package/src/config.ts +117 -32
  46. package/src/multi-step.ts +43 -6
  47. package/src/remote-history.ts +390 -0
  48. package/src/remote.ts +800 -0
  49. package/src/shell-hook.ts +271 -19
  50. package/src/ui/theme.ts +101 -24
  51. package/src/upgrade.ts +397 -0
  52. package/src/utils/console.ts +22 -11
@@ -0,0 +1,348 @@
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 { execSync, spawn } from 'child_process';
9
+ import chalk from 'chalk';
10
+ import * as console2 from './utils/console.js';
11
+ import { getCurrentTheme } from './ui/theme.js';
12
+ // 获取主题颜色
13
+ function getColors() {
14
+ const theme = getCurrentTheme();
15
+ return {
16
+ primary: theme.primary,
17
+ success: theme.success,
18
+ };
19
+ }
20
+ const REPO = 'IvanLark/pretty-please';
21
+ const UPDATE_CHECK_FILE = path.join(os.homedir(), '.please', 'update-check.json');
22
+ const CHECK_INTERVAL = 24 * 60 * 60 * 1000; // 24 小时
23
+ /**
24
+ * 获取最新版本(通过重定向,避免 API 限制)
25
+ * 优先使用 curl(支持代理),fallback 到 https 模块
26
+ */
27
+ export async function getLatestVersion() {
28
+ // 先尝试用 curl(支持环境变量代理)
29
+ try {
30
+ const result = execSync(`curl -fsSI "https://github.com/${REPO}/releases/latest" 2>/dev/null | grep -i "^location:" | head -1`, { timeout: 10000, encoding: 'utf-8' });
31
+ const match = result.match(/\/tag\/([^\s\r\n]+)/);
32
+ if (match) {
33
+ return match[1].trim();
34
+ }
35
+ }
36
+ catch {
37
+ // curl 失败,尝试 https 模块
38
+ }
39
+ // fallback: 使用 https 模块
40
+ return new Promise((resolve) => {
41
+ const req = https.request(`https://github.com/${REPO}/releases/latest`, { method: 'HEAD' }, (res) => {
42
+ const location = res.headers.location;
43
+ if (location) {
44
+ const match = location.match(/\/tag\/([^/]+)$/);
45
+ if (match) {
46
+ resolve(match[1]);
47
+ return;
48
+ }
49
+ }
50
+ resolve(null);
51
+ });
52
+ req.on('error', () => resolve(null));
53
+ req.setTimeout(5000, () => {
54
+ req.destroy();
55
+ resolve(null);
56
+ });
57
+ req.end();
58
+ });
59
+ }
60
+ /**
61
+ * 比较版本号
62
+ * @returns 1 if v1 > v2, -1 if v1 < v2, 0 if equal
63
+ */
64
+ export function compareVersions(v1, v2) {
65
+ const normalize = (v) => v.replace(/^v/, '').split('.').map(Number);
66
+ const parts1 = normalize(v1);
67
+ const parts2 = normalize(v2);
68
+ for (let i = 0; i < Math.max(parts1.length, parts2.length); i++) {
69
+ const p1 = parts1[i] || 0;
70
+ const p2 = parts2[i] || 0;
71
+ if (p1 > p2)
72
+ return 1;
73
+ if (p1 < p2)
74
+ return -1;
75
+ }
76
+ return 0;
77
+ }
78
+ /**
79
+ * 检测当前平台
80
+ */
81
+ export function detectPlatform() {
82
+ const platform = os.platform();
83
+ const arch = os.arch();
84
+ if (platform === 'darwin') {
85
+ if (arch === 'arm64') {
86
+ return { os: 'darwin', arch: 'arm64', artifact: 'pls-darwin-arm64' };
87
+ }
88
+ else if (arch === 'x64') {
89
+ return { os: 'darwin', arch: 'x64', artifact: 'pls-darwin-x64' };
90
+ }
91
+ }
92
+ else if (platform === 'linux') {
93
+ if (arch === 'arm64') {
94
+ return { os: 'linux', arch: 'arm64', artifact: 'pls-linux-arm64' };
95
+ }
96
+ else if (arch === 'x64') {
97
+ return { os: 'linux', arch: 'x64', artifact: 'pls-linux-x64' };
98
+ }
99
+ }
100
+ else if (platform === 'win32') {
101
+ if (arch === 'x64') {
102
+ return { os: 'windows', arch: 'x64', artifact: 'pls-windows-x64.exe' };
103
+ }
104
+ }
105
+ return null;
106
+ }
107
+ /**
108
+ * 获取当前可执行文件路径
109
+ */
110
+ export function getCurrentExecutablePath() {
111
+ return process.execPath;
112
+ }
113
+ /**
114
+ * 读取更新检查缓存
115
+ */
116
+ function readUpdateCache() {
117
+ try {
118
+ if (fs.existsSync(UPDATE_CHECK_FILE)) {
119
+ const data = fs.readFileSync(UPDATE_CHECK_FILE, 'utf-8');
120
+ return JSON.parse(data);
121
+ }
122
+ }
123
+ catch {
124
+ // 忽略错误
125
+ }
126
+ return null;
127
+ }
128
+ /**
129
+ * 写入更新检查缓存
130
+ */
131
+ function writeUpdateCache(cache) {
132
+ try {
133
+ const dir = path.dirname(UPDATE_CHECK_FILE);
134
+ if (!fs.existsSync(dir)) {
135
+ fs.mkdirSync(dir, { recursive: true });
136
+ }
137
+ fs.writeFileSync(UPDATE_CHECK_FILE, JSON.stringify(cache, null, 2));
138
+ }
139
+ catch {
140
+ // 忽略错误
141
+ }
142
+ }
143
+ /**
144
+ * 检查是否有新版本(带缓存)
145
+ */
146
+ export async function checkForUpdates(currentVersion, force = false) {
147
+ const cache = readUpdateCache();
148
+ const now = Date.now();
149
+ // 如果不是强制检查,且缓存有效,使用缓存
150
+ if (!force && cache && now - cache.lastCheck < CHECK_INTERVAL) {
151
+ if (cache.latestVersion) {
152
+ const hasUpdate = compareVersions(cache.latestVersion, currentVersion) > 0;
153
+ return { hasUpdate, latestVersion: cache.latestVersion };
154
+ }
155
+ return { hasUpdate: false, latestVersion: null };
156
+ }
157
+ // 获取最新版本
158
+ const latestVersion = await getLatestVersion();
159
+ // 更新缓存
160
+ writeUpdateCache({ lastCheck: now, latestVersion });
161
+ if (latestVersion) {
162
+ const hasUpdate = compareVersions(latestVersion, currentVersion) > 0;
163
+ return { hasUpdate, latestVersion };
164
+ }
165
+ return { hasUpdate: false, latestVersion: null };
166
+ }
167
+ /**
168
+ * 显示更新提示
169
+ */
170
+ export function showUpdateNotice(currentVersion, latestVersion) {
171
+ const colors = getColors();
172
+ // 使用简洁的单行提示,避免复杂的对齐问题
173
+ console.log('');
174
+ console2.warning(`发现新版本: ${currentVersion} → ${chalk.hex(colors.success)(latestVersion)},运行 ${chalk.hex(colors.primary)('pls upgrade')} 更新`);
175
+ }
176
+ /**
177
+ * 下载文件(使用 curl,支持代理)
178
+ */
179
+ function downloadFile(url, dest, onProgress) {
180
+ return new Promise((resolve, reject) => {
181
+ // 使用 curl 下载,支持代理和进度显示
182
+ const args = ['-fSL', '--progress-bar', '-o', dest, url];
183
+ const curl = spawn('curl', args, { stdio: ['ignore', 'pipe', 'pipe'] });
184
+ let lastPercent = 0;
185
+ // curl 进度输出在 stderr
186
+ curl.stderr?.on('data', (data) => {
187
+ const str = data.toString();
188
+ // 解析 curl 进度条输出,格式如: "### 6.2%"
189
+ const match = str.match(/(\d+\.?\d*)%/);
190
+ if (match && onProgress) {
191
+ const percent = Math.round(parseFloat(match[1]));
192
+ if (percent > lastPercent) {
193
+ lastPercent = percent;
194
+ onProgress(percent);
195
+ }
196
+ }
197
+ });
198
+ curl.on('close', (code) => {
199
+ if (code === 0) {
200
+ resolve();
201
+ }
202
+ else {
203
+ reject(new Error(`curl 退出码: ${code}`));
204
+ }
205
+ });
206
+ curl.on('error', (err) => {
207
+ reject(err);
208
+ });
209
+ });
210
+ }
211
+ /**
212
+ * 检测是否是 Bun 编译的二进制
213
+ */
214
+ export function isBunBinary() {
215
+ const execPath = process.execPath.toLowerCase();
216
+ // npm/node 运行时,execPath 会包含 node
217
+ // tsx 开发时,execPath 会包含 node 或 tsx
218
+ // Bun 编译的二进制,execPath 就是程序自己的路径
219
+ return !execPath.includes('node') && !execPath.includes('bun');
220
+ }
221
+ /**
222
+ * 执行升级
223
+ */
224
+ export async function performUpgrade(currentVersion) {
225
+ console.log('');
226
+ console2.title('🚀 Pretty-Please 升级');
227
+ console2.muted('━'.repeat(40));
228
+ // 检测平台
229
+ console2.info('检测系统平台...');
230
+ const platform = detectPlatform();
231
+ if (!platform) {
232
+ console2.error('不支持的平台');
233
+ return false;
234
+ }
235
+ console2.success(`平台: ${platform.os} ${platform.arch}`);
236
+ // 获取最新版本
237
+ console2.info('获取最新版本...');
238
+ const latestVersion = await getLatestVersion();
239
+ if (!latestVersion) {
240
+ console2.error('无法获取最新版本');
241
+ return false;
242
+ }
243
+ // 比较版本
244
+ if (compareVersions(latestVersion, currentVersion) <= 0) {
245
+ console2.success(`当前已是最新版本 (${currentVersion})`);
246
+ console.log('');
247
+ return true;
248
+ }
249
+ console2.success(`发现新版本: ${currentVersion} → ${latestVersion}`);
250
+ // 检查安装方式
251
+ if (!isBunBinary()) {
252
+ // 如果是通过 npm/node 运行的,提示使用 npm 更新
253
+ console.log('');
254
+ console2.warning('检测到你是通过 npm 安装的,请使用以下命令更新:');
255
+ console.log('');
256
+ console.log(chalk.hex(getColors().primary)(' npm update -g @yivan-lab/pretty-please'));
257
+ console.log('');
258
+ return false;
259
+ }
260
+ // 获取当前可执行文件路径
261
+ const execPath = getCurrentExecutablePath();
262
+ console2.info(`当前程序: ${execPath}`);
263
+ // 下载新版本
264
+ const downloadUrl = `https://github.com/${REPO}/releases/download/${latestVersion}/${platform.artifact}`;
265
+ const tempFile = path.join(os.tmpdir(), `pls-upgrade-${Date.now()}`);
266
+ console2.info('下载中...');
267
+ try {
268
+ let lastPercent = 0;
269
+ await downloadFile(downloadUrl, tempFile, (percent) => {
270
+ if (percent - lastPercent >= 10 || percent === 100) {
271
+ process.stdout.write(`\r${chalk.hex(getCurrentTheme().primary)('[INFO]')} 下载中... ${percent}%`);
272
+ lastPercent = percent;
273
+ }
274
+ });
275
+ console.log(''); // 换行
276
+ console2.success('下载完成');
277
+ }
278
+ catch (err) {
279
+ console2.error(`下载失败: ${err.message}`);
280
+ return false;
281
+ }
282
+ // 替换当前程序
283
+ console2.info('安装新版本...');
284
+ try {
285
+ // 设置可执行权限
286
+ fs.chmodSync(tempFile, 0o755);
287
+ // 备份旧版本
288
+ const backupPath = `${execPath}.backup`;
289
+ if (fs.existsSync(backupPath)) {
290
+ fs.unlinkSync(backupPath);
291
+ }
292
+ // Windows 需要特殊处理
293
+ if (platform.os === 'windows') {
294
+ // Windows 上无法替换正在运行的程序,创建一个批处理脚本
295
+ const batchScript = `@echo off
296
+ timeout /t 1 /nobreak >nul
297
+ move /y "${execPath}" "${backupPath}" >nul
298
+ move /y "${tempFile}" "${execPath}" >nul
299
+ del "${backupPath}" >nul 2>&1
300
+ echo.
301
+ echo 升级完成! ${currentVersion} → ${latestVersion}
302
+ echo.
303
+ pause
304
+ `;
305
+ const batchPath = path.join(os.tmpdir(), 'pls-upgrade.bat');
306
+ fs.writeFileSync(batchPath, batchScript);
307
+ console.log('');
308
+ console2.warning('Windows 上需要额外步骤完成升级:');
309
+ console.log('');
310
+ console.log(chalk.hex(getColors().primary)(` 请运行: ${batchPath}`));
311
+ console.log('');
312
+ return true;
313
+ }
314
+ // Unix 系统:直接替换
315
+ fs.renameSync(execPath, backupPath);
316
+ fs.renameSync(tempFile, execPath);
317
+ // 删除备份
318
+ try {
319
+ fs.unlinkSync(backupPath);
320
+ }
321
+ catch {
322
+ // 忽略删除备份失败
323
+ }
324
+ console2.muted('━'.repeat(40));
325
+ console2.success(`升级成功: ${currentVersion} → ${latestVersion}`);
326
+ console.log('');
327
+ return true;
328
+ }
329
+ catch (err) {
330
+ console2.error(`安装失败: ${err.message}`);
331
+ // 尝试清理
332
+ try {
333
+ if (fs.existsSync(tempFile)) {
334
+ fs.unlinkSync(tempFile);
335
+ }
336
+ }
337
+ catch { }
338
+ // 如果是权限问题,提示使用 sudo
339
+ if (err.code === 'EACCES' || err.code === 'EPERM') {
340
+ console.log('');
341
+ console2.warning('权限不足,请尝试使用 sudo:');
342
+ console.log('');
343
+ console.log(chalk.hex(getColors().primary)(' sudo pls upgrade'));
344
+ console.log('');
345
+ }
346
+ return false;
347
+ }
348
+ }
@@ -1,19 +1,23 @@
1
1
  import chalk from 'chalk';
2
+ import { getCurrentTheme } from '../ui/theme.js';
2
3
  /**
3
4
  * 原生控制台输出工具函数
4
5
  * 用于不需要 Ink 的场景,避免清屏和性能问题
5
6
  */
6
- // 主题色
7
- const colors = {
8
- primary: '#00D9FF',
9
- secondary: '#A78BFA',
10
- accent: '#F472B6',
11
- success: '#10B981',
12
- error: '#EF4444',
13
- warning: '#F59E0B',
14
- info: '#3B82F6',
15
- muted: '#6B7280',
16
- };
7
+ // 获取当前主题颜色
8
+ function getColors() {
9
+ const theme = getCurrentTheme();
10
+ return {
11
+ primary: theme.primary,
12
+ secondary: theme.secondary,
13
+ accent: theme.accent,
14
+ success: theme.success,
15
+ error: theme.error,
16
+ warning: theme.warning,
17
+ info: theme.info,
18
+ muted: theme.text.muted,
19
+ };
20
+ }
17
21
  /**
18
22
  * 计算字符串的显示宽度(中文占2个宽度)
19
23
  */
@@ -33,6 +37,7 @@ export function getDisplayWidth(str) {
33
37
  * 绘制命令框(原生版本)
34
38
  */
35
39
  export function drawCommandBox(command, title = '生成命令') {
40
+ const colors = getColors();
36
41
  const lines = command.split('\n');
37
42
  const titleWidth = getDisplayWidth(title);
38
43
  const maxContentWidth = Math.max(...lines.map((l) => getDisplayWidth(l)));
@@ -72,30 +77,35 @@ export function printSeparator(text = '输出', length = 38) {
72
77
  * 输出成功消息
73
78
  */
74
79
  export function success(message) {
80
+ const colors = getColors();
75
81
  console.log(chalk.hex(colors.success)('✓ ' + message));
76
82
  }
77
83
  /**
78
84
  * 输出错误消息
79
85
  */
80
86
  export function error(message) {
87
+ const colors = getColors();
81
88
  console.log(chalk.hex(colors.error)('✗ ' + message));
82
89
  }
83
90
  /**
84
91
  * 输出警告消息
85
92
  */
86
93
  export function warning(message) {
94
+ const colors = getColors();
87
95
  console.log(chalk.hex(colors.warning)('⚠️ ' + message));
88
96
  }
89
97
  /**
90
98
  * 输出信息消息
91
99
  */
92
100
  export function info(message) {
101
+ const colors = getColors();
93
102
  console.log(chalk.hex(colors.info)(message));
94
103
  }
95
104
  /**
96
105
  * 输出灰色文本
97
106
  */
98
107
  export function muted(message) {
108
+ const colors = getColors();
99
109
  console.log(chalk.hex(colors.muted)(message));
100
110
  }
101
111
  /**
@@ -108,5 +118,6 @@ export function title(message) {
108
118
  * 输出主色文本
109
119
  */
110
120
  export function primary(message) {
121
+ const colors = getColors();
111
122
  console.log(chalk.hex(colors.primary)(message));
112
123
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yivan-lab/pretty-please",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "AI 驱动的命令行工具,将自然语言转换为可执行的 Shell 命令",
5
5
  "type": "module",
6
6
  "bin": {
@@ -31,12 +31,12 @@
31
31
  "license": "MIT",
32
32
  "repository": {
33
33
  "type": "git",
34
- "url": "https://github.com/yivan-lab/pretty-please.git"
34
+ "url": "https://github.com/IvanLark/pretty-please.git"
35
35
  },
36
36
  "bugs": {
37
- "url": "https://github.com/yivan-lab/pretty-please/issues"
37
+ "url": "https://github.com/IvanLark/pretty-please/issues"
38
38
  },
39
- "homepage": "https://github.com/yivan-lab/pretty-please#readme",
39
+ "homepage": "https://github.com/IvanLark/pretty-please#readme",
40
40
  "publishConfig": {
41
41
  "access": "public"
42
42
  },