Search code examples
node.jsgitpre-commit

git pre-commit | file is not committed


I'm using this package to execute a node script on pre-commit hook.

package.json:

{
  "name": "MyApp",
  "version": "0.0.0",
  "description": "ERROR: No README.md file found!",
  "main": "index.js",
  "scripts": {
    "bump": "node ./bump.js",
  },
  "pre-commit": [
    "bump"
  ],
  "devDependencies": {
    "pre-commit": "^1.2.2"
  }
}

bump.js:

#!/usr/bin/env node
'use strict';

const fs = require( 'fs' )
const fileName = __dirname + '/../src/buildNumber.json'
const file = require( fileName )

const date = new Date()

// set current datetime
file.dateTime = date.toLocaleDateString() + ' ' + date.toLocaleTimeString()

// increment build number
file.buildNumber += 1

fs.writeFile( fileName, JSON.stringify( file, null, 2 ), function ( err ) {
    if ( err ) return console.log( err )
    console.log( JSON.stringify( file ) )
    console.log( 'writing to ' + fileName )
} )

The problem I have is that the changes to the file are not committed. What am I doing wrong?


Solution

  • You need to add the changes to git staging area (with git add) so they will be a part of the next commit

    "scripts": {
        "bump": "node ./bump.js && git add ../src/buildNumber.json",
     },