Commit e3f3c262 authored by Xavier Guimard's avatar Xavier Guimard

Update upstream source from tag 'upstream/3.1.4+_3.0.2+_3.1.3+_1.0.1'

Update to upstream version '3.1.4+~3.0.2+~3.1.3+~1.0.1'
with Debian dir 44e9af32fb6ec1f4fb3d03ab391fb2daf6a171b2
parents c307111e 2819a6b8
# Logs
logs
*.log
npm-debug.log*
# Runtime data
pids
*.pid
*.seed
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# node-waf configuration
.lock-wscript
# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
# Dependency directory
node_modules
# Optional npm cache directory
.npm
# Optional REPL history
.node_repl_history
language: node_js
node_js:
- "0.10"
- "0.12"
- "4"
- "6"
- "7"
- "8"
sudo: false
1.0.1 03-02-2016
================
* Fix dependency resolution issue when a component in peerOptionalDependencies is installed at the level of the module declaring in peerOptionalDependencies.
1.0.0 03-02-2016
================
* Initial release allowing us to optionally resolve dependencies in the package.json file under the peerOptionalDependencies tag.
\ No newline at end of file
This diff is collapsed.
# require_optional
Work around the problem that we do not have a optionalPeerDependencies concept in node.js making it a hassle to optionally include native modules
var path = require('path'),
fs = require('fs'),
f = require('util').format,
resolveFrom = require('resolve-from'),
semver = require('semver');
var exists = fs.existsSync || path.existsSync;
// Find the location of a package.json file near or above the given location
var find_package_json = function(location) {
var found = false;
while(!found) {
if (exists(location + '/package.json')) {
found = location;
} else if (location !== '/') {
location = path.dirname(location);
} else {
return false;
}
}
return location;
}
// Find the package.json object of the module closest up the module call tree that contains name in that module's peerOptionalDependencies
var find_package_json_with_name = function(name) {
// Walk up the module call tree until we find a module containing name in its peerOptionalDependencies
var currentModule = module;
var found = false;
while (currentModule) {
// Check currentModule has a package.json
location = currentModule.filename;
var location = find_package_json(location)
if (!location) {
currentModule = currentModule.parent;
continue;
}
// Read the package.json file
var object = JSON.parse(fs.readFileSync(f('%s/package.json', location)));
// Is the name defined by interal file references
var parts = name.split(/\//);
// Check whether this package.json contains peerOptionalDependencies containing the name we're searching for
if (!object.peerOptionalDependencies || (object.peerOptionalDependencies && !object.peerOptionalDependencies[parts[0]])) {
currentModule = currentModule.parent;
continue;
}
found = true;
break;
}
// Check whether name has been found in currentModule's peerOptionalDependencies
if (!found) {
throw new Error(f('no optional dependency [%s] defined in peerOptionalDependencies in any package.json', parts[0]));
}
return {
object: object,
parts: parts
}
}
var require_optional = function(name, options) {
options = options || {};
options.strict = typeof options.strict == 'boolean' ? options.strict : true;
var res = find_package_json_with_name(name)
var object = res.object;
var parts = res.parts;
// Unpack the expected version
var expectedVersions = object.peerOptionalDependencies[parts[0]];
// The resolved package
var moduleEntry = undefined;
// Module file
var moduleEntryFile = name;
try {
// Validate if it's possible to read the module
moduleEntry = require(moduleEntryFile);
} catch(err) {
// Attempt to resolve in top level package
try {
// Get the module entry file
moduleEntryFile = resolveFrom(process.cwd(), name);
if(moduleEntryFile == null) return undefined;
// Attempt to resolve the module
moduleEntry = require(moduleEntryFile);
} catch(err) {
if(err.code === 'MODULE_NOT_FOUND') return undefined;
}
}
// Resolve the location of the module's package.json file
var location = find_package_json(require.resolve(moduleEntryFile));
if(!location) {
throw new Error('package.json can not be located');
}
// Read the module file
var dependentOnModule = JSON.parse(fs.readFileSync(f('%s/package.json', location)));
// Get the version
var version = dependentOnModule.version;
// Validate if the found module satisfies the version id
if(semver.satisfies(version, expectedVersions) == false
&& options.strict) {
var error = new Error(f('optional dependency [%s] found but version [%s] did not satisfy constraint [%s]', parts[0], version, expectedVersions));
error.code = 'OPTIONAL_MODULE_NOT_FOUND';
throw error;
}
// Satifies the module requirement
return moduleEntry;
}
require_optional.exists = function(name) {
try {
var m = require_optional(name);
if(m === undefined) return false;
return true;
} catch(err) {
return false;
}
}
module.exports = require_optional;
{
"name": "require_optional",
"version": "1.0.1",
"description": "Allows you declare optionalPeerDependencies that can be satisfied by the top level module but ignored if they are not.",
"main": "index.js",
"scripts": {
"test": "mocha"
},
"repository": {
"type": "git",
"url": "https://github.com/christkv/require_optional.git"
},
"keywords": [
"optional",
"require",
"optionalPeerDependencies"
],
"author": "Christian Kvalheim Amor",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/christkv/require_optional/issues"
},
"homepage": "https://github.com/christkv/require_optional",
"dependencies": {
"semver": "^5.1.0",
"resolve-from": "^2.0.0"
},
"devDependencies": {
"bson": "0.4.21",
"co": "4.6.0",
"es6-promise": "^3.0.2",
"mocha": "^2.4.5"
},
"peerOptionalDependencies": {
"co": ">=5.6.0",
"es6-promise": "^3.0.2",
"es6-promise2": "^4.0.2",
"bson": "0.4.21"
}
}
var require_optional = require('../../')
function findPackage(packageName) {
var pkg = require_optional(packageName);
return pkg;
}
module.exports.findPackage = findPackage
{
"name": "nestedtest",
"version": "1.0.0",
"description": "A dummy package that facilitates testing that require_optional correctly walks up the module call stack",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Sebastian Hallum Clarke",
"license": "ISC"
}
var assert = require('assert'),
require_optional = require('../'),
nestedTest = require('./nestedTest');
describe('Require Optional', function() {
describe('top level require', function() {
it('should correctly require co library', function() {
var promise = require_optional('es6-promise');
assert.ok(promise);
});
it('should fail to require es6-promise library', function() {
try {
require_optional('co');
} catch(e) {
assert.equal('OPTIONAL_MODULE_NOT_FOUND', e.code);
return;
}
assert.ok(false);
});
it('should ignore optional library not defined', function() {
assert.equal(undefined, require_optional('es6-promise2'));
});
});
describe('internal module file require', function() {
it('should correctly require co library', function() {
var Long = require_optional('bson/lib/bson/long.js');
assert.ok(Long);
});
});
describe('top level resolve', function() {
it('should correctly use exists method', function() {
assert.equal(false, require_optional.exists('co'));
assert.equal(true, require_optional.exists('es6-promise'));
assert.equal(true, require_optional.exists('bson/lib/bson/long.js'));
assert.equal(false, require_optional.exists('es6-promise2'));
});
});
describe('require_optional inside dependencies', function() {
it('should correctly walk up module call stack searching for peerOptionalDependencies', function() {
assert.ok(nestedTest.findPackage('bson'))
});
it('should return null when a package is defined in top-level package.json but not installed', function() {
assert.equal(null, nestedTest.findPackage('es6-promise2'))
});
it('should error when searching for an optional dependency that is not defined in any ancestor package.json', function() {
try {
nestedTest.findPackage('bison')
} catch (err) {
assert.equal(err.message, 'no optional dependency [bison] defined in peerOptionalDependencies in any package.json')
}
})
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment