Merge pull request #136 from strongloop/fix/omit-middleware-main-module-file

Excl. mod. main path from middleware instructions
This commit is contained in:
Miroslav Bajtoš 2015-06-24 19:00:15 +02:00
commit 5c8334c7c7
2 changed files with 36 additions and 13 deletions

View File

@ -408,21 +408,29 @@ function tryResolveAppPath(rootDir, relativePath, resolveOptions) {
fullPath = modulePaths fullPath = modulePaths
.map(function(candidateDir) { .map(function(candidateDir) {
var absPath = path.join(candidateDir, relativePath);
try { try {
var filePath = path.join(candidateDir, relativePath); // NOTE(bajtos) We need to create a proper String object here,
filePath = require.resolve(filePath); // otherwise we can't attach additional properties to it
/*jshint -W053 */
var filePath = new String(require.resolve(absPath));
filePath.unresolvedPath = absPath;
return filePath; return filePath;
} catch (err) { } catch (err) {
return filePath; return absPath;
} }
}) })
.filter(function(candidate) { .filter(function(candidate) {
return fs.existsSync(candidate); return fs.existsSync(candidate.toString());
}) })
[0]; [0];
if (fullPath) if (fullPath) {
return fullPath; if (fullPath.unresolvedPath && resolveOptions.fullResolve === false)
return fullPath.unresolvedPath;
// Convert String object back to plain string primitive
return fullPath.toString();
}
debug ('Skipping %s - module not found', fullPath); debug ('Skipping %s - module not found', fullPath);
return undefined; return undefined;
@ -517,18 +525,28 @@ function resolveMiddlewarePath(rootDir, middleware) {
pathName = path.resolve(rootDir, pathName); pathName = path.resolve(rootDir, pathName);
} }
var resolveOpts = _.extend(opts, {
// Workaround for strong-agent to allow probes to detect that
// strong-express-middleware was loaded: exclude the path to the
// module main file from the source file path.
// For example, return
// node_modules/strong-express-metrics
// instead of
// node_modules/strong-express-metrics/index.js
fullResolve: false
});
var sourceFile = resolveAppScriptPath(rootDir, middlewarePath, resolveOpts);
if (!fragment) { if (!fragment) {
resolved.sourceFile = resolveAppScriptPath(rootDir, middlewarePath, opts); resolved.sourceFile = sourceFile;
return resolved; return resolved;
} }
var err;
// Try to require the module and check if <module>.<fragment> is a valid // Try to require the module and check if <module>.<fragment> is a valid
// function // function
var m = require(pathName); var m = require(pathName);
if (typeof m[fragment] === 'function') { if (typeof m[fragment] === 'function') {
resolved.sourceFile = resolveAppScriptPath(rootDir, middlewarePath, opts); resolved.sourceFile = sourceFile;
return resolved; return resolved;
} }
@ -544,6 +562,7 @@ function resolveMiddlewarePath(rootDir, middleware) {
// pathName + '/' + fragment // pathName + '/' + fragment
]; ];
var err;
for (var ix in candidates) { for (var ix in candidates) {
try { try {
resolved.sourceFile = resolveAppScriptPath(rootDir, candidates[ix], opts); resolved.sourceFile = resolveAppScriptPath(rootDir, candidates[ix], opts);

View File

@ -1759,7 +1759,7 @@ describe('compiler', function() {
expect(instructions.middleware.middleware[0]).have.property( expect(instructions.middleware.middleware[0]).have.property(
'sourceFile', 'sourceFile',
require.resolve('loopback')); pathWithoutIndex(require.resolve('loopback')));
expect(instructions.middleware.middleware[0]).have.property( expect(instructions.middleware.middleware[0]).have.property(
'fragment', 'fragment',
'errorHandler'); 'errorHandler');
@ -1781,7 +1781,7 @@ describe('compiler', function() {
expect(instructions.middleware.middleware[0]).have.property( expect(instructions.middleware.middleware[0]).have.property(
'sourceFile', 'sourceFile',
appdir.resolve(HANDLER_FILE)); pathWithoutIndex(appdir.resolve(HANDLER_FILE)));
}); });
it('prefers appRootDir over node_modules for middleware', function() { it('prefers appRootDir over node_modules for middleware', function() {
@ -1846,7 +1846,7 @@ describe('compiler', function() {
expect(instructions.middleware.middleware).to.have.length(1); expect(instructions.middleware.middleware).to.have.length(1);
expect(instructions.middleware.middleware[0]).have.property( expect(instructions.middleware.middleware[0]).have.property(
'sourceFile', file); 'sourceFile', pathWithoutIndex(file));
}); });
it('prefers coffeescript over json for relative middleware path', it('prefers coffeescript over json for relative middleware path',
@ -2077,3 +2077,7 @@ function pathWithoutExtension(value) {
path.dirname(value), path.dirname(value),
path.basename(value, path.extname(value))); path.basename(value, path.extname(value)));
} }
function pathWithoutIndex(filePath) {
return filePath.replace(/[\\\/]index\.[^.]+$/, '');
}