jjb-cmd 2.5.6-beat.1 → 2.5.6-beta.2
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/bin/command.js +19 -0
- package/package.json +41 -40
- package/src/push-npm.js +199 -0
package/bin/command.js
CHANGED
|
@@ -75,6 +75,25 @@ commander.command('push [args]').description('-- 发布包').action(async args =
|
|
|
75
75
|
}
|
|
76
76
|
});
|
|
77
77
|
|
|
78
|
+
// npm-publish 命令 - 发布npm包
|
|
79
|
+
commander.command('npm-publish [version]')
|
|
80
|
+
.description('-- 发布npm包(内部命令)')
|
|
81
|
+
.option('--tag <tag>', 'npm发布标签(如:beta、alpha等,默认为latest)')
|
|
82
|
+
.action(async (version, options) => {
|
|
83
|
+
const isLatest = await checkCmdVersion();
|
|
84
|
+
if (!isLatest) {
|
|
85
|
+
process.exit(1);
|
|
86
|
+
}
|
|
87
|
+
if (!version) {
|
|
88
|
+
console.log(`缺少必要参数,请使用:`);
|
|
89
|
+
console.log(` yarn npm-publish <version> 发布npm包(latest标签)`);
|
|
90
|
+
console.log(` yarn npm-publish <version> --tag <tag> 发布npm包(指定标签)`);
|
|
91
|
+
process.exit(0);
|
|
92
|
+
}
|
|
93
|
+
const tag = options.tag || null;
|
|
94
|
+
await require('../src/push-npm.js')(version, tag);
|
|
95
|
+
});
|
|
96
|
+
|
|
78
97
|
// rm-rf 命令
|
|
79
98
|
commander.command('rm-rf').description('-- 删除全部').action(async () => {
|
|
80
99
|
const isLatest = await checkCmdVersion();
|
package/package.json
CHANGED
|
@@ -1,40 +1,41 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "jjb-cmd",
|
|
3
|
-
"use": "no",
|
|
4
|
-
"env": "prod",
|
|
5
|
-
"httpMethod": "http",
|
|
6
|
-
"pushMessage": "yes",
|
|
7
|
-
"version": "2.5.6-
|
|
8
|
-
"description": "jjb-cmd命令行工具",
|
|
9
|
-
"main": "index.js",
|
|
10
|
-
"scripts": {
|
|
11
|
-
"test": "node bin/command.js help",
|
|
12
|
-
"install:package": "npm i",
|
|
13
|
-
"test:ai-pull": "node bin/command.js ai-pull"
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
|
|
22
|
-
"@babel/
|
|
23
|
-
"@babel/
|
|
24
|
-
"@babel/
|
|
25
|
-
"@babel/preset-
|
|
26
|
-
"@babel/preset-
|
|
27
|
-
"@babel/
|
|
28
|
-
"@
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
|
|
40
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "jjb-cmd",
|
|
3
|
+
"use": "no",
|
|
4
|
+
"env": "prod",
|
|
5
|
+
"httpMethod": "http",
|
|
6
|
+
"pushMessage": "yes",
|
|
7
|
+
"version": "2.5.6-beta.2",
|
|
8
|
+
"description": "jjb-cmd命令行工具",
|
|
9
|
+
"main": "index.js",
|
|
10
|
+
"scripts": {
|
|
11
|
+
"test": "node bin/command.js help",
|
|
12
|
+
"install:package": "npm i",
|
|
13
|
+
"test:ai-pull": "node bin/command.js ai-pull",
|
|
14
|
+
"npm-publish": "node bin/command.js npm-publish"
|
|
15
|
+
},
|
|
16
|
+
"bin": {
|
|
17
|
+
"jjb-cmd": "bin/command.js"
|
|
18
|
+
},
|
|
19
|
+
"author": "jiaoxiwei",
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@babel/core": "^7.19.3",
|
|
23
|
+
"@babel/generator": "^7.26.0",
|
|
24
|
+
"@babel/parser": "^7.26.1",
|
|
25
|
+
"@babel/preset-env": "^7.23.2",
|
|
26
|
+
"@babel/preset-react": "^7.18.6",
|
|
27
|
+
"@babel/preset-typescript": "^7.23.2",
|
|
28
|
+
"@babel/traverse": "^7.25.9",
|
|
29
|
+
"@cqsjjb/react-code-optimization": "latest",
|
|
30
|
+
"axios": "^1.1.3",
|
|
31
|
+
"better-sqlite3": "^12.6.2",
|
|
32
|
+
"chalk": "2.4.0",
|
|
33
|
+
"commander": "^1.3.2",
|
|
34
|
+
"compressing": "^1.5.1",
|
|
35
|
+
"inquirer": "^8.2.7",
|
|
36
|
+
"jenkins": "^1.0.2",
|
|
37
|
+
"prettier": "^3.3.3",
|
|
38
|
+
"request": "2.88.2",
|
|
39
|
+
"single-line-log": "1.1.2"
|
|
40
|
+
}
|
|
41
|
+
}
|
package/src/push-npm.js
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const child_process = require('child_process');
|
|
4
|
+
const axios = require('axios');
|
|
5
|
+
const {
|
|
6
|
+
logInfo,
|
|
7
|
+
logSuccess,
|
|
8
|
+
logError,
|
|
9
|
+
logWarning,
|
|
10
|
+
readFile,
|
|
11
|
+
writeFile,
|
|
12
|
+
executeCommand,
|
|
13
|
+
fileExists
|
|
14
|
+
} = require('./utils');
|
|
15
|
+
const { getApiHost } = require('./config');
|
|
16
|
+
const { loadAuth } = require('./crypto-utils');
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 执行npm发布流程
|
|
20
|
+
* @param {string} version - 版本号
|
|
21
|
+
* @param {string} tag - npm tag(可选,默认为latest)
|
|
22
|
+
*/
|
|
23
|
+
module.exports = async (version, tag) => {
|
|
24
|
+
if (!version) {
|
|
25
|
+
logError('请提供版本号,例如: jjb-cmd push 2.5.7 或 jjb-cmd push 2.5.7 --tag beta');
|
|
26
|
+
process.exit(1);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// 如果没有指定tag,默认为latest
|
|
30
|
+
const npmTag = tag || 'latest';
|
|
31
|
+
|
|
32
|
+
const rootPath = path.resolve(__dirname, '..');
|
|
33
|
+
const packageJsonPath = path.join(rootPath, 'package.json');
|
|
34
|
+
|
|
35
|
+
// 读取当前package.json
|
|
36
|
+
let packageJson;
|
|
37
|
+
try {
|
|
38
|
+
const packageJsonContent = readFile(packageJsonPath);
|
|
39
|
+
packageJson = JSON.parse(packageJsonContent);
|
|
40
|
+
} catch (error) {
|
|
41
|
+
logError(`读取package.json失败: ${error.message}`);
|
|
42
|
+
process.exit(1);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const currentVersion = packageJson.version;
|
|
46
|
+
logInfo(`当前版本: ${currentVersion}`);
|
|
47
|
+
logInfo(`目标版本: ${version}`);
|
|
48
|
+
|
|
49
|
+
// 更新package.json中的版本号
|
|
50
|
+
try {
|
|
51
|
+
packageJson.version = version;
|
|
52
|
+
writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2) + '\n');
|
|
53
|
+
logSuccess(`已更新package.json版本号为: ${version}`);
|
|
54
|
+
} catch (error) {
|
|
55
|
+
logError(`更新package.json失败: ${error.message}`);
|
|
56
|
+
process.exit(1);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// 保存原始npm registry配置
|
|
60
|
+
let originalRegistry = 'https://registry.npmmirror.com/';
|
|
61
|
+
try {
|
|
62
|
+
const registryResult = child_process.execSync('npm config get registry', { encoding: 'utf8', cwd: rootPath });
|
|
63
|
+
originalRegistry = registryResult.trim() || 'https://registry.npmmirror.com/';
|
|
64
|
+
} catch (error) {
|
|
65
|
+
logWarning('无法获取当前npm registry配置,将使用默认值');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
try {
|
|
69
|
+
// 1. 切换npm源到官方源
|
|
70
|
+
logInfo('正在切换npm源到官方源...');
|
|
71
|
+
executeCommand('npm config set registry https://registry.npmjs.org/', rootPath);
|
|
72
|
+
logSuccess('npm源已切换到官方源');
|
|
73
|
+
|
|
74
|
+
// 检查是否已登录npm(针对官方源)
|
|
75
|
+
logInfo('正在检查npm登录状态...');
|
|
76
|
+
try {
|
|
77
|
+
const whoamiResult = child_process.execSync('npm whoami --registry=https://registry.npmjs.org/', { encoding: 'utf8', cwd: rootPath, stdio: 'pipe' });
|
|
78
|
+
const npmUser = whoamiResult.trim();
|
|
79
|
+
if (npmUser) {
|
|
80
|
+
logSuccess(`已登录npm账号: ${npmUser}`);
|
|
81
|
+
} else {
|
|
82
|
+
logError('未登录npm官方源,请先执行: npm login --registry=https://registry.npmjs.org/');
|
|
83
|
+
throw new Error('需要先登录npm官方源才能发布');
|
|
84
|
+
}
|
|
85
|
+
} catch (error) {
|
|
86
|
+
if (error.message.includes('需要先登录')) {
|
|
87
|
+
throw error;
|
|
88
|
+
}
|
|
89
|
+
logError('未登录npm官方源,请先执行: npm login --registry=https://registry.npmjs.org/');
|
|
90
|
+
logError('或者执行: npm adduser --registry=https://registry.npmjs.org/');
|
|
91
|
+
throw new Error('需要先登录npm官方源才能发布');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// 2. 发布到npm
|
|
95
|
+
// 注意:npm publish 不需要在命令中指定版本号,版本号已经从package.json中读取
|
|
96
|
+
// 但需要明确指定registry,确保使用官方源
|
|
97
|
+
logInfo(`正在发布版本 ${version} 到npm(tag: ${npmTag})...`);
|
|
98
|
+
logInfo(`执行命令: npm publish --tag ${npmTag} --registry=https://registry.npmjs.org/(版本号 ${version} 已从package.json读取)`);
|
|
99
|
+
try {
|
|
100
|
+
const publishCommand = npmTag === 'latest'
|
|
101
|
+
? 'npm publish --registry=https://registry.npmjs.org/'
|
|
102
|
+
: `npm publish --tag ${npmTag} --registry=https://registry.npmjs.org/`;
|
|
103
|
+
executeCommand(publishCommand, rootPath);
|
|
104
|
+
logSuccess(`版本 ${version} 发布成功(tag: ${npmTag})!`);
|
|
105
|
+
} catch (error) {
|
|
106
|
+
// 如果发布失败,恢复package.json版本号
|
|
107
|
+
packageJson.version = currentVersion;
|
|
108
|
+
writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2) + '\n');
|
|
109
|
+
logError(`npm发布失败: ${error.message}`);
|
|
110
|
+
throw error;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// 3. 切回镜像源
|
|
114
|
+
logInfo('正在切回npm镜像源...');
|
|
115
|
+
executeCommand('npm config set registry https://registry.npmmirror.com/', rootPath);
|
|
116
|
+
logSuccess('npm源已切回镜像源');
|
|
117
|
+
|
|
118
|
+
// 4. 更新数据库中的版本号
|
|
119
|
+
logInfo('正在更新服务器端版本号...');
|
|
120
|
+
try {
|
|
121
|
+
const authPath = path.join(rootPath, '.auth');
|
|
122
|
+
let username, password;
|
|
123
|
+
|
|
124
|
+
// 尝试从认证文件读取
|
|
125
|
+
if (fileExists(authPath)) {
|
|
126
|
+
try {
|
|
127
|
+
const authData = loadAuth(authPath);
|
|
128
|
+
username = authData.username;
|
|
129
|
+
password = authData.password;
|
|
130
|
+
} catch (error) {
|
|
131
|
+
logWarning(`读取认证信息失败: ${error.message}`);
|
|
132
|
+
logWarning('跳过服务器端版本号更新,请手动执行 jjb-cmd update-server-version');
|
|
133
|
+
logSuccess('npm发布完成!');
|
|
134
|
+
process.exit(0);
|
|
135
|
+
}
|
|
136
|
+
} else {
|
|
137
|
+
logWarning('未找到认证信息,跳过服务器端版本号更新');
|
|
138
|
+
logWarning('请手动执行 jjb-cmd update-server-version 来更新服务器端版本号');
|
|
139
|
+
logSuccess('npm发布完成!');
|
|
140
|
+
process.exit(0);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// 调用服务器接口更新版本号
|
|
144
|
+
const apiHost = getApiHost();
|
|
145
|
+
const response = await axios.post(`${apiHost}/api/cmd/update-version`, {
|
|
146
|
+
username,
|
|
147
|
+
password
|
|
148
|
+
}, {
|
|
149
|
+
timeout: 15000
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
if (response.data && response.data.status) {
|
|
153
|
+
const { version: updatedVersion, previousVersion } = response.data.data;
|
|
154
|
+
logSuccess('服务器端版本号更新成功!');
|
|
155
|
+
if (previousVersion) {
|
|
156
|
+
logInfo(` 旧版本: ${previousVersion}`);
|
|
157
|
+
}
|
|
158
|
+
logInfo(` 新版本: ${updatedVersion}`);
|
|
159
|
+
} else {
|
|
160
|
+
logWarning(`服务器端版本号更新失败: ${response.data.message || '未知错误'}`);
|
|
161
|
+
logWarning('请手动执行 jjb-cmd update-server-version 来更新服务器端版本号');
|
|
162
|
+
}
|
|
163
|
+
} catch (error) {
|
|
164
|
+
if (error && error.response) {
|
|
165
|
+
const status = error.response.status;
|
|
166
|
+
const serverMsg = (error.response.data && (error.response.data.message || error.response.data.msg)) || '';
|
|
167
|
+
logWarning(`服务器端版本号更新失败 (${status}): ${serverMsg || error.message}`);
|
|
168
|
+
} else if (error && error.request) {
|
|
169
|
+
logWarning('网络连接失败: 无法连接到服务器');
|
|
170
|
+
} else {
|
|
171
|
+
logWarning(`服务器端版本号更新失败: ${error.message || '未知错误'}`);
|
|
172
|
+
}
|
|
173
|
+
logWarning('请手动执行 jjb-cmd update-server-version 来更新服务器端版本号');
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
logSuccess('所有操作完成!');
|
|
177
|
+
process.exit(0);
|
|
178
|
+
} catch (error) {
|
|
179
|
+
// 发生错误时,尝试恢复npm源
|
|
180
|
+
try {
|
|
181
|
+
logInfo('正在恢复npm源...');
|
|
182
|
+
executeCommand(`npm config set registry ${originalRegistry}`, rootPath);
|
|
183
|
+
logInfo('npm源已恢复');
|
|
184
|
+
} catch (restoreError) {
|
|
185
|
+
logError(`恢复npm源失败: ${restoreError.message}`);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// 恢复package.json版本号
|
|
189
|
+
try {
|
|
190
|
+
packageJson.version = currentVersion;
|
|
191
|
+
writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2) + '\n');
|
|
192
|
+
logInfo('已恢复package.json版本号');
|
|
193
|
+
} catch (restoreError) {
|
|
194
|
+
logError(`恢复package.json失败: ${restoreError.message}`);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
process.exit(1);
|
|
198
|
+
}
|
|
199
|
+
};
|