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

Add .exists() and document .version. #43

Merged
merged 3 commits into from
Oct 8, 2017
Merged
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
95 changes: 70 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,40 @@
### Ember CLI Version Checker
# Ember CLI Version Checker

[![npm version](https://badge.fury.io/js/ember-cli-version-checker.svg)](https://badge.fury.io/js/ember-cli-version-checker)
[![Build Status](https://travis-ci.org/ember-cli/ember-cli-version-checker.svg?branch=master)](https://travis-ci.org/ember-cli/ember-cli-version-checker)

Makes it easier to determine if a compatible version of a given NPM or Bower package is present.

### Usage
## Usage

#### assertAbove
Example:

You want to provide two different sets of templates, based on the currently running Ember version.

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

module.exports = {
name: 'awesome-addon',
treeForAddonTemplates(tree) {
let checker = new VersionChecker(this);
let dep = checker.for('ember', 'bower');

let baseTemplatesPath = path.join(this.root, 'addon/templates');

if (dep.satisfies('>= 1.13.0') {
return this.treeGenerator(path.join(baseTemplatesPath, 'current'));
} else {
return this.treeGenerator(path.join(baseTemplatesPath, 'legacy'));
}
}
};
```

## API

### assertAbove

Throws an error with the given message if a minimum version isn't met.

Expand Down Expand Up @@ -41,7 +68,7 @@ module.exports = {
};
```

#### isAbove
### isAbove

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

Expand All @@ -63,50 +90,68 @@ module.exports = {
};
```

#### Real World Example
### forEmber

You want to provide two different sets of templates, based on the currently running Ember version.
Since ember introduced the `ember-source` from NPM, ember has two ways to be
shipped. One from bower `ember` and other from NPM `ember-source`. The
function `forEmber` will look for `ember-source` from NPM, if it doesn't
find it, it will look for `ember` in bower.

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

module.exports = {
name: 'awesome-addon',
treeForAddonTemplates(tree) {
init() {
let checker = new VersionChecker(this);
let dep = checker.for('ember', 'bower');

let baseTemplatesPath = path.join(this.root, 'addon/templates');
let ember = checker.forEmber();

if (dep.satisfies('>= 1.13.0') {
return this.treeGenerator(path.join(baseTemplatesPath, 'current'));
} else {
return this.treeGenerator(path.join(baseTemplatesPath, 'legacy'));
}
if (ember.isAbove('2.10.0')) {
/* deal with 2.10.0 stuff */
};
}
};
```

#### forEmber
### exists

Since ember introduced the `ember-source` from NPM, ember has two ways to be
shipped. One from bower `ember` and other from NPM `ember-source`. The
function `forEmber` will look for `ember-source` from NPM, if it doesn't
find it, it will look for `ember` in bower.
Returns `true` or `false` indicating if the dependency exists (at any version).

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

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

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

if (ember.isAbove('2.10.0')) {
/* deal with 2.10.0 stuff */
if (dep.exists()) {
/* do things when present */
};
}
};
```

### version

A property that returns the version for the dependency, if the dependency is not found
`undefined` will be returned.

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

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

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

// do something with dep.version
}
};
```
4 changes: 4 additions & 0 deletions src/dependency-version-checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ class DependencyVersionChecker {
return this._version;
}

exists() {
return this.version !== undefined;
}

isAbove(compareVersion) {
if (!this.version) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/version-checker.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const path = require('path');
/* eslint-env node */

const BowerDependencyVersionChecker = require('./bower-dependency-version-checker');
const NPMDependencyVersionChecker = require('./npm-dependency-version-checker');
Expand Down
16 changes: 16 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

/* eslint-env mocha, node */

const assert = require('assert');
const VersionChecker = require('..');
const co = require('co');
Expand Down Expand Up @@ -136,6 +138,20 @@ describe('ember-cli-version-checker', function() {
});
});

describe('exists', function() {
it('returns true when present', function() {
let thing = checker.for('ember');

assert.ok(thing.exists());
});

it('returns false when not present', function() {
let thing = checker.for('rando-thing-here');

assert.ok(!thing.exists());
});
});

describe('version', function() {
it('can return a bower version', function() {
let thing = checker.for('ember', 'bower');
Expand Down