Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git hook update package json version

In our project we often forget to update version numbers in Package.json file. Ours is a AngularJS project. In our package JSON file we are specifying the below two version information

"version": "1.0.7",
"devVersion": "1.0.4"

Before Merging a branch to develop I want a automated script to update these above two version numbers. I am thinking Git Hooks will help me.

Where can i find the hooks, I am able to see the hooks in my local repo under .git folder. I am confused which hook to use. Searching on Google suggests I have to create hooks on server.

Where can i find them and can i update the above both keys (version and devVersion) ?

Pls suggest the location and hook to use, this will solve a lot of problem.

like image 232
user804401 Avatar asked Mar 25 '16 05:03

user804401


3 Answers

I am using husky and git-branch-is:

"scripts": {
  ...
  "postmerge": "(git-branch-is master && npm version minor || 
  (git-branch-is dev && npm --no-git-tag-version version patch)",
  ...
},

Read more about npm version

Webpack or Vue.js

If you are using webpack or Vue.js, you can display this in the UI using Auto inject version - Webpack plugin

NUXT

In nuxt.config.js:

var WebpackAutoInject = require('webpack-auto-inject-version');

module.exports = {
  build: {
    plugins: [
      new WebpackAutoInject({
        // options
        // example:
        components: {
          InjectAsComment: false
        },
      }),
    ]
  },
}

Inside your template for example in the footer:

<p> All rights reserved © 2018 [v[AIV]{version}[/AIV]]</p>
like image 67
Anima-t3d Avatar answered Nov 03 '22 01:11

Anima-t3d


You have two kinds of hooks (both present in any .git/hooks folder): server and client hooks.
They are listed in "Customizing Git - Git Hooks"

A merge is a local operation, so if you wanted to automate any process during a merge, you would need a client hook, like a post-commit hook (meaning executed just after creating a merge commit).
If you need to update that file before a merge, you can try a pre-commit hook, and check if a merge is in progress (if not, your pre-commit hook would do nothing since you want to update the versions only before a merge).

You can see in this answer an example of a post-commit hook which generates a version.json file.
If is written in node, but you can write a hook ni any scripting language you want.

like image 25
VonC Avatar answered Nov 03 '22 02:11

VonC


With Husky, it's extremely simple:

{
  "name": "demo-project",
  "version": "0.0.3",
  "husky": {
    "hooks": {
      "pre-commit": "npm --no-git-tag-version version patch && git add ."
    }
  }
}

Note: I put git add . in the end because after we update package version, we need to stage it

like image 4
Duc Trung Mai Avatar answered Nov 03 '22 01:11

Duc Trung Mai