diff --git a/__tests__/commands/version.js b/__tests__/commands/version.js index 2ee3062e37..3877c8d205 100644 --- a/__tests__/commands/version.js +++ b/__tests__/commands/version.js @@ -133,3 +133,24 @@ test('run version and make sure commit hooks are disabled by config', async (): expect(spawn.mock.calls[2][0]).toEqual(gitArgs); }); }); + +test('run version with --major flag and make sure major version is incremented', (): Promise => { + return runRun([], {gitTagVersion, major: true}, 'no-args', async (config): ?Promise => { + const pkg = await fs.readJson(path.join(config.cwd, 'package.json')); + expect(pkg.version).toEqual('2.0.0'); + }); +}); + +test('run version with --minor flag and make sure minor version is incremented', (): Promise => { + return runRun([], {gitTagVersion, minor: true}, 'no-args', async (config): ?Promise => { + const pkg = await fs.readJson(path.join(config.cwd, 'package.json')); + expect(pkg.version).toEqual('1.1.0'); + }); +}); + +test('run version with --patch flag and make sure patch version is incremented', (): Promise => { + return runRun([], {gitTagVersion, patch: true}, 'no-args', async (config): ?Promise => { + const pkg = await fs.readJson(path.join(config.cwd, 'package.json')); + expect(pkg.version).toEqual('1.0.1'); + }); +}); diff --git a/src/cli/commands/version.js b/src/cli/commands/version.js index 577e0a2644..f77c724fd9 100644 --- a/src/cli/commands/version.js +++ b/src/cli/commands/version.js @@ -21,6 +21,9 @@ function isValidNewVersion(oldVersion: string, newVersion: string, looseSemver: export function setFlags(commander: Object) { commander.description('Update the version of your package via the command line.'); commander.option(NEW_VERSION_FLAG, 'new version'); + commander.option('--major', 'auto-increment major version number'); + commander.option('--minor', 'auto-increment minor version number'); + commander.option('--patch', 'auto-increment patch version number'); commander.option('--message [message]', 'message'); commander.option('--no-git-tag-version', 'no git tag version'); commander.option('--no-commit-hooks', 'bypass git hooks when committing new version'); @@ -77,6 +80,17 @@ export async function setVersion( throw new MessageError(reporter.lang('invalidVersion')); } + // get new version by bumping old version, if requested + if (!newVersion) { + if (flags.major) { + newVersion = semver.inc(oldVersion, 'major'); + } else if (flags.minor) { + newVersion = semver.inc(oldVersion, 'minor'); + } else if (flags.patch) { + newVersion = semver.inc(oldVersion, 'patch'); + } + } + // wasn't passed a version arg so ask interactively while (!newVersion) { // make sure we're not running in non-interactive mode before asking for new version