fe-build-cli 1.5.0 → 1.6.1

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 CHANGED
@@ -206,6 +206,68 @@ fe-build rollback production --version build-20240101-abc123
206
206
  请选择要回滚的备份 (1-3):
207
207
  ```
208
208
 
209
+ ### update(更新)
210
+
211
+ ```bash
212
+ fe-build update [--force|--auto]
213
+ fe-build check-update
214
+ ```
215
+
216
+ | 参数 | 说明 |
217
+ |------|------|
218
+ | `--force` | 自动更新,无需确认 |
219
+ | `--auto` | 自动更新,无需确认(同 --force) |
220
+
221
+ **功能说明:**
222
+
223
+ - `update` - 检查并更新到最新版本(交互确认)
224
+ - `update --force` - 自动更新,无需确认
225
+ - `check-update` - 仅检查是否有新版本
226
+
227
+ **示例:**
228
+
229
+ ```bash
230
+ # 检查并更新(需要确认)
231
+ fe-build update
232
+
233
+ # 自动更新(无需确认)
234
+ fe-build update --force
235
+ fe-build update --auto
236
+
237
+ # 仅检查是否有更新
238
+ fe-build check-update
239
+
240
+ # 手动更新(推荐)
241
+ npm update fe-build-cli --global
242
+ ```
243
+
244
+ **更新输出示例:**
245
+
246
+ ```
247
+ ========================================
248
+ 🔄 fe-build-cli 版本检查
249
+ ========================================
250
+ 当前版本: 1.5.0
251
+ 最新版本: 1.6.0
252
+
253
+ 📌 发现新版本!
254
+
255
+ 更新方法:
256
+ fe-build update # 自动更新
257
+ npm update fe-build-cli --global # 手动更新
258
+ ========================================
259
+ ```
260
+
261
+ ### version(版本)
262
+
263
+ ```bash
264
+ fe-build version
265
+ fe-build --version
266
+ fe-build -v
267
+ ```
268
+
269
+ 显示当前安装的版本号。
270
+
209
271
  ### help(帮助)
210
272
 
211
273
  ```bash
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fe-build-cli",
3
- "version": "1.5.0",
3
+ "version": "1.6.1",
4
4
  "description": "前端项目打包部署 CLI 工具,支持多服务器部署、分支管理、回滚、钉钉通知、智能处理本地改动等功能",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
package/src/cli.js CHANGED
@@ -23,6 +23,7 @@ import {
23
23
  sendRollbackNotification
24
24
  } from './dingtalk.js';
25
25
  import { DeployLogger } from './logger.js';
26
+ import { checkForUpdate, performUpdate, showUpdateInfo, getCurrentVersion } from './update.js';
26
27
 
27
28
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
28
29
 
@@ -168,6 +169,10 @@ fe-build-cli - 前端项目打包部署工具
168
169
  命令:
169
170
  deploy [环境] 部署到指定环境(默认命令)
170
171
  rollback [环境] 回滚到指定版本(交互选择备份来源)
172
+ update 检查并更新到最新版本
173
+ update --force 自动更新(无需确认)
174
+ check-update 仅检查是否有新版本
175
+ version 显示当前版本号
171
176
  help 显示帮助信息
172
177
 
173
178
  选项:
@@ -723,6 +728,26 @@ function formatFileSize(bytes) {
723
728
  return (bytes / (1024 * 1024)).toFixed(2) + ' MB';
724
729
  }
725
730
 
731
+ /**
732
+ * 启动时检查更新
733
+ */
734
+ async function checkUpdateOnStart() {
735
+ try {
736
+ const info = await checkForUpdate();
737
+ if (info && info.hasUpdate) {
738
+ console.log('\n========================================');
739
+ console.log(' 🔄 发现新版本');
740
+ console.log('========================================');
741
+ console.log(`当前版本: ${info.currentVersion}`);
742
+ console.log(`最新版本: ${info.latestVersion}`);
743
+ console.log('\n更新命令: fe-build update --force');
744
+ console.log('========================================\n');
745
+ }
746
+ } catch (error) {
747
+ // 检查更新失败,不影响主流程
748
+ }
749
+ }
750
+
726
751
  /**
727
752
  * 主入口
728
753
  */
@@ -736,6 +761,42 @@ async function main() {
736
761
  process.exit(0);
737
762
  }
738
763
 
764
+ // 显示版本
765
+ if (command === 'version' || args.includes('--version') || args.includes('-v')) {
766
+ const version = getCurrentVersion();
767
+ console.log(`fe-build-cli v${version}`);
768
+ process.exit(0);
769
+ }
770
+
771
+ // 检查更新
772
+ if (command === 'check-update') {
773
+ await showUpdateInfo();
774
+ process.exit(0);
775
+ }
776
+
777
+ // 执行更新
778
+ if (command === 'update') {
779
+ const forceUpdate = args.includes('--force') || args.includes('--auto');
780
+ const info = await showUpdateInfo();
781
+
782
+ if (info && info.hasUpdate) {
783
+ if (forceUpdate) {
784
+ // 自动更新,无需确认
785
+ await performUpdate(true);
786
+ } else {
787
+ // 交互确认
788
+ const answer = await prompt('\n是否立即更新? (y/n): ');
789
+ if (answer.toLowerCase() === 'y') {
790
+ await performUpdate(true);
791
+ }
792
+ }
793
+ }
794
+ process.exit(0);
795
+ }
796
+
797
+ // 其他命令启动时检查更新(静默检查)
798
+ await checkUpdateOnStart();
799
+
739
800
  // 加载配置
740
801
  const config = await loadConfig();
741
802
 
package/src/index.js CHANGED
@@ -59,5 +59,14 @@ export {
59
59
  cleanLocalBackups
60
60
  } from './logger.js';
61
61
 
62
+ // 更新模块
63
+ export {
64
+ getCurrentVersion,
65
+ getLatestVersion,
66
+ checkForUpdate,
67
+ performUpdate,
68
+ showUpdateInfo
69
+ } from './update.js';
70
+
62
71
  // 配置模板
63
72
  export { default as configTemplate } from './config-template.js';
package/src/update.js ADDED
@@ -0,0 +1,141 @@
1
+ import { execSync } from 'node:child_process';
2
+ import process from 'node:process';
3
+ import fs from 'node:fs';
4
+ import path from 'node:path';
5
+ import { fileURLToPath } from 'node:url';
6
+
7
+ /**
8
+ * 更新模块
9
+ * 支持检查更新和自动更新
10
+ */
11
+
12
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
13
+
14
+ /**
15
+ * 获取当前版本
16
+ * @returns {string} 当前版本号
17
+ */
18
+ export function getCurrentVersion() {
19
+ try {
20
+ // 从 package.json 获取版本
21
+ const packageJsonPath = path.join(__dirname, '..', 'package.json');
22
+ const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
23
+ return packageJson.version;
24
+ } catch (error) {
25
+ // 通过 npm 命令获取
26
+ try {
27
+ const version = execSync('npm list fe-build-cli --global --depth=0', { encoding: 'utf-8' });
28
+ const match = version.match(/fe-build-cli@(\d+\.\d+\.\d+)/);
29
+ return match ? match[1] : '未知';
30
+ } catch (e) {
31
+ return '未知';
32
+ }
33
+ }
34
+ }
35
+
36
+ /**
37
+ * 获取 npm 最新版本
38
+ * @returns {Promise<string>} 最新版本号
39
+ */
40
+ export async function getLatestVersion() {
41
+ try {
42
+ // 使用 --silent 参数避免显示 npm 警告
43
+ const result = execSync('npm view fe-build-cli version --silent 2>nul', {
44
+ encoding: 'utf-8',
45
+ timeout: 10000,
46
+ stdio: ['pipe', 'pipe', 'pipe'] // 隐藏 stderr
47
+ });
48
+ return result.trim();
49
+ } catch (error) {
50
+ throw new Error('无法获取最新版本,请检查网络连接或 npm 源');
51
+ }
52
+ }
53
+
54
+ /**
55
+ * 检查是否有更新
56
+ * @returns {Promise<object>} 更新信息
57
+ */
58
+ export async function checkForUpdate() {
59
+ const currentVersion = await getCurrentVersion();
60
+ const latestVersion = await getLatestVersion();
61
+
62
+ const hasUpdate = currentVersion !== latestVersion && latestVersion !== '未知';
63
+
64
+ return {
65
+ currentVersion,
66
+ latestVersion,
67
+ hasUpdate
68
+ };
69
+ }
70
+
71
+ /**
72
+ * 执行更新
73
+ * @param {boolean} global - 是否全局更新
74
+ * @returns {Promise<boolean>} 是否成功
75
+ */
76
+ export async function performUpdate(global = true) {
77
+ try {
78
+ console.log('\n正在更新 fe-build-cli...');
79
+
80
+ const command = global
81
+ ? 'npm update fe-build-cli --global'
82
+ : 'npm update fe-build-cli';
83
+
84
+ execSync(command, { stdio: 'inherit', timeout: 60000 });
85
+
86
+ console.log('\n✅ 更新完成!');
87
+
88
+ // 显示更新后的版本
89
+ const newVersion = await getLatestVersion();
90
+ console.log(`当前版本: ${newVersion}`);
91
+
92
+ return true;
93
+ } catch (error) {
94
+ console.error('\n❌ 更新失败:', error.message);
95
+ console.log('\n请尝试手动更新:');
96
+ console.log(' npm update fe-build-cli --global');
97
+ return false;
98
+ }
99
+ }
100
+
101
+ /**
102
+ * 显示更新信息
103
+ */
104
+ export async function showUpdateInfo() {
105
+ console.log('\n========================================');
106
+ console.log(' 🔄 fe-build-cli 版本检查');
107
+ console.log('========================================');
108
+
109
+ try {
110
+ const info = await checkForUpdate();
111
+
112
+ console.log(`当前版本: ${info.currentVersion}`);
113
+ console.log(`最新版本: ${info.latestVersion}`);
114
+
115
+ if (info.hasUpdate) {
116
+ console.log('\n📌 发现新版本!');
117
+ console.log('\n更新方法:');
118
+ console.log(' fe-build update # 自动更新');
119
+ console.log(' npm update fe-build-cli --global # 手动更新');
120
+ } else {
121
+ console.log('\n✅ 已是最新版本');
122
+ }
123
+
124
+ console.log('========================================');
125
+
126
+ return info;
127
+ } catch (error) {
128
+ console.error('❌ 检查更新失败:', error.message);
129
+ console.log('\n请检查网络连接或手动更新:');
130
+ console.log(' npm update fe-build-cli --global');
131
+ return null;
132
+ }
133
+ }
134
+
135
+ export default {
136
+ getCurrentVersion,
137
+ getLatestVersion,
138
+ checkForUpdate,
139
+ performUpdate,
140
+ showUpdateInfo
141
+ };