electron/script/release/version-bumper.js

100 lines
2.7 KiB
JavaScript
Raw Normal View History

#!/usr/bin/env node
2020-03-20 20:28:31 +00:00
const semver = require('semver');
const minimist = require('minimist');
const { getElectronVersion } = require('../lib/get-version');
2020-03-20 20:28:31 +00:00
const versionUtils = require('./version-utils');
function parseCommandLine () {
2020-03-20 20:28:31 +00:00
let help;
const opts = minimist(process.argv.slice(2), {
string: ['bump', 'version'],
boolean: ['dryRun', 'help'],
alias: { version: ['v'] },
unknown: () => { help = true; }
2020-03-20 20:28:31 +00:00
});
if (help || opts.help || !opts.bump) {
console.log(`
Bump release version number. Possible arguments:\n
--bump=patch to increment patch version\n
--version={version} to set version number directly\n
--dryRun to print the next version without updating files
Note that you can use both --bump and --stable simultaneously.
2020-03-20 20:28:31 +00:00
`);
process.exit(0);
}
2020-03-20 20:28:31 +00:00
return opts;
}
// run the script
async function main () {
2020-03-20 20:28:31 +00:00
const opts = parseCommandLine();
const currentVersion = getElectronVersion();
2020-03-20 20:28:31 +00:00
const version = await nextVersion(opts.bump, currentVersion);
// print would-be new version and exit early
if (opts.dryRun) {
2020-03-20 20:28:31 +00:00
console.log(`new version number would be: ${version}\n`);
return 0;
}
2020-03-20 20:28:31 +00:00
console.log(`Bumped to version: ${version}`);
}
// get next version for release based on [nightly, alpha, beta, stable]
async function nextVersion (bumpType, version) {
if (
versionUtils.isNightly(version) ||
versionUtils.isAlpha(version) ||
versionUtils.isBeta(version)
) {
switch (bumpType) {
case 'nightly':
2020-03-20 20:28:31 +00:00
version = await versionUtils.nextNightly(version);
break;
case 'alpha':
version = await versionUtils.nextAlpha(version);
break;
case 'beta':
2020-03-20 20:28:31 +00:00
version = await versionUtils.nextBeta(version);
break;
case 'stable':
2020-03-20 20:28:31 +00:00
version = semver.valid(semver.coerce(version));
break;
default:
2020-03-20 20:28:31 +00:00
throw new Error('Invalid bump type.');
}
} else if (versionUtils.isStable(version)) {
switch (bumpType) {
case 'nightly':
2020-03-20 20:28:31 +00:00
version = versionUtils.nextNightly(version);
break;
case 'alpha':
throw new Error('Cannot bump to alpha from stable.');
case 'beta':
2020-03-20 20:28:31 +00:00
throw new Error('Cannot bump to beta from stable.');
case 'minor':
2020-03-20 20:28:31 +00:00
version = semver.inc(version, 'minor');
break;
case 'stable':
2020-03-20 20:28:31 +00:00
version = semver.inc(version, 'patch');
break;
default:
2020-03-20 20:28:31 +00:00
throw new Error('Invalid bump type.');
}
} else {
2020-03-20 20:28:31 +00:00
throw new Error(`Invalid current version: ${version}`);
}
2020-03-20 20:28:31 +00:00
return version;
}
if (require.main === module) {
main().catch((error) => {
2020-03-20 20:28:31 +00:00
console.error(error);
process.exit(1);
});
}
module.exports = { nextVersion };