Reputation: 16074
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 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.
I have setup Husky and commitlint in order to validate conventional commit message and it works fine.
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?
Upvotes: 4
Views: 8523
Reputation: 1172
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.
Upvotes: 3
Reputation: 24590
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
Upvotes: 2
Reputation: 1313
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();
Upvotes: -2