Why I am unable to trigger the custom version bump script upon merging in master?

42 views Asked by At

I have the following package.json:

{
  "name": "@ellakcy/fa-checkbox",
  "author": "Cypriot Free Software Foundation",
  "license": "MIT",
  "version": "0.0.11",
  "description": "CSS and SASS lib that shows a shows beautifull checkboxed using fontawesome",
  "main": "index.html",
  "keywords": [
    "css",
    "checkbox",
    "fontawesome"
  ],
  "scripts": {
    "start": "npm-run-all --parallel watch-css start-server",
    "watch-css": "sass --watch scss:dev",
    "start-server": "live-server",
    "sass": "sass scss:dist",
    "prepare": "husky install",
    "version": "echo $npm_package_version",
    "bump-version": "node scripts/bump-version.mjs",
    "postinstall": "husky install"
  },
  "husky": {
    "hooks": {
      "prepare-commit-msg": "npm run bump-version && git add package.json && git add package-lock.json"
    }
  },
  "devDependencies": {
    "husky": "^8.0.3",
    "inquirer": "^9.2.13",
    "live-server": "^1.2.2",
    "npm-run-all": "^4.1.5",
    "sass": "^1.69.7"
  }
}

And I try upon merging in master to run the following script:

import inquirer  from 'inquirer';
import * as fs from 'fs';
import semver from 'semver';


async function getVersionBumpChoices(currentVersion) {

  const versionChoices = ['patch', 'minor', 'major'];

  const messages = {
    'patch': "Patch (Bug fixes)",
    'minor': "Minor (New features)",
    'major': "Major (Breaking changes)" 
  }

  // Generate choice objects with custom messages based on the current version
  const choices = versionChoices.map((bumpType) => {

    const bumpedVersion = semver.inc(currentVersion, bumpType);
    
    return {
      name: `${messages[bumpType]}  -> ${bumpedVersion}`,
      value: bumpedVersion,
    };

  });

  return choices;
}

async function bumpVersion() {
  // Read package.json
  const packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf-8'));

  console.log(`Current version ${packageJson.version}`)

  const choices = await getVersionBumpChoices(packageJson.version);
  // Ask the user for the new version
  const { versionType } = await inquirer.prompt([
    {
      type: 'list',
      name: 'versionType',
      message: 'Select the version bump type:',
      choices: choices,
    },
  ]);

  // Bump the version
  packageJson.version = versionType;

  // Write the updated package.json
  fs.writeFileSync('./package.json', JSON.stringify(packageJson, null, 2));

  console.log(`Version bumped to ${versionType}`);
}

bumpVersion();

That I tun it as:

npm run bump-version

I want once I run:

git checkout master
git merge dev

To automatically run the script above. But as you can see here I fail to do so.

Also, I found out that husky does not install a post-merge script:

ls -l .git/hooks/
σύνολο 60
-rwxrwxr-x 1 pcmagas pcmagas  478 Ιαν  16 21:48 applypatch-msg.sample
-rwxrwxr-x 1 pcmagas pcmagas  896 Ιαν  16 21:48 commit-msg.sample
-rwxrwxr-x 1 pcmagas pcmagas 4655 Ιαν  16 21:48 fsmonitor-watchman.sample
-rwxrwxr-x 1 pcmagas pcmagas  189 Ιαν  16 21:48 post-update.sample
-rwxrwxr-x 1 pcmagas pcmagas  424 Ιαν  16 21:48 pre-applypatch.sample
-rwxrwxr-x 1 pcmagas pcmagas 1643 Ιαν  16 21:48 pre-commit.sample
-rwxrwxr-x 1 pcmagas pcmagas  416 Ιαν  16 21:48 pre-merge-commit.sample
-rwxrwxr-x 1 pcmagas pcmagas 1492 Ιαν  16 21:48 prepare-commit-msg.sample
-rwxrwxr-x 1 pcmagas pcmagas 1374 Ιαν  16 21:48 pre-push.sample
-rwxrwxr-x 1 pcmagas pcmagas 4898 Ιαν  16 21:48 pre-rebase.sample
-rwxrwxr-x 1 pcmagas pcmagas  544 Ιαν  16 21:48 pre-receive.sample
-rwxrwxr-x 1 pcmagas pcmagas 2783 Ιαν  16 21:48 push-to-checkout.sample
-rwxrwxr-x 1 pcmagas pcmagas 3650 Ιαν  16 21:48 update.sample

I also tried this:

$ npx husky install
husky - Git hooks installed
pcmagas@pcmagas-System-Product-Name:~/Kwdikas/HTML5/fa-checkbox$ git commit -am "Remove spaces" README.md 
fatal: paths 'README.md ...' with -a does not make sense
pcmagas@pcmagas-System-Product-Name:~/Kwdikas/HTML5/fa-checkbox$ git commit -m "Remove spaces" README.md 
[dev 1db5fe2] Remove spaces
 1 file changed, 2 deletions(-)
pcmagas@pcmagas-System-Product-Name:~/Kwdikas/HTML5/fa-checkbox$ git checkout master 
Αλλαγή στον κλάδο 'master'
pcmagas@pcmagas-System-Product-Name:~/Kwdikas/HTML5/fa-checkbox$ git merge dev 
Updating a8d7a6c..1db5fe2
Fast-forward
 README.md | 2 --
 1 file changed, 2 deletions(-)

And I changed the hook as:

    "hooks": {
      "pre-merge-commit": "npm run bump-version"
    }

And still the script is not fired.

1

There are 1 answers

0
Dimitrios Desyllas On

In my case I needed to completely reinstall husky and let it reset.

In order to do so run:

Step 1: Nuke it from orbit

npx husky uninstall
# Backup hook scripts elsewhere first
rm -rf .husky

The upon package.json remove the dependency turning:

"devDependencies": {
    "husky": "^8.0.3",
    "inquirer": "^9.2.13",
    "live-server": "^1.2.2",
    "npm-run-all": "^4.1.5",
    "sass": "^1.69.7"
  }

Into this:

"devDependencies": {
    "inquirer": "^9.2.13",
    "live-server": "^1.2.2",
    "npm-run-all": "^4.1.5",
    "sass": "^1.69.7"
  }

And running:

npm install

Step 2 Reinstall

Follow the steps mentioned: https://typicode.github.io/husky/get-started.html For the record I tried:

npm install --save-dev husky
npx husky init

And I installed the newer version currently in my case is the 9.0.10.

I commited the .husky folder

git add .husky
git commit

Step 3 Enable post merge

At .husky folder create a post-merge script containing the following:

npm run bump-version

An easy command line oneliner in bash is:

echo "npm run bump-version" > .husky/post-merge

Step 4 Test it

git checkout dev
git add .husky/post-merge 
git commit

git checkout master
git merge dev

This will run the script, but I had issues with interactive ones sunch the one in your case. For some reason it hangs unexpectedly.


NOTE

If upon commit you see the following error:

npm ERR! Missing script: "test"
npm ERR! 
npm ERR! To see a list of scripts, run:
npm ERR!   npm run

Then place in your package.json at scripts section the following:

"scripts": {
    "test": "exit 0",
    // Rest of scripts here
}

In my case the scripts section is:

  "scripts": {
    "test": "exit 0",
    "start": "npm-run-all --parallel watch-css start-server",
    "watch-css": "sass --watch scss:dev",
    "start-server": "live-server",
    "sass": "sass scss:dist",
    "version": "echo $npm_package_version",
    "bump-version": "node scripts/bump-version.mjs",
    "prepare": "husky"
  },