@vitarx/release-cli 1.0.4 → 1.0.6
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/dist/release.js +26 -7
- package/dist/utils.d.ts +2 -1
- package/dist/utils.js +3 -2
- package/package.json +1 -1
package/dist/release.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env ts-node
|
|
2
2
|
import chalk from 'chalk';
|
|
3
|
+
import { execSync } from 'node:child_process';
|
|
4
|
+
import * as console from 'node:console';
|
|
3
5
|
import fs from 'node:fs';
|
|
4
6
|
import path from 'node:path';
|
|
5
7
|
import process from 'node:process';
|
|
@@ -98,8 +100,9 @@ function parseArgs() {
|
|
|
98
100
|
* @param packageManager - 检测到的包管理器类型
|
|
99
101
|
* @param isDryRun - 是否为试运行模式,默认为 false
|
|
100
102
|
* @param args - 命令行参数对象
|
|
103
|
+
* @param lastCommitHash - 最后一次提交哈希值
|
|
101
104
|
*/
|
|
102
|
-
async function publishPackage(pkgDir, packageManager, isDryRun = false, args) {
|
|
105
|
+
async function publishPackage(pkgDir, packageManager, isDryRun = false, args, lastCommitHash) {
|
|
103
106
|
// 获取 package.json 的完整路径
|
|
104
107
|
const pkgPath = path.join(pkgDir, 'package.json');
|
|
105
108
|
// 读取并解析 package.json 文件
|
|
@@ -131,6 +134,7 @@ async function publishPackage(pkgDir, packageManager, isDryRun = false, args) {
|
|
|
131
134
|
const confirm = await prompt(`是否确认发布 ${pkg.name}@${newVersion}? (y/N) `);
|
|
132
135
|
if (confirm && confirm.toLowerCase() !== 'y') {
|
|
133
136
|
log.warn('已取消发布');
|
|
137
|
+
rollbackChanges(undefined, isDryRun, lastCommitHash);
|
|
134
138
|
process.exit(0);
|
|
135
139
|
}
|
|
136
140
|
// -------------------- Step 4: 构建和测试项目 --------------------
|
|
@@ -171,6 +175,7 @@ async function publishPackage(pkgDir, packageManager, isDryRun = false, args) {
|
|
|
171
175
|
const confirm = await prompt(chalk.yellow('⚠️ 生成 changelog 失败,要继续发布流程吗? (y/N) '));
|
|
172
176
|
if (confirm.toLowerCase() !== 'y') {
|
|
173
177
|
log.warn('已取消发布');
|
|
178
|
+
rollbackChanges(undefined, isDryRun, lastCommitHash);
|
|
174
179
|
process.exit(0);
|
|
175
180
|
}
|
|
176
181
|
}
|
|
@@ -198,16 +203,15 @@ async function publishPackage(pkgDir, packageManager, isDryRun = false, args) {
|
|
|
198
203
|
runCommand(`git push${tagName ? ' && git push --tags' : ''}`, isDryRun);
|
|
199
204
|
}
|
|
200
205
|
catch {
|
|
201
|
-
log.warn('包已发布到pnm,但推送代码和标签到远程仓库意外失败,请仔细核查npm
|
|
206
|
+
log.warn('包已发布到pnm,但推送代码和标签到远程仓库意外失败,请仔细核查npm仓库和Git远程仓库状态。');
|
|
202
207
|
}
|
|
203
208
|
// 打印发布成功信息
|
|
204
209
|
log.success(`发布完成 ${pkg.name}@${newVersion}` + (isDryRun ? ' (dry-run)' : ''));
|
|
205
210
|
}
|
|
206
211
|
catch (err) {
|
|
207
|
-
|
|
208
|
-
log.error(err.message);
|
|
212
|
+
console.error(err);
|
|
209
213
|
// 回滚更改
|
|
210
|
-
rollbackChanges(tagName, isDryRun);
|
|
214
|
+
rollbackChanges(tagName, isDryRun, lastCommitHash);
|
|
211
215
|
process.exit(1);
|
|
212
216
|
}
|
|
213
217
|
}
|
|
@@ -279,6 +283,21 @@ export async function main() {
|
|
|
279
283
|
log.info('当前运行模式', isDryRun ? 'dry-run' : 'publish');
|
|
280
284
|
const isSubmit = checkGitStatus();
|
|
281
285
|
log.info('当前工作目录状态', isSubmit ? '已提交' : '未提交');
|
|
286
|
+
// 记录当前最新的提交哈希,用于回滚
|
|
287
|
+
let lastCommitHash = '';
|
|
288
|
+
// 如果不是试运行模式,记录当前最新的提交哈希
|
|
289
|
+
if (!isDryRun) {
|
|
290
|
+
try {
|
|
291
|
+
// 获取最新提交的哈希值
|
|
292
|
+
lastCommitHash = execSync('git rev-parse HEAD').toString().trim();
|
|
293
|
+
log.info('当前最新一条提交', lastCommitHash);
|
|
294
|
+
}
|
|
295
|
+
catch (err) {
|
|
296
|
+
console.error(err);
|
|
297
|
+
log.warn('无法获取当前GIT提交信息');
|
|
298
|
+
process.exit(1);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
282
301
|
// 如果有未提交的更改,则报错退出
|
|
283
302
|
if (!isDryRun && !isSubmit) {
|
|
284
303
|
log.error('请先提交或暂存当前更改再发布。');
|
|
@@ -329,11 +348,11 @@ export async function main() {
|
|
|
329
348
|
}
|
|
330
349
|
selectedPackage = packages[index - 1];
|
|
331
350
|
}
|
|
332
|
-
await publishPackage(selectedPackage.path, packageManager, isDryRun, args);
|
|
351
|
+
await publishPackage(selectedPackage.path, packageManager, isDryRun, args, lastCommitHash);
|
|
333
352
|
}
|
|
334
353
|
else {
|
|
335
354
|
// 单包项目,发布根目录
|
|
336
|
-
await publishPackage('.', packageManager, isDryRun, args);
|
|
355
|
+
await publishPackage('.', packageManager, isDryRun, args, lastCommitHash);
|
|
337
356
|
}
|
|
338
357
|
}
|
|
339
358
|
// 执行主函数
|
package/dist/utils.d.ts
CHANGED
|
@@ -82,8 +82,9 @@ export declare function runCommand(cmd: string, isDryRun?: boolean): void;
|
|
|
82
82
|
* 回滚代码变更的函数
|
|
83
83
|
* @param tagName - 可选的标签名称,用于删除创建的标签
|
|
84
84
|
* @param isDryRun - 是否为试运行模式,默认为false
|
|
85
|
+
* @param lastCommitHash
|
|
85
86
|
*/
|
|
86
|
-
export declare function rollbackChanges(tagName
|
|
87
|
+
export declare function rollbackChanges(tagName: string | undefined, isDryRun: boolean, lastCommitHash: string): void;
|
|
87
88
|
/**
|
|
88
89
|
* 更新指定包的版本号
|
|
89
90
|
* @param pkgPath - 包的文件路径
|
package/dist/utils.js
CHANGED
|
@@ -341,13 +341,14 @@ export function runCommand(cmd, isDryRun = false) {
|
|
|
341
341
|
* 回滚代码变更的函数
|
|
342
342
|
* @param tagName - 可选的标签名称,用于删除创建的标签
|
|
343
343
|
* @param isDryRun - 是否为试运行模式,默认为false
|
|
344
|
+
* @param lastCommitHash
|
|
344
345
|
*/
|
|
345
|
-
export function rollbackChanges(tagName, isDryRun
|
|
346
|
+
export function rollbackChanges(tagName, isDryRun, lastCommitHash) {
|
|
346
347
|
// 输出警告信息,提示用户正在回滚
|
|
347
348
|
log.warn('发生错误,正在回滚...');
|
|
348
349
|
try {
|
|
349
350
|
// 执行git命令,回退到上一个提交
|
|
350
|
-
runCommand(
|
|
351
|
+
runCommand(`git reset --hard ${lastCommitHash}`, isDryRun);
|
|
351
352
|
}
|
|
352
353
|
catch {
|
|
353
354
|
// 捕获并忽略执行过程中的错误
|