module-lookup-amd
Advanced tools
Comparing version 2.0.3 to 2.0.4
@@ -14,2 +14,3 @@ #!/usr/bin/env node | ||
.option('-f, --filename <path>', 'file containing the dependency') | ||
.option('-d, --directory <path>', 'directory containing all files') | ||
.parse(process.argv); | ||
@@ -16,0 +17,0 @@ |
43
index.js
var ConfigFile = require('requirejs-config-file').ConfigFile; | ||
var path = require('path'); | ||
var normalize = require('./lib/normalize'); | ||
var debug = require('debug')('lookup'); | ||
@@ -12,11 +13,18 @@ /** | ||
* @param {String} filepath - the file containing the dependency | ||
* @param {String} [directory] - location of all files | ||
* | ||
* @return {String} | ||
*/ | ||
module.exports = function(config, depPath, filepath) { | ||
module.exports = function(config, depPath, filepath, directory) { | ||
var configPath; | ||
debug('given config: ', config); | ||
debug('given depPath: ', depPath); | ||
debug('given filepath: ', filepath); | ||
debug('given directory: ', directory); | ||
if (typeof config === 'string') { | ||
configPath = path.dirname(config); | ||
config = module.exports._readConfig(config); | ||
debug('converting given config file to an object'); | ||
} | ||
@@ -26,2 +34,3 @@ | ||
config.baseUrl = configPath || './'; | ||
debug('no baseUrl found in config. Defaulting to ' + config.baseUrl); | ||
} | ||
@@ -31,16 +40,38 @@ | ||
config.baseUrl = config.baseUrl + '/'; | ||
debug('normalized the trailing slash'); | ||
} | ||
debug('baseUrl: ', config.baseUrl); | ||
var filepathWithoutBase = filepath.split(config.baseUrl)[0]; | ||
debug('filepath without base ' + filepathWithoutBase); | ||
// Uses a plugin loader | ||
var exclamationLocation; | ||
if ((exclamationLocation = depPath.indexOf('!')) !== -1) { | ||
var exclamationLocation = depPath.indexOf('!'); | ||
if (exclamationLocation !== -1) { | ||
debug('stripping off the plugin loader'); | ||
depPath = depPath.slice(exclamationLocation + 1); | ||
debug('depPath is now ' + depPath); | ||
} | ||
var normalized = normalize(depPath, filepath || '', config); | ||
var normalized = normalize(depPath, filepath, config); | ||
debug('normalized path is ' + normalized); | ||
var filepathWithoutBase = filepath.split(config.baseUrl)[0]; | ||
// A file containing the dependency that's not within the baseurl | ||
// Example: a test file importing the dependency | ||
if (filepath.indexOf(config.baseUrl) === -1) { | ||
debug('filepath was not within baseUrl'); | ||
normalized = path.join(filepathWithoutBase, normalized); | ||
if (!directory) { | ||
debug('did not know how to resolve the path'); | ||
return ''; | ||
} | ||
normalized = path.join(directory, normalized); | ||
} else { | ||
normalized = path.join(filepathWithoutBase, normalized); | ||
} | ||
debug('final normalized path is ' + normalized); | ||
return normalized; | ||
@@ -47,0 +78,0 @@ }; |
@@ -0,1 +1,3 @@ | ||
var debug = require('debug')('lookup'); | ||
//jscs: disable | ||
@@ -66,3 +68,3 @@ | ||
* @param {String} name the relative name | ||
* @param {String} baseName a real name that the name arg is relative to. | ||
* @param {String} filepath a real name that the name arg is relative to. | ||
* | ||
@@ -73,13 +75,17 @@ * @param {Object} config - Requirejs Config for maps | ||
*/ | ||
module.exports = function normalize(name, baseName, config) { | ||
module.exports = function normalize(name, filepath, config) { | ||
var baseUrl = config.baseUrl; | ||
var trimmedBase = baseName.split(baseUrl)[1] || baseName; | ||
var trimmedBase = filepath.split(baseUrl)[1] || filepath || ''; | ||
if (trimmedBase.indexOf('/') === 0) { | ||
debug('trimming the filepath to ' + trimmedBase); | ||
if (trimmedBase && trimmedBase.indexOf('/') === 0) { | ||
trimmedBase = trimmedBase.slice(1); | ||
debug('stripping off the leading slash to ' + trimmedBase); | ||
} | ||
var mapped = normalizeMap(name, trimmedBase, config); | ||
var mapped = config.map ? normalizeMap(name, trimmedBase, config) : name; | ||
debug('mapped filepath ' + mapped); | ||
var pathed = normalizePath(mapped, baseName, config); | ||
var pathed = config.paths ? normalizePath(mapped, filepath, config) : mapped; | ||
debug('pathed filepath ' + pathed); | ||
@@ -86,0 +92,0 @@ return pathed; |
{ | ||
"name": "module-lookup-amd", | ||
"version": "2.0.3", | ||
"version": "2.0.4", | ||
"description": "Resolve aliased dependency paths using a RequireJS config", | ||
@@ -30,2 +30,3 @@ "main": "index.js", | ||
"commander": "^2.8.1", | ||
"debug": "~2.2.0", | ||
"requirejs-config-file": "~2.0.0" | ||
@@ -32,0 +33,0 @@ }, |
@@ -14,3 +14,2 @@ ### module-lookup-amd [![npm](http://img.shields.io/npm/v/module-lookup-amd.svg)](https://npmjs.org/package/module-lookup-amd) [![npm](http://img.shields.io/npm/dm/module-lookup-amd.svg)](https://npmjs.org/package/module-lookup-amd) | ||
```js | ||
var lookup = require('module-lookup-amd'); | ||
@@ -21,3 +20,3 @@ | ||
### `lookup(configPath, dependencyPath, filepath)` | ||
### `lookup(configPath, dependencyPath, filepath, directory)` | ||
@@ -27,2 +26,4 @@ * `configPath`: the path to your RequireJS configuration file | ||
* `filepath`: the filepath of the file that contains the dependency (i.e., parent module) | ||
* `directory`: (Optional) the path to all files | ||
* Used as a last resort for resolving paths from files that are not within the config's base url (like test files that import a module) | ||
@@ -33,2 +34,2 @@ ### Shell usage | ||
`lookup-amd -c path/to/my/config.js -f path/to/file/containing/dependency my/dependency/name` | ||
`lookup-amd -c path/to/my/config.js -f path/to/file/containing/dependency -d path/containing/all/files my/dependency/name` |
@@ -9,7 +9,13 @@ var assert = require('assert'); | ||
var dir = '/path/from/my/machine/js'; | ||
var filename = dir + '/poet/Remote.js'; | ||
var configPath = __dirname + '/example/config.json'; | ||
var dir; | ||
var filename; | ||
var configPath; | ||
describe('lookup', function() { | ||
beforeEach(function() { | ||
dir = '/path/from/my/machine/js'; | ||
filename = dir + '/poet/Remote.js'; | ||
configPath = __dirname + '/example/config.json'; | ||
}); | ||
it('returns the real path of an aliased module given a path to a requirejs config file', function() { | ||
@@ -55,6 +61,24 @@ assert.equal(lookup(configPath, 'a', filename), path.join(dir, 'foo/a')); | ||
it('does not throw if config.map is missing', function() { | ||
var configObject = new ConfigFile(configPath).read(); | ||
delete configObject.map; | ||
assert.doesNotThrow(function() { | ||
lookup(configObject, 'foobar', filename); | ||
}); | ||
}); | ||
it('does not throw if config.paths is missing', function() { | ||
var configObject = new ConfigFile(configPath).read(); | ||
delete configObject.paths; | ||
assert.doesNotThrow(function() { | ||
lookup(configObject, 'foobar', filename); | ||
}); | ||
}); | ||
describe('when no baseUrl is in the config', function() { | ||
it('defaults the baseUrl to the directory of the config file', function() { | ||
var stub = sinon.stub().returns(''); | ||
lookup.__set__('normalize', stub); | ||
var revert = lookup.__set__('normalize', stub); | ||
@@ -67,2 +91,3 @@ sinon.stub(lookup, '_readConfig').returns({baseUrl: undefined}); | ||
assert.equal(stub.args[0][2].baseUrl, path.dirname(configPath) + '/'); | ||
revert(); | ||
}); | ||
@@ -76,3 +101,3 @@ | ||
lookup.__set__('normalize', stub); | ||
var revert = lookup.__set__('normalize', stub); | ||
@@ -82,4 +107,21 @@ lookup(configObject, 'foobar', filename); | ||
assert.equal(stub.args[0][2].baseUrl, './'); | ||
revert(); | ||
}); | ||
}); | ||
describe('when a filepath is not within the base url', function() { | ||
it('does not throw', function() { | ||
assert.doesNotThrow(function() { | ||
lookup(configPath, 'my/sweet/path', '/some/random/folder/foo.js'); | ||
}) | ||
}); | ||
it('returns an empty string if a directory is not given', function() { | ||
assert.equal(lookup(configPath, 'my/sweet/path', '/some/random/folder/foo.js'), ''); | ||
}); | ||
it('returns the normalized path about the given directory and the base url', function() { | ||
assert.equal(lookup(configPath, 'my/sweet/path', '/some/random/folder/foo.js', '/some/random/folder/'), '/some/random/folder/my/sweet/path'); | ||
}); | ||
}); | ||
}); |
17684
388
32
3
+ Addeddebug@~2.2.0
+ Addeddebug@2.2.0(transitive)
+ Addedms@0.7.1(transitive)