Skip to content

Instantly share code, notes, and snippets.

@kbkk
Created March 12, 2020 13:52
Show Gist options
  • Save kbkk/40427b8834dad774017cdbe35bae2ccf to your computer and use it in GitHub Desktop.
Save kbkk/40427b8834dad774017cdbe35bae2ccf to your computer and use it in GitHub Desktop.
Bump package.json major version
const fs = require( 'fs' );
const semver = require( 'semver' );
const cwd = process.cwd();
const packageJsonPath = cwd + '/package.json';
const content = fs.readFileSync( packageJsonPath, 'utf-8' );
const [ fullMatch, currentVersion ] = content.match( /"version": "(.*)?"/ ) || [];
let newVersion;
if ( currentVersion.startsWith( '0' ) ) {
newVersion = semver.inc( currentVersion, 'minor' );
} else {
newVersion = semver.inc( currentVersion, 'major' );
}
const newContent = content.replace( fullMatch, `"version": "${ newVersion }"` );
fs.writeFileSync( packageJsonPath, newContent );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment