使用预提交挂钩更新 package.json 版本

发布于 2025-01-09 21:07:27 字数 535 浏览 1 评论 0原文

我正在尝试自动化托管在私人 github 存储库上的 Node.js 项目的版本更新。

该项目旨在本地运行,因此不会发布,也不会发布。我组织中的人员只需拉出 main 分支并使用 yarn && 在自己的计算机上运行它即可。纱线开始

我想要实现的目标

我想要实现的是,在预提交阶段,对该项目的 package.json 进行版本提升(主要、补丁或次要),并与代码根据提交消息进行更改。

我想要做的就是我的 PR 将更改包含在 package.json 中,而无需我手动执行。我不需要为此发布版本或 CI。

我成功地

设置了 Husky 和 ​​commitlint 来验证传统的提交消息,并且它工作正常。

我尝试过,但失败了,

我尝试使用semantic-release和其他包来提供此功能,但它们都暗示在某个地方有 CI 构建或发布,所以我陷入了困境。

有什么想法吗?

I'm trying to automate the version bump of a Node.js project hosted on a private github repo.

The project is meant to run locally, so it's not published, neither released. People in my organization just pull the main branch and run it on their machines with yarn && yarn start.

What I want to achieve

What I want to achieve is that in the pre-commit phase a version bump is made (major, patch or minor) to the package.json of this project and committed together with the code changed according to the commit message.

All I want to do is that my PR is including the change in the package.json without me having to do it manually. I don't need releases or CI for this.

What I did successfully

I have setup Husky and commitlint in order to validate conventional commit message and it works fine.

What I tried, but failed

I tried to use semantic-release and other packages to provide this functionality, but they all imply there's a CI build or release somewhere so I'm stuck.

Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风吹过旳痕迹 2025-01-16 21:07:28

您应该使用 commit-msg 挂钩,而不是 pre-commit
pre-commit 钩子在有提交消息之前触发,但如果使用 commit-msg 钩子,它将在有提交消息后触发。
该钩子将触发您的脚本,并且您的脚本将收到的第一个参数将是提交消息本身。

然后您可以在提交消息之上编写任何逻辑。您可以查看该消息,并根据您的请求根据消息更新版本。有很多方法可以实现

npm version major

Instead of pre-commit you should use the commit-msg hook.
The pre-commit hook is triggered before there is a commit message yet, but if you use the commit-msg hook, it will be triggered after there is a commit message.
The hook will trigered your script and the first argument your script will receive will be the commit message itself.

Then you can write any logic on top of the commit message. You can check the message, and update version based of the message as you requested. There are many ways to it

npm version major
你是暖光i 2025-01-16 21:07:28

您可以使用 updateCommitId 模块编写自定义节点 JS 代码以在预提交挂钩期间运行。示例工作代码如下所示。请根据需要升级。

{
  "version": "1.12.0",
  "dependencies": {},
  "pre-commit": ["updateCommitId"],
  "scripts": {
    "updateCommitId": "node updateVersion.js"
  },
  "devDependencies": {
    "pre-commit": "^1.2.2",
    "semver": "^7.3.5",
    "simple-git": "^3.2.6"
  }
}

const semver = require("semver");
const simpleGit = require("simple-git");

const options = {
  baseDir: process.cwd(),
  binary: "git",
  maxConcurrentProcesses: 6,
};

const git = simpleGit(options);

const json = require("./package.json");

function savePackage() {
  require("fs").writeFileSync(
    process.cwd() + "/package.json",
    JSON.stringify(json, null, 2)
  );
}
async function updateVersion() {
  try {
    if (json.version) {
      const version = semver.parse(json.version);
      version.inc("minor");
      json.version = version.toString();
      savePackage();
      await git.add("package.json");
      process.exit(0);
    }
  } catch (e) {
    console.log(e);
    process.exit(1);
  }
}

updateVersion();

you can user updateCommitId modules to write custom node JS code to run during pre-commit hook. Sample working code can look like below. Please upgrade as deemed required.

{
  "version": "1.12.0",
  "dependencies": {},
  "pre-commit": ["updateCommitId"],
  "scripts": {
    "updateCommitId": "node updateVersion.js"
  },
  "devDependencies": {
    "pre-commit": "^1.2.2",
    "semver": "^7.3.5",
    "simple-git": "^3.2.6"
  }
}

const semver = require("semver");
const simpleGit = require("simple-git");

const options = {
  baseDir: process.cwd(),
  binary: "git",
  maxConcurrentProcesses: 6,
};

const git = simpleGit(options);

const json = require("./package.json");

function savePackage() {
  require("fs").writeFileSync(
    process.cwd() + "/package.json",
    JSON.stringify(json, null, 2)
  );
}
async function updateVersion() {
  try {
    if (json.version) {
      const version = semver.parse(json.version);
      version.inc("minor");
      json.version = version.toString();
      savePackage();
      await git.add("package.json");
      process.exit(0);
    }
  } catch (e) {
    console.log(e);
    process.exit(1);
  }
}

updateVersion();

笨笨の傻瓜 2025-01-16 21:07:27

如果您有任何类型的 CICD 管道(不完全限于 git hooks 范围),您可以使用 gitversion cli

它支持每个提交消息正则表达式的版本碰撞 配置示例

major-version-bump-message: '\+semver:\s?(breaking|major)'
minor-version-bump-message: '\+semver:\s?(feature|minor)'
patch-version-bump-message: '\+semver:\s?(fix|patch)'
no-bump-message: '\+semver:\s?(none|skip)'

意味着提交信息我的提交消息+semver:修复将触发补丁版本碰撞,

请参阅本文 包含详细的设置说明。

in case you have any kind of CICD pipeline (not entirely limited to git hooks scope) you can use gitversion cli

it supports version bump per commit message regex configuration example:

major-version-bump-message: '\+semver:\s?(breaking|major)'
minor-version-bump-message: '\+semver:\s?(feature|minor)'
patch-version-bump-message: '\+semver:\s?(fix|patch)'
no-bump-message: '\+semver:\s?(none|skip)'

means that a commit with message my commit msg +semver: fix will trigger a patch version bump

see this article with detailed explanation for setup.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文