[Tool] Automatically update package.json version at each commit
Telokis last edited by
Hey everyone !
I don't like having to manually update the
package.jsonfile's patch version (the 'x' in '1.0.x').
Because of that, I decided to take advantage of git hooks and wrote some scripts to make my life easier.
You can find the scripts here but I'll make a dedicated repo if this seem useful to people.
Here is how it works:
- I assume everyone is setting up a tag for major/minor version changes ('x.x.0')
- I assume the
scriptsfolder is at the root of the repository (to generate the hooks)
Then, after cloning a repo, I do this:
This will generate a post and a pre commit hook.
The pre-commit hook is used to automatically create a new tag if major or minor version has changed. Inside
The post-commit hook is used to automatically update the package.json after each commit. It increases the patch version by one (each commit is considered a patch) by counting the number of commits since the last tag (or the beginning if no tags made)
The post-commit hook (file
setup_readme_version.sh) also updates the
readme.mdof my project to write
_Current repository version : v<major>.<minor>.<patch>_
If you don't want this behavior, you don't have anything to do. If you want it, you have to put
_Current repository version : v_somewhere in your readme. The regexp will match and update it.
And, if you want to bump minor or major version, simply do it in your
package.jsonand the hooks will properly read and process it. Even updating the readme. And a new tag will be created.
Let me know what you think about this stuff !
npm version patch/minor/majorupdates
package.jsonright? personally that works for me.
Your setup looks more complicated. How does your script handle if I want to create a minor/major version?
Telokis last edited by
And, if you want to bump minor or major version, simply do it in your package.json and the hooks will properly read and process it. Even updating the readme. And a new tag will be created.
I didn't know about npm version but this is one step too much for me. I want to update the patch version at each comimt.
This may not be optimal for js because it was originally meant to be a C++ thing. I just ported it to js.