Launch Week Day 5: Introducing Reachability for PHP.Learn More
Socket
Book a DemoSign in
Socket

module-lookup-amd

Package Overview
Dependencies
Maintainers
1
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

module-lookup-amd - npm Package Compare versions

Comparing version
1.0.0
to
2.0.0
.npmignore

Sorry, the diff of this file is not supported yet

+9
language: node_js
node_js:
- "0.10"
notifications:
email: false
sudo: false
#!/usr/bin/env node
'use strict';
var lookup = require('../');
var program = require('commander');
program
.version(require('../package.json').version)
.usage('[options] <path>')
.option('-c, --config <path>', 'location of a RequireJS config file for AMD')
.option('-f, --filename <path>', 'file containing the dependency')
.parse(process.argv);
var config = program.config;
var filename = program.filename;
var path = program.args[0];
console.log(lookup(config, path, filename));
/**
* Source code extracted from requirejs
*
* @license RequireJS 2.1.18 Copyright (c) 2010-2015, The Dojo Foundation All Rights Reserved.
* Available via the MIT or new BSD license.
* see: http://github.com/jrburke/requirejs for details
*/
var op = Object.prototype;
var hasOwn = op.hasOwnProperty;
var ostring = op.toString;
function hasProp(obj, prop) {
return hasOwn.call(obj, prop);
}
function getOwn(obj, prop) {
return hasProp(obj, prop) && obj[prop];
}
function isArray(it) {
return ostring.call(it) === '[object Array]';
}
/**
* Trims the . and .. from an array of path segments.
* It will keep a leading path segment if a .. will become
* the first path segment, to help with module name lookups,
* which act like paths, but can be remapped. But the end result,
* all paths that use this function should look normalized.
* NOTE: this method MODIFIES the input array.
* @param {Array} ary the array of path segments.
*/
function trimDots(ary) {
var i, part;
for (i = 0; i < ary.length; i++) {
part = ary[i];
if (part === '.') {
ary.splice(i, 1);
i -= 1;
} else if (part === '..') {
// If at the start, or previous value is still ..,
// keep them so that when converted to a path it may
// still work when converted to a path, even though
// as an ID it is less than ideal. In larger point
// releases, may be better to just kick out an error.
if (i === 0 || (i === 1 && ary[2] === '..') || ary[i - 1] === '..') {
continue;
} else if (i > 0) {
ary.splice(i - 1, 2);
i -= 2;
}
}
}
}
/**
* Given a relative module name, like ./something, normalize it to
* a real name that can be mapped to a path.
*
* @param {String} name the relative name
* @param {String} baseName a real name that the name arg is relative to.
*
* @param {Object} config - Requirejs Config for maps
*
* @returns {String} normalized name
*/
module.exports = function normalize(name, baseName, config) {
var baseUrl = config.baseUrl;
var trimmedBase = baseName.split(baseUrl)[1];
if (trimmedBase.indexOf('/') === 0) {
trimmedBase = trimmedBase.slice(1);
}
var mapped = normalizeMap(name, trimmedBase, config);
var pathed = normalizePath(mapped, baseName, config);
return pathed;
}
function normalizeMap(name, baseName, config) {
var pkgMain, mapValue, nameParts, i, j, nameSegment, lastIndex,
foundMap, foundI, foundStarMap, starI, normalizedBaseParts,
baseParts = (baseName && baseName.split('/')),
map = config.map,
starMap = map && map['*'],
// We always want this
applyMap = true;
config.pkgs = config.pkgs || {};
//Adjust any relative paths.
if (name) {
name = name.split('/');
lastIndex = name.length - 1;
// If wanting node ID compatibility, strip .js from end
// of IDs. Have to do this here, and not in nameToUrl
// because node allows either .js or non .js to map
// to same file.
if (config.nodeIdCompat && jsSuffixRegExp.test(name[lastIndex])) {
name[lastIndex] = name[lastIndex].replace(jsSuffixRegExp, '');
}
// Starts with a '.' so need the baseName
if (name[0].charAt(0) === '.' && baseParts) {
//Convert baseName to array, and lop off the last part,
//so that . matches that 'directory' and not name of the baseName's
//module. For instance, baseName of 'one/two/three', maps to
//'one/two/three.js', but we want the directory, 'one/two' for
//this normalization.
normalizedBaseParts = baseParts.slice(0, baseParts.length - 1);
name = normalizedBaseParts.concat(name);
}
trimDots(name);
name = name.join('/');
}
//Apply map config if available.
if (applyMap && map && (baseParts || starMap)) {
nameParts = name.split('/');
outerLoop: for (i = nameParts.length; i > 0; i -= 1) {
nameSegment = nameParts.slice(0, i).join('/');
if (baseParts) {
//Find the longest baseName segment match in the config.
//So, do joins on the biggest to smallest lengths of baseParts.
for (j = baseParts.length; j > 0; j -= 1) {
mapValue = getOwn(map, baseParts.slice(0, j).join('/'));
//baseName segment has config, find if it has one for
//this name.
if (mapValue) {
mapValue = getOwn(mapValue, nameSegment);
if (mapValue) {
//Match, update name to the new value.
foundMap = mapValue;
foundI = i;
break outerLoop;
}
}
}
}
//Check for a star map match, but just hold on to it,
//if there is a shorter segment match later in a matching
//config, then favor over this star map.
if (!foundStarMap && starMap && getOwn(starMap, nameSegment)) {
foundStarMap = getOwn(starMap, nameSegment);
starI = i;
}
}
if (!foundMap && foundStarMap) {
foundMap = foundStarMap;
foundI = starI;
}
if (foundMap) {
nameParts.splice(0, foundI, foundMap);
name = nameParts.join('/');
}
}
// If the name points to a package's name, use
// the package main instead.
pkgMain = getOwn(config.pkgs, name);
return pkgMain ? pkgMain : name;
}
function normalizePath(name, parentModule, config) {
//A module that needs to be converted to a path.
var paths = config.paths;
var syms = name.split('/');
//For each module name segment, see if there is a path
//registered for it. Start with most specific name
//and work up from it.
for (i = syms.length; i > 0; i -= 1) {
var parentModule = syms.slice(0, i).join('/');
var parentPath = getOwn(paths, parentModule);
if (parentPath) {
//If an array, it means there are a few choices,
//Choose the one that is desired
if (isArray(parentPath)) {
parentPath = parentPath[0];
}
syms.splice(0, i, parentPath);
break;
}
}
//Join the path parts together, then figure out if baseUrl is needed.
var url = syms.join('/');
// Add back the base url
url = (url.charAt(0) === '/' || url.match(/^[\w\+\.\-]+:/) ? '' : config.baseUrl) + url;
return config.urlArgs ? url + ((url.indexOf('?') === -1 ? '?' : '&') + config.urlArgs) : url;
}
+15
-27

@@ -1,3 +0,4 @@

var ConfigFile = require('requirejs-config-file').ConfigFile,
path = require('path');
var ConfigFile = require('requirejs-config-file').ConfigFile;
var path = require('path');
var normalize = require('./lib/normalize');

@@ -10,5 +11,7 @@ /**

* @param {String} depPath
* @param {String} filepath - the file containing the dependency
*
* @return {String}
*/
module.exports = function(config, depPath) {
module.exports = function(config, depPath, filepath) {
var configPath;

@@ -21,33 +24,18 @@

var baseUrl = configPath || config.baseUrl,
pathTokens = depPath.split('/'),
topLevelDir = pathTokens[0],
exclamationLocation, alias;
if (config.baseUrl[config.baseUrl.length - 1] !== '/') {
config.baseUrl = config.baseUrl + '/';
}
// Uses a plugin loader
if ((exclamationLocation = topLevelDir.indexOf('!')) !== -1) {
topLevelDir = topLevelDir.slice(exclamationLocation + 1);
if ((exclamationLocation = depPath.indexOf('!')) !== -1) {
depPath = depPath.slice(exclamationLocation + 1);
}
// Check if the top-most dir of path is an alias
alias = config.paths[topLevelDir];
var normalized = normalize(depPath, filepath || '', config);
if (alias) {
// Handle alias values that are relative paths (about the baseUrl)
if (alias.indexOf('..') === 0 && baseUrl) {
// Get the resolved path of the baseURL from the depPath
configPath = configPath || depPath.slice(0, depPath.indexOf(baseUrl) + baseUrl.length);
var filepathWithoutBase = filepath.split(config.baseUrl)[0];
alias = path.resolve(configPath, alias);
}
normalized = path.join(filepathWithoutBase, normalized);
alias = alias[alias.length - 1] === '/' ? alias : alias + '/';
depPath = alias + pathTokens.slice(1).join('/');
}
// Normalize trailing slash
depPath = depPath[depPath.length - 1] === '/' ? depPath.slice(0, -1) : depPath;
return depPath;
return normalized;
};
{
"name": "module-lookup-amd",
"version": "1.0.0",
"version": "2.0.0",
"description": "Resolve aliased dependency paths using a RequireJS config",

@@ -9,2 +9,5 @@ "main": "index.js",

},
"bin": {
"lookup-amd": "bin/cli.js"
},
"repository": {

@@ -27,2 +30,3 @@ "type": "git",

"dependencies": {
"commander": "~2.8.1",
"requirejs-config-file": "~2.0.0"

@@ -29,0 +33,0 @@ },

@@ -1,5 +0,9 @@

### module-lookup-amd
### 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)
Gives you the real path of (possibly) aliased modules. Otherwise, gives you back the same dependency name if it's not aliased.
This module basically exposes the requirejs config map and path resolution logic
and gives you back the real, absolute, path of (possibly) aliased modules names.
I built this for [Dependents'](https://sublime.wbond.net/packages/Dependents) [jump to dependency](https://github.com/mrjoelkemp/Dependents#jump-to-a-dependency) feature that lets you click on a module name
and open the file that name resolves to.
`npm install module-lookup-amd`

@@ -13,8 +17,15 @@

var realPath = lookup('path/to/my/config.js', 'dependency/path');
var realPath = lookup('path/to/my/config.js', 'dependency/path', 'path/to/file/containing/dependency');
```
### `lookup(configPath, dependencyPath)`
### `lookup(configPath, dependencyPath, filepath)`
* `configPath`: the path to your RequireJS configuration file
* `dependencyPath`: the (potentially aliased) dependency that you want to lookup
* `filepath`: the filepath of the file that contains the dependency (i.e., parent module)
### Shell usage
*Assumes a global `-g` installation*
`lookup-amd -c path/to/my/config.js -f path/to/file/containing/dependency my/dependency/name`
require.config({
"baseUrl": "/js",
"paths": {
"a": "./a",
"foobar": "./b",
"templates": "./templates"
"a": "foo/a",
"foobar": "foo/bar/b",
"templates": "../templates"
},
"map": {
"poet": {
"poet/templates": "templates/poet"
}
}
});

@@ -1,11 +0,13 @@

var assert = require('assert'),
path = require('path'),
ConfigFile = require('requirejs-config-file').ConfigFile,
lookup = require('../');
var assert = require('assert');
var path = require('path');
var ConfigFile = require('requirejs-config-file').ConfigFile;
var lookup = require('../');
var dir = '/path/from/my/machine/js';
var filename = dir + '/poet/Remote.js';
var configPath = __dirname + '/example/config.json';
describe('lookup', function() {
var configPath = __dirname + '/example/config.json';
it('returns the real path of an aliased module given a path to a requirejs config file', function() {
assert(lookup(configPath, 'a') === './a');
assert.equal(lookup(configPath, 'a', filename), path.join(dir, 'foo/a'));
});

@@ -15,8 +17,26 @@

var configObject = new ConfigFile(configPath).read();
assert(lookup(configObject, 'foobar') === './b');
assert.equal(lookup(configObject, 'foobar', filename), path.join(dir, 'foo/bar/b'));
});
it('supports paths that use plugin loaders', function() {
assert(lookup(configPath, 'hgn!templates/a') === './templates/a');
assert.equal(lookup(configPath, 'hgn!templates/a', filename), path.join(dir, '../templates/a'));
});
it('supports relative plugin loader paths', function() {
// templates should path lookup to ../templates
assert.equal(lookup(configPath, 'hgn!./templates/a', filename), path.join(dir, '../templates/poet/a'));
assert.equal(lookup(configPath, 'text!./templates/a.mustache', filename), path.join(dir, '../templates/poet/a.mustache'));
});
it('supports map aliasing', function() {
assert.equal(lookup(configPath, 'hgn!./templates/_icons/_embed', filename), path.join(dir, '../templates/poet/_icons/_embed'));
});
it('supports relative pathing', function() {
assert.equal(lookup(configPath, 'hgn!./templates/_icons/_embed', filename), path.join(dir, '../templates/poet/_icons/_embed'));
});
it('returns the same dependency if not aliased', function() {
assert.equal(lookup(configPath, 'my/sweet/path', filename), path.join(dir, 'my/sweet/path'));
});
});