← Runnable Projects

阅读中文版本

Semver Bump CLI

A Node CLI that bumps a semver string by major, minor, or patch according to semver rules, resetting lower components and dropping prerelease.

beginnerjavascriptunverified

Project

node-semver-bump-cli

Entry point: bump.mjs

Install

node --version

Run

node bump.mjs minor 1.4.9

Test

node --test bump.test.mjs

Expected output

Prints the bumped version, for example 1.5.0 for a minor bump of 1.4.9, with lower components reset to zero.

Dependencies

  • node >=20

What it does

Parses a semver into major, minor, patch, and prerelease, applies the requested bump with the correct resets, and drops any prerelease or build metadata because the result is a release version.

const RE = /^(\d+)\.(\d+)\.(\d+)(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/;

export function bump(release, type) {
  const m = String(release).trim().match(RE);
  if (!m) throw new SyntaxError('invalid semver: ' + release);
  let [major, minor, patch] = [Number(m[1]), Number(m[2]), Number(m[3])];
  if (type === 'major') { major += 1; minor = 0; patch = 0; }
  else if (type === 'minor') { minor += 1; patch = 0; }
  else if (type === 'patch') { patch += 1; }
  else throw new RangeError('type must be major, minor, or patch');
  return major + '.' + minor + '.' + patch;
}

const [type, version] = process.argv.slice(2);
console.log(bump(version, type));

Run and verify

node bump.mjs minor 1.4.9

Run the test command to check the implementation: node --test bump.test.mjs.

Expected Output

A minor bump of 1.4.9 resets patch to zero and increments minor:

1.5.0

Limitations

Produces release versions only; it does not create prerelease identifiers or preserve build metadata, which callers must add themselves if needed.