daodou-command 1.1.2 → 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/CHANGELOG.md CHANGED
@@ -12,6 +12,25 @@
12
12
  ### 修复
13
13
  ### 移除
14
14
 
15
+ ## [1.2.0] - 2025-09-16
16
+
17
+ ### 新增
18
+ - 添加 `dao upgrade` 命令用于检查和更新工具版本
19
+ - 支持 `--check` 选项仅检查版本不更新
20
+ - 支持 `--force` 选项强制更新
21
+ - 自动从npm获取最新版本信息
22
+ - 智能版本比较和更新提示
23
+ - 主程序自动读取package.json中的版本号,无需手动维护
24
+
25
+ ### 更改
26
+ - 将update命令重命名为upgrade,更符合CLI工具命名习惯
27
+ - 优化版本管理流程,减少手动维护工作
28
+
29
+ ### 技术改进
30
+ - 改进错误处理和用户反馈
31
+ - 添加彩色输出和加载动画
32
+ - 增强网络超时和错误恢复机制
33
+
15
34
  ## [1.1.2] - 2025-09-16
16
35
 
17
36
  ### 修复
package/bin/daodou.js CHANGED
@@ -34,6 +34,22 @@ program
34
34
  }
35
35
  });
36
36
 
37
+ // 添加 upgrade 命令
38
+ program
39
+ .command('upgrade')
40
+ .description('检查并更新daodou-command到最新版本')
41
+ .option('-c, --check', '仅检查是否有新版本,不执行更新')
42
+ .option('-f, --force', '强制更新到最新版本')
43
+ .action(async (options) => {
44
+ try {
45
+ const upgradeCommand = require('../lib/commands/upgrade');
46
+ await upgradeCommand.execute(options);
47
+ } catch (error) {
48
+ console.error(chalk.red('更新失败:'), error.message);
49
+ process.exit(1);
50
+ }
51
+ });
52
+
37
53
  // 添加 lang 命令
38
54
  const langCmd = program
39
55
  .command('lang')
@@ -75,6 +91,10 @@ program.addHelpText('after', `
75
91
  $ dao build --jenkins-url http://jenkins.example.com
76
92
  $ dao build --parameters '{"param1":"value1"}'
77
93
 
94
+ $ dao upgrade # 检查并更新到最新版本
95
+ $ dao upgrade --check # 仅检查是否有新版本
96
+ $ dao upgrade --force # 强制更新到最新版本
97
+
78
98
  $ dao lang add "hello"
79
99
  $ dao lang add "hello" "Hello World"
80
100
  $ dao lang add "world" --lang en
@@ -0,0 +1,151 @@
1
+ const axios = require('axios');
2
+ const chalk = require('chalk');
3
+ const ora = require('ora');
4
+ const { execSync } = require('child_process');
5
+ const packageJson = require('../../package.json');
6
+
7
+ /**
8
+ * 获取npm上的最新版本号
9
+ * @returns {Promise<string>} 最新版本号
10
+ */
11
+ async function getLatestVersion() {
12
+ try {
13
+ const response = await axios.get(`https://registry.npmjs.org/${packageJson.name}/latest`, {
14
+ timeout: 10000
15
+ });
16
+ return response.data.version;
17
+ } catch (error) {
18
+ throw new Error(`获取最新版本失败: ${error.message}`);
19
+ }
20
+ }
21
+
22
+ /**
23
+ * 获取当前安装的版本号
24
+ * @returns {string} 当前版本号
25
+ */
26
+ function getCurrentVersion() {
27
+ return packageJson.version;
28
+ }
29
+
30
+ /**
31
+ * 比较版本号
32
+ * @param {string} current 当前版本
33
+ * @param {string} latest 最新版本
34
+ * @returns {boolean} 是否有更新
35
+ */
36
+ function hasUpdate(current, latest) {
37
+ const currentParts = current.split('.').map(Number);
38
+ const latestParts = latest.split('.').map(Number);
39
+
40
+ for (let i = 0; i < Math.max(currentParts.length, latestParts.length); i++) {
41
+ const currentPart = currentParts[i] || 0;
42
+ const latestPart = latestParts[i] || 0;
43
+
44
+ if (latestPart > currentPart) {
45
+ return true;
46
+ } else if (latestPart < currentPart) {
47
+ return false;
48
+ }
49
+ }
50
+
51
+ return false;
52
+ }
53
+
54
+ /**
55
+ * 执行npm更新
56
+ * @param {string} version 要更新的版本
57
+ */
58
+ function updatePackage(version) {
59
+ try {
60
+ console.log(chalk.blue(`正在更新到版本 ${version}...`));
61
+ execSync(`npm install -g ${packageJson.name}@${version} --force`, {
62
+ stdio: 'inherit',
63
+ timeout: 300000 // 5分钟超时
64
+ });
65
+ console.log(chalk.green(`✅ 成功更新到版本 ${version}`));
66
+ } catch (error) {
67
+ throw new Error(`更新失败: ${error.message}`);
68
+ }
69
+ }
70
+
71
+ /**
72
+ * 显示更新信息
73
+ * @param {string} current 当前版本
74
+ * @param {string} latest 最新版本
75
+ * @param {boolean} hasUpdateAvailable 是否有更新
76
+ */
77
+ function displayUpdateInfo(current, latest, hasUpdateAvailable) {
78
+ console.log(chalk.cyan('📦 daodou-command 版本信息:'));
79
+ console.log(` 当前版本: ${chalk.yellow(current)}`);
80
+ console.log(` 最新版本: ${chalk.yellow(latest)}`);
81
+
82
+ if (hasUpdateAvailable) {
83
+ console.log(chalk.red(' ⚠️ 有新版本可用!'));
84
+ console.log(chalk.gray(' 使用 "dao update" 命令进行更新'));
85
+ } else {
86
+ console.log(chalk.green(' ✅ 已是最新版本'));
87
+ }
88
+ }
89
+
90
+ /**
91
+ * 执行更新命令
92
+ * @param {Object} options 命令选项
93
+ */
94
+ async function execute(options) {
95
+ const spinner = ora('检查更新中...').start();
96
+
97
+ try {
98
+ // 获取版本信息
99
+ const currentVersion = getCurrentVersion();
100
+ const latestVersion = await getLatestVersion();
101
+ const hasUpdateAvailable = hasUpdate(currentVersion, latestVersion);
102
+
103
+ spinner.stop();
104
+
105
+ // 显示版本信息
106
+ displayUpdateInfo(currentVersion, latestVersion, hasUpdateAvailable);
107
+
108
+ // 如果只是检查版本,直接返回
109
+ if (options.check) {
110
+ return;
111
+ }
112
+
113
+ // 如果没有更新且不是强制更新,提示用户
114
+ if (!hasUpdateAvailable && !options.force) {
115
+ console.log(chalk.gray(' 无需更新'));
116
+ return;
117
+ }
118
+
119
+ // 如果有更新或强制更新,执行更新
120
+ if (hasUpdateAvailable || options.force) {
121
+ console.log(); // 空行
122
+
123
+ if (options.force && !hasUpdateAvailable) {
124
+ console.log(chalk.yellow('⚠️ 强制更新到当前版本'));
125
+ }
126
+
127
+ updatePackage(latestVersion);
128
+
129
+ // 验证更新结果
130
+ console.log(chalk.blue('验证更新结果...'));
131
+ try {
132
+ const { execSync } = require('child_process');
133
+ const newVersion = execSync('dao --version', { encoding: 'utf8' }).trim();
134
+ console.log(chalk.green(`✅ 更新完成!当前版本: ${newVersion}`));
135
+ } catch (error) {
136
+ console.log(chalk.yellow('⚠️ 更新完成,但无法验证版本号'));
137
+ }
138
+ }
139
+
140
+ } catch (error) {
141
+ spinner.stop();
142
+ throw error;
143
+ }
144
+ }
145
+
146
+ module.exports = {
147
+ execute,
148
+ getLatestVersion,
149
+ getCurrentVersion,
150
+ hasUpdate
151
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "daodou-command",
3
- "version": "1.1.2",
3
+ "version": "1.2.0",
4
4
  "description": "刀豆命令行工具 - 自动化构建和部署",
5
5
  "main": "index.js",
6
6
  "bin": {