7

I'm trying to find out how to correctly use npm version with prepatch (also premajor or preminor) / preid options to increment the counter behind the suffix.

e.g.:

  • I have a v.0.5.22 and want to append -rc
  • I used the command npm version prepatch --preid rc
  • then I get v.0.5.23-rc.0, fine so far.
  • but the next time I'm using the same command I end up with v.0.5.24-rc.0,
  • what I want is to get v.0.5.23-rc.1 instead

How can I only increment the counter behind -rc. and keep the patch number? Or am I misunderstanding the purpose of prepatch / preid?

Sascha
  • 858
  • 1
  • 16
  • 30

2 Answers2

8

Alright, I'll answer my own question for future reference.

After having v.0.5.23-rc.0 the command to only bump the number behind the . has to be:

npm version prerelease

Then I would get v.0.5.23-rc.1.

Sascha
  • 858
  • 1
  • 16
  • 30
0

An alternative to npm version would be to write a bash script.

Ended up with this, fetching the version from the package.json

#!/usr/bin/env bash
set -e

packageJsonLocation="../package.json"
current_version=$(grep '"version":' $packageJsonLocation | cut -d\" -f4)
pre=0

if [ -z "${current_version}" ]; then
  echo "No version found in package.json"
  exit 1
fi

if [[ $current_version =~ ^.*-.* ]]; then
  # increment prerelease version
  IFS='-' read -ra array_pre <<< "$current_version"
  IFS='.' read -ra array <<< "${array_pre[0]}"
  pre=$(( ${array_pre[1]} + 1))
else 
  IFS='.' read -ra array <<< "${current_version}"
fi

new_version="${array[0]}.${array[1]}.${array[2]}-${pre}"

echo "Pre version: ${current_version} -> ${new_version}"
# Output
2.9.3   -> 2.9.3-0
and
2.9.3-1 -> 2.9.3-2
julian
  • 76
  • 7
  • 2
    Well, my question was clearly about npm version. Seems like a bit overcomplicated instead of just using the built-in feature. – Sascha Sep 30 '22 at 08:33