ember-cli-version-checker
Advanced tools
Comparing version 1.2.0 to 1.3.0
187
index.js
'use strict'; | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var semver = require('semver'); | ||
function getEmberCLIVersion(addon) { | ||
if (!addon.project || !addon.project.emberCLIVersion) { | ||
return null; | ||
} | ||
var version = addon.project.emberCLIVersion(); | ||
// remove any meta-data, for dealing with `npm link`'ed ember-cli | ||
return version.split('-')[0]; | ||
} | ||
function getVersionFromJSONFile(filePath) { | ||
if (fs.existsSync(filePath)) { | ||
var content = fs.readFileSync(filePath); | ||
try { | ||
return JSON.parse(content).version; | ||
} catch (exception) { | ||
return null; | ||
} | ||
} | ||
} | ||
/** | ||
* VersionChecker | ||
*/ | ||
function VersionChecker(addon) { | ||
this._addon = addon; | ||
} | ||
VersionChecker.prototype.for = function(name, type) { | ||
if (type === 'bower') { | ||
return new BowerDependencyVersionChecker(this, name); | ||
} else if (type === 'npm') { | ||
return new NPMDependencyVersionChecker(this, name); | ||
} | ||
}; | ||
VersionChecker.prototype.forEmber = function() { | ||
var emberVersionChecker = this.for('ember-source', 'npm'); | ||
if (emberVersionChecker.version) { | ||
return emberVersionChecker; | ||
} | ||
return this.for('ember', 'bower'); | ||
}; | ||
/** | ||
* DependencyVersionChecker | ||
*/ | ||
function DependencyVersionChecker(parent, name) { | ||
this._parent = parent; | ||
this.name = name; | ||
} | ||
Object.defineProperty(DependencyVersionChecker.prototype, 'version', { | ||
get: function() { | ||
if (this._version === undefined) { | ||
this._version = getVersionFromJSONFile(this._jsonPath); | ||
} | ||
if (this._version === undefined && this._fallbackJsonPath) { | ||
this._version = getVersionFromJSONFile(this._fallbackJsonPath); | ||
} | ||
return this._version; | ||
} | ||
}); | ||
DependencyVersionChecker.prototype.isAbove = function isAbove(compareVersion) { | ||
if (!this.version) { | ||
return false; | ||
} | ||
return semver.gt(this.version, compareVersion); | ||
} | ||
var semverMethods = ['gt', 'lt', 'satisfies']; | ||
semverMethods.forEach(function(method) { | ||
DependencyVersionChecker.prototype[method] = function(range) { | ||
if (!this.version) { | ||
return false; | ||
} | ||
return semver[method](this.version, range); | ||
}; | ||
}); | ||
DependencyVersionChecker.prototype.assertAbove = function assertAbove(compareVersion, _message) { | ||
var message = _message; | ||
if (!message) { | ||
message = 'The addon `' + this._parent._addon.name + '` requires the ' + this._type + ' package ' + | ||
'`' + this.name + '` to be above ' + compareVersion + ', but you have ' + this.version + '.'; | ||
} | ||
if (!this.isAbove(compareVersion)) { | ||
var error = new Error(message); | ||
error.suppressStacktrace = true; | ||
throw error; | ||
} | ||
}; | ||
DependencyVersionChecker.prototype._super$constructor = DependencyVersionChecker; | ||
/** | ||
* BowerDependencyVersionChecker | ||
*/ | ||
function BowerDependencyVersionChecker() { | ||
this._super$constructor.apply(this, arguments); | ||
var addon = this._parent._addon; | ||
var project = addon.project; | ||
var bowerDependencyPath = path.join(project.root, project.bowerDirectory, this.name); | ||
this._jsonPath = path.join(bowerDependencyPath, '.bower.json'); | ||
this._fallbackJsonPath = path.join(bowerDependencyPath, 'bower.json'); | ||
this._type = 'bower'; | ||
} | ||
BowerDependencyVersionChecker.prototype = Object.create(DependencyVersionChecker.prototype); | ||
/** | ||
* NPMDependencyVersionChecker | ||
*/ | ||
function NPMDependencyVersionChecker() { | ||
this._super$constructor.apply(this, arguments); | ||
var addon = this._parent._addon; | ||
var project = addon.project; | ||
var nodeModulesPath = project.nodeModulesPath || path.join(project.root, 'node_modules') | ||
var npmDependencyPath = path.join(nodeModulesPath, this.name); | ||
this._jsonPath = path.join(npmDependencyPath, 'package.json'); | ||
this._type = 'npm'; | ||
} | ||
NPMDependencyVersionChecker.prototype = Object.create(DependencyVersionChecker.prototype); | ||
/** | ||
* EmberCLIDependencyVersionChecker | ||
*/ | ||
function EmberCLIDependencyVersionChecker(addon) { | ||
// intentially not calling _super here | ||
this._version = getEmberCLIVersion(addon); | ||
this._type = 'npm'; | ||
} | ||
EmberCLIDependencyVersionChecker.prototype = Object.create(DependencyVersionChecker.prototype); | ||
/** | ||
* Backwards compatibility class methods | ||
* | ||
* They compare the version of ember-cli only. | ||
*/ | ||
VersionChecker.isAbove = function deprecatedIsAbove(addon, comparisonVersion) { | ||
var dependencyChecker = new EmberCLIDependencyVersionChecker(addon); | ||
return dependencyChecker.satisfies('>=' + comparisonVersion); | ||
}; | ||
VersionChecker.satisfies = function deprecatedSatisfies(addon, comparison) { | ||
var dependencyChecker = new EmberCLIDependencyVersionChecker(addon); | ||
return dependencyChecker.satisfies(comparison); | ||
}; | ||
VersionChecker.assertAbove = function deprecatedAssertAbove(addon, comparisonVersion, _message) { | ||
var dependencyChecker = new EmberCLIDependencyVersionChecker(addon); | ||
var comparison = '>= ' + comparisonVersion; | ||
var message = _message; | ||
if (!message) { | ||
message = 'The addon `' + addon.name + '` requires an Ember CLI version of ' + comparisonVersion + | ||
' or above, but you are running ' + dependencyChecker.version + '.'; | ||
} | ||
if (!dependencyChecker.satisfies(comparison)) { | ||
var error = new Error(message); | ||
error.suppressStacktrace = true; | ||
throw error; | ||
} | ||
}; | ||
module.exports = VersionChecker; | ||
module.exports = require('./src/version-checker'); |
{ | ||
"name": "ember-cli-version-checker", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Determine if your addon is being used by a minimum version of Ember CLI.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
### Ember CLI Version Checker | ||
Makes determining if a compatible version of a given NPM or Bower package that is present. | ||
[![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 | ||
@@ -17,2 +20,4 @@ | ||
init: function() { | ||
this._super && this._super.init.apply(this, arguments); | ||
var checker = new VersionChecker(this); | ||
@@ -86,1 +91,24 @@ | ||
``` | ||
#### forEmber | ||
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 | ||
var VersionChecker = require('ember-cli-version-checker'); | ||
module.exports = { | ||
name: 'awesome-addon', | ||
init: function() { | ||
var checker = new VersionChecker(this); | ||
var ember = checker.forEmber(); | ||
if (ember.isAbove('2.10.0')) { | ||
/* deal with 2.10.0 stuff */ | ||
}; | ||
} | ||
}; | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
113
0
3820
2
2