How to use the inc function from semver

Find comprehensive JavaScript semver.inc code examples handpicked from public code repositorys.

semver.inc is a function in the SemVer library that increments a given version string according to a specified release type.

146
147
148
149
150
151
152
153
154
155

/**
 * Get the next version number, given the current level.
 */
async nextVersion() {
  return semver.inc(await this.lastVersion(), this.level());
}

/**
 * Get the formatted list of snippets as a string
fork icon238
star icon323
watch icon19

218
219
220
221
222
223
224
225
226
227
228
 * @param {string} version The version to increment
 * @param {int} releaseWeight The release weight to apply
 * @returns {string} The incremented version
 */
function getNextVersion(version, releaseWeight) {
  return semver.inc(version, RELEASE_TYPE[releaseWeight].toLocaleLowerCase())
}


main(process.argv.slice(2), process.argv[1]).catch(error => {
  console.error(error)
fork icon202
star icon563
watch icon40

+ 2 other calls in file

How does semver.inc work?

semver.inc is a function in the SemVer library that increments a given version string according to a specified release type. When you call semver.inc, you pass in the version string that you want to increment, along with a release type. The release type can be one of major, minor, patch, premajor, preminor, prepatch, or prerelease. The major, minor, and patch release types increment the corresponding part of the version string. For example, if you have a version string of 1.2.3 and you call semver.inc('1.2.3', 'patch'), the function will return the string 1.2.4. The premajor, preminor, and prepatch release types are used for pre-release versions, and they work the same way as their non-prerelease counterparts, except that they also reset the pre-release identifier to zero. The prerelease release type increments the pre-release identifier, or adds a new pre-release identifier if none exists. Overall, semver.inc is a powerful function that allows you to easily increment version strings in a standardized and predictable way. It is particularly useful in build systems and release pipelines, where you need to manage version numbers for multiple artifacts and releases.

92
93
94
95
96
97
98
99
100
101
const release = await bumpedVersionType();
core.info(`${release.reason}, therefore release type should be ${release.releaseType}`);

const newVersion =
    semver.valid(release.releaseType, undefined) ||
    semver.inc(currentVersion, release.releaseType, prereleaseRequested, 'rc');
core.info(`new version is ${newVersion}`);
if (writeFile) {
    packageJson.version = newVersion;
    fs.writeFileSync('./package.json', JSON.stringify(packageJson, null, 2));
fork icon110
star icon225
watch icon26

+ 3 other calls in file

187
188
189
190
191
192
193
194
195
196
}

if (bumpType === 'dev') {
    newVersion = currentVersion + config.devSuffix;
} else {
    newVersion = semver.inc(currentVersion, bumpType);
}
data.version = newVersion;

var content = JSON.stringify(data, null, config.indentation);
fork icon12
star icon22
watch icon0

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
const semver = require("semver");

console.log(semver.inc("1.2.3", "patch")); // Output: 1.2.4
console.log(semver.inc("1.2.3", "minor")); // Output: 1.3.0
console.log(semver.inc("1.2.3", "major")); // Output: 2.0.0

console.log(semver.inc("1.2.3-alpha.0", "prerelease")); // Output: 1.2.3-alpha.1
console.log(semver.inc("1.2.3-alpha.1", "prerelease")); // Output: 1.2.3-alpha.2

console.log(semver.inc("1.2.3-alpha.1", "patch")); // Output: 1.2.3
console.log(semver.inc("1.2.3-alpha.1", "prepatch")); // Output: 1.2.4-0
console.log(semver.inc("1.2.3-alpha.1", "preminor")); // Output: 1.3.0-0
console.log(semver.inc("1.2.3-alpha.1", "premajor")); // Output: 2.0.0-0

In this example, we first import the semver library using require. We then call semver.inc several times with different inputs and release types. For example, semver.inc('1.2.3', 'patch') increments the patch version of the string 1.2.3, resulting in the string 1.2.4. Similarly, semver.inc('1.2.3-alpha.1', 'prerelease') increments the pre-release identifier of the string 1.2.3-alpha.1, resulting in the string 1.2.3-alpha.2. Overall, these examples demonstrate how semver.inc can be used to increment version strings in a standardized and predictable way, and how it can help you to manage version numbers in your software projects.

904
905
906
907
908
909
910
911
912
913
case semver.valid(version):
  manifest.version = version;
  break;

case ['minor', 'major', 'patch'].includes(version):
  manifest.version = semver.inc(manifest.version, version);
  break;

default:
  throw new Error('Invalid version. Must be either patch, minor or major.');
fork icon4
star icon5
watch icon4

+ 3 other calls in file

178
179
180
181
182
183
184
185
186
187
188
189
190
function getNewVersion(oldVersion, input) {
  if (!isValidVersionInput(input)) {
    throw new Error(`Version should be either ${SEMVER_INCREMENTS.join(', ')} or a valid semver version.`);
  }


  return SEMVER_INCREMENTS.indexOf(input) === -1 ? input : semver.inc(oldVersion, input);
};




function prettyVersionDiff(oldVersion, inc) {
fork icon0
star icon1
watch icon1

+ 2 other calls in file

231
232
233
234
235
236
237
238
239
240
});

grunt.registerTask('release', '#shipit', function(version) {
  var curVersion = grunt.config.get('version');

  version = semver.inc(curVersion, version) || version;

  if (!semver.valid(version) || semver.lte(version, curVersion)) {
    grunt.fatal('hey dummy, that version is no good!');
  }
fork icon0
star icon1
watch icon0

70
71
72
73
74
75
76
77
78
79
    newVersion = null;
    reporter.error(reporter.lang('invalidSemver'));
  }
}
if (newVersion) {
  newVersion = semver.inc(oldVersion, newVersion, config.looseSemver) || newVersion;
}
invariant(newVersion, 'expected new version');

if (newVersion === pkg.version) {
fork icon0
star icon0
watch icon1

+ 7 other calls in file

321
322
323
324
325
326
327
328
329
330
  }
} else if (type === 'patch') {
  if (preMode) {
    throw new Error(`Unexpected pre mode ${preMode} on current branch`);
  }
  nextVersion = semver.inc(currentVersion, 'patch');
}

await fs.writeJson(
  path.join(repo.root.dir, 'package.json'),
fork icon0
star icon0
watch icon1

+ 9 other calls in file

332
333
334
335
336
337
338
339
340
341
function getPackageVersion(filename) {
  return JSON.parse(fs.readFileSync(filename, {encoding: 'utf8'})).version;
}

function bump(type) {
  pkg.version = semver.inc(pkg.version, type);
  fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2));
  const filename = 'bower.json';
  const p = JSON.parse(fs.readFileSync(filename, {encoding: 'utf8'}));
  p.version = pkg.version;
fork icon0
star icon0
watch icon1

12
13
14
15
16
17
18
19
20
21
22
23
if (tsVersion && semver.gt(tsVersion, masterVersion)) {
	version = tsVersion;
}


// increment the patch
version = semver.inc(version, "patch");


// write it to the package.json
const packageFile = resolve(__dirname, "../package.json");
const packageObj = JSON.parse(fs.readFileSync(packageFile, "utf-8"));
fork icon0
star icon0
watch icon1

199
200
201
202
203
204
205
206
207
208
    { title: 'major', value: 'major' },
  ],
  initial: 0,
})
  .then(({ semverIncrement }) => {
    const increasedVersion = semver.inc(version, semverIncrement)
    const cmdNpm = spawn('npm', ['version', increasedVersion])

    cmdNpm.stdout.on('data', (data) => console.log(data.toString()))
    cmdNpm.stderr.on('data', (data) => console.warn(data.toString()))
fork icon0
star icon0
watch icon1

12
13
14
15
16
17
18
19
20
21
22
23
const semver = require('semver');
const path = require('path');


const NEW_VERSION_FLAG = '--new-version [version]';
function isValidNewVersion(oldVersion: string, newVersion: string, looseSemver: boolean, identifier?: string): boolean {
  return !!(semver.valid(newVersion, looseSemver) || semver.inc(oldVersion, newVersion, looseSemver, identifier));
}


export function setFlags(commander: Object) {
  commander.description('Update the version of your package via the command line.');
fork icon0
star icon0
watch icon1

+ 8 other calls in file

44
45
46
47
48
49
50
51
52
53

  if (args[0] === 'from-git') {
    retrieveTagVersion(silent, data, cb_)
  } else {
    var newVersion = semver.valid(args[0])
    if (!newVersion) newVersion = semver.inc(data.version, args[0], npm.config.get('preid'))
    if (!newVersion) return cb_(version.usage)
    persistVersion(newVersion, silent, data, cb_)
  }
})
fork icon0
star icon0
watch icon0

14
15
16
17
18
19
20
21
22
23
  minor,
  patch,
} = semver.parse(version);

if (patch < maxVersion) {
  manifest.version = semver.inc(version, 'patch');
} else if (minor < maxVersion) {
  manifest.version = semver.inc(version, 'minor');
} else {
  manifest.version = semver.inc(version, 'major');
fork icon0
star icon0
watch icon0

+ 5 other calls in file

23
24
25
26
27
28
29
30
31
32
33
34
const targetPackage = require(targetPackagePath);


// Use version least likely to conflict with what's been
// published to npm. (Maps to `lerna version` command
// in e2e.npm.publish.sh)
const version = semver.inc(web3Package.version, 'minor');


const web3Modules = [
  "web3",
  "web3-bzz",
fork icon0
star icon0
watch icon0

337
338
339
340
341
342
343
344
345
346
// number to the correct status. If it's a new package, then ensure
// the package.json version is correct according to the status.
if (this.existingPackages.has(name)) {
  let newVersion = status === 'released'
    ? semver.inc(pkg.version, bump)
    : semver.inc(pkg.version, 'prerelease', status);
  versions.set(name, [pkg.version, newVersion, pkg.private]);
} else {
  let newVersion = '3.0.0';
  if (status !== 'released') {
fork icon0
star icon0
watch icon0