@vitarx/release-cli 1.0.4 → 1.0.5

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 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 文件
@@ -198,16 +201,15 @@ async function publishPackage(pkgDir, packageManager, isDryRun = false, args) {
198
201
  runCommand(`git push${tagName ? ' && git push --tags' : ''}`, isDryRun);
199
202
  }
200
203
  catch {
201
- log.warn('包已发布到pnm,但推送代码和标签到远程仓库意外失败,请仔细核查npm仓库和远程仓库状态。');
204
+ log.warn('包已发布到pnm,但推送代码和标签到远程仓库意外失败,请仔细核查npm仓库和Git远程仓库状态。');
202
205
  }
203
206
  // 打印发布成功信息
204
207
  log.success(`发布完成 ${pkg.name}@${newVersion}` + (isDryRun ? ' (dry-run)' : ''));
205
208
  }
206
209
  catch (err) {
207
- // 错误处理
208
- log.error(err.message);
210
+ console.error(err);
209
211
  // 回滚更改
210
- rollbackChanges(tagName, isDryRun);
212
+ rollbackChanges(tagName, isDryRun, lastCommitHash);
211
213
  process.exit(1);
212
214
  }
213
215
  }
@@ -279,6 +281,21 @@ export async function main() {
279
281
  log.info('当前运行模式', isDryRun ? 'dry-run' : 'publish');
280
282
  const isSubmit = checkGitStatus();
281
283
  log.info('当前工作目录状态', isSubmit ? '已提交' : '未提交');
284
+ // 记录当前最新的提交哈希,用于回滚
285
+ let lastCommitHash = '';
286
+ // 如果不是试运行模式,记录当前最新的提交哈希
287
+ if (!isDryRun) {
288
+ try {
289
+ // 获取最新提交的哈希值
290
+ lastCommitHash = execSync('git rev-parse HEAD').toString().trim();
291
+ log.info('当前最新一条提交', lastCommitHash);
292
+ }
293
+ catch (err) {
294
+ console.error(err);
295
+ log.warn('无法获取当前GIT提交信息');
296
+ process.exit(1);
297
+ }
298
+ }
282
299
  // 如果有未提交的更改,则报错退出
283
300
  if (!isDryRun && !isSubmit) {
284
301
  log.error('请先提交或暂存当前更改再发布。');
@@ -329,11 +346,11 @@ export async function main() {
329
346
  }
330
347
  selectedPackage = packages[index - 1];
331
348
  }
332
- await publishPackage(selectedPackage.path, packageManager, isDryRun, args);
349
+ await publishPackage(selectedPackage.path, packageManager, isDryRun, args, lastCommitHash);
333
350
  }
334
351
  else {
335
352
  // 单包项目,发布根目录
336
- await publishPackage('.', packageManager, isDryRun, args);
353
+ await publishPackage('.', packageManager, isDryRun, args, lastCommitHash);
337
354
  }
338
355
  }
339
356
  // 执行主函数
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?: string, isDryRun?: boolean): void;
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 = false) {
346
+ export function rollbackChanges(tagName, isDryRun, lastCommitHash) {
346
347
  // 输出警告信息,提示用户正在回滚
347
348
  log.warn('发生错误,正在回滚...');
348
349
  try {
349
350
  // 执行git命令,回退到上一个提交
350
- runCommand('git reset --hard HEAD~1', isDryRun);
351
+ runCommand(`git reset --hard ${lastCommitHash}`, isDryRun);
351
352
  }
352
353
  catch {
353
354
  // 捕获并忽略执行过程中的错误
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitarx/release-cli",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "A CLI tool for automated npm package release with workspace support",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",