V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
liutao1998
V2EX  ›  分享创造

一款 Changelog 日志生成工具(应用于 npm 包或应用项目中)

  •  
  •   liutao1998 · 14 天前 · 601 次点击

    Changelog 生成工具

    github 地址

    @eat-fish/changelog 是一款专用于 npm 包版本升级与 版本变更日志生成的工具, 也可以使用在应用项目中

    通过提取 commit 信息来生成 CHANGELOG.md 文件

    在发布新版本前使用 @eat-fish/changelog 来生成下一个版本号与 CHANGELOG 变更日志

    通过自定义 commit matcher 可以实现基于 commit 提取生成或者 基于 PR 提取生成两种方式的 CHANGELOG 日志

    示例项目

    基于 commit 方案

    通过 自定义 配置文件 matcher 函数来提取 特定 commit 生成

    module.exports = {
      matcher: (rawCommitInfo) => {
        const { message } = rawCommitInfo;
    
        // 提取 feat 、fix 类型 的 commit
        const [, type, scope, description] = message.match(/(feat|fix)(?:\(([^)]*?)\))?:\s?(.+)/) || [];
    
        if (!type || !description) return false;
    
        return {
          type,
          scope,
          description,
        };
      },
    };
    

    changelog-commit-example

    CHANGELOG 示例查看

    基于 PR 方案

    通过 自定义 配置文件 matcher 函数来提取 特定 PR commit 生成

    module.exports = {
      matcher: (rawCommitInfo) => {
        const { message, description: rawDescription } = rawCommitInfo;
    
        // 先过滤 PR 类型 commit
        const messageMatchRes = message.match(/Merge pull request #\d+ from ([^\\]+)\/(.+)/) || [];
    
        if (!messageMatchRes) return false;
    
        const [, author] = messageMatchRes;
    
        // 再提取 commit 信息
        const [, type, scope, description] = rawDescription.match(/(feat|fix)(?:\(([^)]*?)\))?:\s?(.+)/) || [];
    
        if (!type || !description) return false;
    
        return {
          type,
          scope,
          description,
          author,
        };
      },
    };
    

    changelog-pr-example

    CHANGELOG 示例查看

    基于 MR 方案

    在 大部分公司项目中, 是走 MR 合并

    使用

    安装

    pnpm add @eat-fish/changelog@latest -D
    

    手动创建第一个 tag

    如果是首次引入 @eat-fish/changelog, 需要先手动创建第一个 tag

    使用如下命令, 创建的 tag 名为 v+ 当前 package.json 中的版本号, 例如 v1.0.0

    git tag -a v1.0.0 -m "Version 1.0.0"
    

    开始

    运行前确保 没有 package.json 文件以及 CHANGELOG.md 文件的变更

    然后执行升级并生成 CHANGELOG 命令

    # 可以选择你想升级的版本
    
    # fix 版本变更: 1.0.0 -> 1.0.1
    pnpm changelog release patch
    
    # feature 版本变更: 1.0.0 -> 1.1.0
    pnpm changelog release minor
    
    # feature 版本变更: 1.0.0 -> 2.0.0
    pnpm changelog release major
    

    确认无误后提交升级变更

    运行完成后将自动 commit

    包含 package.json 版本号变更以及 CHANGELOG.md 日志变更

    确认无误后可以提交上去, 如有问题则使用一下命令撤回

    git reset --hard HEAD~1
    

    后续如果是 npm 包可以正常走 npm publish 发布

    如果是应用项目则结束了

    2 条回复    2024-04-17 00:21:01 +08:00
    lisongeee
        1
    lisongeee  
       14 天前
    好奇你的 GitHub 提交记录 https://github.com/xjq7/changelog/commits/main/

    类似 xiajieqiong 这种无法点击头像的不能溯源的用户的怎么实现的?
    liutao1998
        2
    liutao1998  
    OP
       14 天前
    @lisongeee email 有问题,是用的公司 email ,就这样了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   843 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 20:53 · PVG 04:53 · LAX 13:53 · JFK 16:53
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.