Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write Git hash to (node's) package.json?

is there a way to automatically update version number of a package.json (https://npmjs.org/doc/json.html) with the HEAD hash of git? I would like to have something like version: 1.0.0+rev82e4b91cfe42cd86e9453b4987b9cc446566de6 inside the project's package.json file. Eveything before the plus is set manually and the hash is updated everytime I commit something.

Is this possible? I couldn't find anything on this topic :-/

like image 363
Sebastian Sebald Avatar asked Apr 08 '13 14:04

Sebastian Sebald


People also ask

How do I add a Git repository to package json?

How do I add a Git repo to a NPM package? To npm install a public project that is hosted on Github, and not the NPM registry, add the Github repo to package. json dependencies using the username/repo#branch-name format. Run npm install and npm will download the project and save it into your /node_modules/ folder.

How is Git hash determined?

Every time a commit is added to a git repository, a hash string which identifies this commit is generated. This hash is computed with the SHA-1 algorithm and is 160 bits (20 bytes) long. Expressed in hexadecimal notation, such hashes are 40 digit strings.

Should package json be Git?

json should not be in the . gitignore file. The main purpose of package-lock. json is to guarantee that all contributors install exactly the same dependencies.

How many digits is a Git hash?

A commit in git always has a hash that contains 40 characters.


1 Answers

It appears that this can be done directly without a plugin. npm version $(git describe) will take the value from git describe and update the value of the version in package.json.

(I'm currently using npm version 3.10.)

like image 139
eflat Avatar answered Sep 21 '22 04:09

eflat