Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support semver config option to control how versions are bumped #41

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ package is put there before packaging. The default is `dist/`. The package file
"fileEncoding" : "utf-8", // file encoding when traversing the file system, default is UTF-8
"generatePom" : true, // generate a POM based on the configuration
"pomFile" : "pom.xml", // use this existing pom.xml instead of generating one (generatePom must be false)
"version" : "{version}", // sets the final version of the release
"semver" : "minor", // increases package.json's version. Values here can be any semver.inc release
// type strings, or an array of the release type and prerelease components.
"repositories" : [ // array of repositories, each with id and url to a Maven repository.
{
"id": "example-internal-snapshot",
Expand Down Expand Up @@ -81,6 +84,43 @@ Usage: `maven.deploy( repositoryId, [snapshot = false], [callback])`
maven.config(config);
maven.deploy('example-internal-release', 'file.jar');

## Versioning
By default, an artifact's version is based on the version in package.json (ie. config.version==='{version}'). To control the version in package.json, you can specify the semver release type param and any prerelease params.
All prerelease components in the resulting version will be turned into maven qualifiers and separated by '-' instead of '.'. For example 1.2.3-alpha.3 will be 1.2.3-alpha-3

Note: semver is not configured by default for release artifacts.

### Snapshots

All snapshot builds will have the '-SNAPSHOT' qualifier appended to them.
As well, if a version has prerelease components then the last build number will be dropped in place of the SNAPSHOT qualifier.

Note: semver is set to 'patch' by default for snapshot artifacts.

### Example: bumping the patch version

console.log(packageJson.version); //1.2.3
config.semver = 'patch';
maven.config(config);
maven.deploy('example-internal-release');
//artifact version will be 1.2.4

### Example: bumping the release candidate version

console.log(packageJson.version); //1.2.3-alpha.3
config.semver = ['prerelease', 'alpha'];
maven.config(config);
maven.deploy('example-internal-release');
//artifact version will be 1.2.3-alpha-4

### Example: replacing build numbers with SNAPSHOT

console.log(packageJson.version); //1.2.3-alpha.3
config.semver = ['prerelease', 'alpha'];
maven.config(config);
maven.deploy('example-internal-release', true);
//artifact version will be 1.2.3-alpha-SNAPSHOT

## Contributing

We would love your contribution, please consult the [contributing](CONTRIBUTE.md) page for how to make your contributions land into the project as easily as possible.
44 changes: 42 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const DEFAULT_CONFIG = {
type: 'war',
fileEncoding: 'utf-8',
version: '{version}',
generatePom: true
generatePom: true,
semver: null
};

validateConfig = defineOpts({
Expand Down Expand Up @@ -129,8 +130,47 @@ function getConfig (isSnapshot) {

var pkg = readPackageJSON(configTmpl.fileEncoding);

if (!configTmpl.semver && isSnapshot) {
//for backwards compat inc 'patch' version when making snapshots
configTmpl.semver = 'patch';
}

if (configTmpl.semver) {
if (Array.isArray(configTmpl.semver)) {
//interpret semver config as params to semver.inc
pkg.version = semver.inc.apply(null, [pkg.version].concat(configTmpl.semver));
}
else if (semver.valid(configTmpl.semver)) {
//semver is a valid version to use
pkg.version = configTmpl.semver
}
else {
//interpret semver as the second option to semver.inc
pkg.version = semver.inc(pkg.version, configTmpl.semver);
}
}

//convert prerelease components into proper qualifiers
var components = semver.prerelease(pkg.version);
if (components) {
//check if we should drop the last build number component
if (isSnapshot && isFinite(components[components.length - 1])) {
components.pop();
}

//replace the '.' between prerelease components with '-' to be proper qualifiers
if (components.length > 0) {
pkg.version = [semver.major(pkg.version), semver.minor(pkg.version), semver.patch(pkg.version)].join('.') + '-' + components.join('-');
}
else {
//should only get here if we popped off the last component, so we don't want to put an extra '-'
pkg.version = [semver.major(pkg.version), semver.minor(pkg.version), semver.patch(pkg.version)].join('.')
}
}

//pkg version should be modified above but still need to add snapshot when appropriate
if (isSnapshot) {
pkg.version = semver.inc(pkg.version, 'patch') + '-SNAPSHOT';
pkg.version += '-SNAPSHOT';
}

return filterConfig(configTmpl, pkg);
Expand Down
50 changes: 50 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,56 @@ describe('maven-deploy', function () {
});
});

describe('versioning', function() {
it('semver release type passed in for releases', function () {
const EXPECTED_VERSION = '1.0.1';

maven.config( extend({finalName: '{name}-{version}', semver: 'patch'}, TEST_CONFIG) );
maven.package();

assertWarFileToEqual(TEST_PKG_JSON.name + '-' + EXPECTED_VERSION + '.war');
});

it('semver release type passed in for snapshots', function () {
const EXPECTED_VERSION = '1.1.0-SNAPSHOT';

maven.config( extend({finalName: '{name}-{version}', semver: 'minor'}, TEST_CONFIG) );
maven.package(true);

assertWarFileToEqual(TEST_PKG_JSON.name + '-' + EXPECTED_VERSION + '.war');
});

it('semver prerelease types for releases', function () {
const EXPECTED_VERSION = '1.0.1-beta-0';

maven.config( extend({finalName: '{name}-{version}', semver: ['prerelease', 'beta']}, TEST_CONFIG) );
maven.package();

assertWarFileToEqual(TEST_PKG_JSON.name + '-' + EXPECTED_VERSION + '.war');
});

it('semver prerelease build number qualifiers', function () {
const EXPECTED_VERSION = '1.2.3-6';

npmVersion('1.2.3-5');
maven.config( extend({finalName: '{name}-{version}', semver: ['prerelease']}, TEST_CONFIG) );
maven.package();

assertWarFileToEqual(TEST_PKG_JSON.name + '-' + EXPECTED_VERSION + '.war');
});

it('semver replacing prerelease build numbers with snapshots', function () {
const EXPECTED_VERSION = '1.2.3-alpha-SNAPSHOT';

npmVersion('1.2.3-alpha.5');
maven.config( extend({finalName: '{name}-{version}', semver: ['prerelease', 'alpha']}, TEST_CONFIG) );
maven.package(true);

assertWarFileToEqual(TEST_PKG_JSON.name + '-' + EXPECTED_VERSION + '.war');
});

});

describe('package', function () {
it('should create an archive based on defaults', function () {
maven.config(TEST_CONFIG);
Expand Down