Comparing version 0.1.5 to 0.1.6
@@ -1,2 +0,2 @@ | ||
var fs = require('fs') | ||
var fs = require('fs') | ||
var path = require('path') | ||
@@ -15,3 +15,4 @@ var resolve = require('resolve').sync | ||
nodeModulesResolve, | ||
globalResolve | ||
prefixResolve, | ||
execPathResolve | ||
] | ||
@@ -28,3 +29,3 @@ | ||
// resolve using native require() function | ||
// if NODE_PATH is define, a global module should be natively resolved | ||
// if NODE_PATH is defined, a global module should be natively resolved | ||
function nativeResolve(module, dirname) { | ||
@@ -43,5 +44,4 @@ try { | ||
if (!nodePath) { return } | ||
if (isWin32) { nodePath = nodePath.replace(':', ';') } | ||
nodePath = nodePath.split(';').map(function (nodepath) { | ||
nodePath = nodePath.split(path.delimiter).map(function (nodepath) { | ||
return path.normalize(nodepath) | ||
@@ -63,3 +63,3 @@ }) | ||
var paths = [ | ||
var paths = [ | ||
'node_modules', | ||
@@ -80,4 +80,4 @@ 'node_libraries', | ||
// See: https://npmjs.org/doc/files/npm-folders.html#prefix-Configuration | ||
// it uses execPath to discover node_modules based on the node binary location | ||
function globalResolve(module) { | ||
// it uses execPath to discover the default prefix on *nix and %APPDATA% on Windows | ||
function prefixResolve(module) { | ||
var modulePath, dirname | ||
@@ -87,9 +87,10 @@ var prefix = rc('npm').prefix | ||
if (isWin32) { | ||
dirname = prefix || path.dirname(process.execPath) | ||
prefix = prefix || path.join(process.env.APPDATA, 'npm') | ||
dirname = prefix | ||
} | ||
else { | ||
prefix = prefix || path.join(process.execPath, '../..') | ||
prefix = prefix || path.join(path.dirname(process.execPath), '..') | ||
dirname = path.join(prefix, 'lib') | ||
} | ||
dirname = path.join(dirname, 'node_modules') | ||
@@ -101,2 +102,21 @@ modulePath = resolveFn(module, dirname) | ||
// Resolves packages using the node installation path | ||
// Useful for resolving global packages such as npm when the prefix has been overriden by the user | ||
function execPathResolve(module) { | ||
var modulePath, dirname | ||
var execPath = path.dirname(process.execPath) | ||
if (isWin32) { | ||
dirname = execPath | ||
} | ||
else { | ||
dirname = path.join(execPath, '..', 'lib') | ||
} | ||
dirname = path.join(dirname, 'node_modules') | ||
modulePath = resolveFn(module, dirname) | ||
return modulePath | ||
} | ||
function nodeModulesResolve(module) { | ||
@@ -107,3 +127,3 @@ var i, l, modulePath | ||
if (typeof nodeModules === 'string') { | ||
nodeModules = nodeModules.split(';') | ||
nodeModules = nodeModules.split(path.delimiter) | ||
for (i = 0, l = nodeModules.length; i < l; i += 1) { | ||
@@ -115,4 +135,4 @@ if (modulePath = resolveFn(module, nodeModules[i])) { | ||
} | ||
return modulePath | ||
} |
{ | ||
"name": "requireg", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"description": "Require and resolve global modules like a boss", | ||
@@ -30,3 +30,3 @@ "homepage": "http://github.com/h2non/requireg", | ||
"scripts": { | ||
"test": "make test" | ||
"test": "mocha -u tdd --ui exports --reporter spec --slow 2000ms --bail" | ||
}, | ||
@@ -33,0 +33,0 @@ "keywords": [ |
@@ -0,1 +1,3 @@ | ||
var path = require('path'); | ||
var expect = require('expect.js') | ||
@@ -17,3 +19,3 @@ var resolvers = require('rewire')('../lib/resolvers') | ||
describe('requireg API', function () { | ||
it('should globalize', function () { | ||
@@ -43,3 +45,3 @@ requiregModule.globalize() | ||
before(function () { | ||
process.env.NODE_PATH = __dirname + '/fixtures/lib' | ||
process.env.NODE_PATH = path.join(__dirname, 'fixtures', 'lib'); | ||
}) | ||
@@ -57,4 +59,4 @@ | ||
expect(requiregModule.resolve('beaker')) | ||
.to.be.equal(__dirname + '/fixtures/lib/node_modules/beaker/index.js') | ||
}) | ||
.to.be.equal(path.join(__dirname, 'fixtures', 'lib', 'node_modules', 'beaker', 'index.js')) | ||
}) | ||
@@ -66,3 +68,3 @@ }) | ||
before(function () { | ||
process.env[homeVar] = __dirname + '/fixtures/lib' | ||
process.env[homeVar] = path.join(__dirname, 'fixtures', 'lib') | ||
}) | ||
@@ -83,3 +85,3 @@ | ||
before(function () { | ||
process.env.NODE_MODULES = __dirname + '/fixtures/lib' | ||
process.env.NODE_MODULES = path.join(__dirname, 'fixtures', 'lib') | ||
}) | ||
@@ -102,4 +104,3 @@ | ||
before(function () { | ||
process.execPath = __dirname + '/fixtures/bin/node' | ||
resolvers.__set__('rc', function () { return {} }) | ||
process.execPath = path.join(__dirname, 'fixtures', (isWin32 ? 'lib' : 'bin'), 'node') | ||
}) | ||
@@ -109,3 +110,2 @@ | ||
process.execPath = execPath | ||
resolvers.__set__('rc', rc) | ||
}) | ||
@@ -119,3 +119,3 @@ | ||
expect(requiregModule.resolve('beaker')) | ||
.to.be.equal(__dirname + '/fixtures/lib/node_modules/beaker/index.js') | ||
.to.be.equal(path.join(__dirname, 'fixtures', 'lib', 'node_modules', 'beaker', 'index.js')) | ||
}) | ||
@@ -131,3 +131,3 @@ | ||
return { | ||
prefix: __dirname + (isWin32 ? '/fixtures/lib' : '/fixtures') | ||
prefix: path.join(__dirname, 'fixtures', (isWin32 ? 'lib' : '')) | ||
} | ||
@@ -147,3 +147,3 @@ }) | ||
expect(requiregModule.resolve('beaker')) | ||
.to.be.equal(__dirname + '/fixtures/lib/node_modules/beaker/index.js') | ||
.to.be.equal(path.join(__dirname, 'fixtures', 'lib', 'node_modules', 'beaker', 'index.js')) | ||
}) | ||
@@ -150,0 +150,0 @@ |
Sorry, the diff of this file is not supported yet
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
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
12124
233
16