Skip to content
Open
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: 36 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ module.exports = {

### assertAbove

Throws an error with the given message if a minimum version isn't met.
Throws an error if the package version is not greater than the given version.
You can also provide a specific message as the third argument if you'd like to
customize the output.

```javascript
let VersionChecker = require('ember-cli-version-checker');
Expand All @@ -73,11 +75,16 @@ module.exports = {
let checker = new VersionChecker(this.project);

checker.for('ember-cli').assertAbove('2.0.0');
checker.for('ember-cli').assertAbove('2.0.0', 'To use awesome-addon you must have ember-cli 2.0.1');
}
};
```

You can also provide a specific message as the third argument to `assertAbove` if you'd like to customize the output.
### assertAtOrAbove

Throws an error if the package version is not greater than or equal to the
given (minimum) version. You can also provide a specific message as the third
argument if you'd like to customize the output.

```javascript
let VersionChecker = require('ember-cli-version-checker');
Expand All @@ -89,14 +96,15 @@ module.exports = {

let checker = new VersionChecker(this.project);

checker.for('ember-cli').assertAbove('2.0.0', 'To use awesome-addon you must have ember-cli 2.0.0');
checker.for('ember-cli').assertAtOrAbove('2.0.0');
checker.for('ember-cli').assertAtOrAbove('2.0.0', 'To use awesome-addon you must have ember-cli 2.0.0');
}
};
```

### isAbove

Returns `true` if the packages version is above the specified comparison range.
Returns `true` if the package version is greater than the specified comparison version.

```javascript
let VersionChecker = require('ember-cli-version-checker');
Expand All @@ -110,6 +118,30 @@ module.exports = {
let dep = checker.for('ember-cli');

if (dep.isAbove('2.0.0')) {
/* deal with 2.0.1 stuff */
} else {
/* provide backwards compat */
};
}
};
```

### isAtOrAbove

Returns `true` if the package version is greater than or equal to the specified comparison version.

```javascript
let VersionChecker = require('ember-cli-version-checker');

module.exports = {
name: 'awesome-addon',
init() {
this._super.init.apply(this, arguments);

let checker = new VersionChecker(this.project);
let dep = checker.for('ember-cli');

if (dep.isAtOrAbove('2.0.0')) {
/* deal with 2.0.0 stuff */
} else {
/* provide backwards compat */
Expand Down
18 changes: 18 additions & 0 deletions src/dependency-version-checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ class DependencyVersionChecker {
return semver.gt(this.version, compareVersion);
}

isAtOrAbove(compareVersion) {
if (!this.version) {
return false;
}
return semver.gte(this.version, compareVersion);
}

assertAbove(compareVersion, _message) {
let message = _message;
if (!this.isAbove(compareVersion)) {
Expand All @@ -73,6 +80,17 @@ class DependencyVersionChecker {
throw new Error(message);
}
}

assertAtOrAbove(compareVersion, _message) {
let message = _message;
if (!this.isAtOrAbove(compareVersion)) {
if (!message) {
const parentAddon = this._parent._addon;
message = `The addon \`${parentAddon.name}\` @ \`${parentAddon.root}\` requires the npm package \`${this.name}\` to be at or above ${compareVersion}, but you have ${this.version}.`;
}
throw new Error(message);
}
}
}

for (let method of ['gt', 'lt', 'gte', 'lte', 'eq', 'neq', 'satisfies']) {
Expand Down
41 changes: 41 additions & 0 deletions tests/index-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,47 @@ describe('ember-cli-version-checker', function() {
thing.assertAbove('999.0.0', message);
}, new RegExp(message));
});

it('throws an error with a default message if versions match', function() {
let thing = checker.for('ember', 'npm');
let message =
'The addon `.*` requires the npm package `ember` to be above 2.0.0, but you have 2.0.0.';

assert.throws(() => {
thing.assertAbove('2.0.0');
}, new RegExp(message));
});
});

describe('assertAtOrAbove', function() {
it('throws an error with a default message if a matching version was not found', function() {
let thing = checker.for('ember', 'npm');
let message =
'The addon `.*` requires the npm package `ember` to be at or above 999.0.0, but you have 2.0.0.';

assert.throws(() => {
thing.assertAtOrAbove('999.0.0');
}, new RegExp(message));
});

it('throws an error with the given message if a matching version was not found', function() {
let message = 'Must use at least Ember CLI 0.1.2 to use xyz feature';
let thing = checker.for('ember', 'npm');

assert.throws(() => {
thing.assertAtOrAbove('999.0.0', message);
}, new RegExp(message));
});

it('does not throw an error with a default message if versions match', function() {
let thing = checker.for('ember', 'npm');
let message =
'The addon `.*` requires the npm package `ember` to be at or above 2.0.0, but you have 2.0.0.';

assert.doesNotThrow(() => {
thing.assertAtOrAbove('2.0.0');
}, new RegExp(message));
});
});
});
});