Merge pull request #117 from strongloop/fix/compiler-code-cleanup

compiler: code cleanup
This commit is contained in:
Miroslav Bajtoš 2015-04-07 12:38:24 +02:00
commit ade8605618
1 changed files with 51 additions and 41 deletions

View File

@ -322,16 +322,33 @@ function tryResolveAppPath(rootDir, relativePath, resolveOptions) {
* does not enforce any such restriction when resolving the path */
resolveOptions = resolveOptions || { strict: false };
var isModuleRelative = false;
if (relativePath[0] === '/') {
fullPath = relativePath;
} else if (start === './' || start === '..' || !resolveOptions.strict) {
} else if (start === './' || start === '..') {
fullPath = path.resolve(rootDir, relativePath);
} else if (!resolveOptions.strict) {
isModuleRelative = true;
fullPath = path.resolve(rootDir, relativePath);
}
if (fullPath && fs.existsSync(fullPath))
if (fullPath) {
// This check is needed to support paths pointing to a directory
if (fs.existsSync(fullPath)) {
return fullPath;
}
try {
fullPath = require.resolve(fullPath);
return fullPath;
} catch (err) {
if (!isModuleRelative) {
debug ('Skipping %s - %s', fullPath, err);
return undefined;
}
}
}
if (start !== './' && start !== '..') {
// Handle module-relative path, e.g. `loopback/common/models`
// Module.globalPaths is a list of globally configured paths like
@ -358,16 +375,8 @@ function tryResolveAppPath(rootDir, relativePath, resolveOptions) {
if (fullPath)
return fullPath;
} else {
// Handle relative path, e.g. `./common/models`
try {
fullPath = require.resolve(fullPath);
return fullPath;
} catch (err) {
debug ('Skipping %s - %s', fullPath, err);
}
}
debug ('Skipping %s - module not found', fullPath);
return undefined;
}
@ -447,6 +456,7 @@ function resolveMiddlewarePath(rootDir, middleware) {
var pathName = segments[0];
var fragment = segments[1];
var middlewarePath = pathName;
var opts = { strict: true };
if (fragment) {
resolved.fragment = fragment;
@ -458,7 +468,7 @@ function resolveMiddlewarePath(rootDir, middleware) {
}
if (!fragment) {
resolved.sourceFile = resolveAppScriptPath(rootDir, middlewarePath);
resolved.sourceFile = resolveAppScriptPath(rootDir, middlewarePath, opts);
return resolved;
}
@ -468,7 +478,7 @@ function resolveMiddlewarePath(rootDir, middleware) {
// function
var m = require(pathName);
if (typeof m[fragment] === 'function') {
resolved.sourceFile = resolveAppScriptPath(rootDir, middlewarePath);
resolved.sourceFile = resolveAppScriptPath(rootDir, middlewarePath, opts);
return resolved;
}
@ -486,7 +496,7 @@ function resolveMiddlewarePath(rootDir, middleware) {
for (var ix in candidates) {
try {
resolved.sourceFile = resolveAppScriptPath(rootDir, candidates[ix]);
resolved.sourceFile = resolveAppScriptPath(rootDir, candidates[ix], opts);
delete resolved.fragment;
return resolved;
}
@ -516,7 +526,7 @@ function buildComponentInstructions(rootDir, componentConfig) {
.filter(function(name) { return !!componentConfig[name]; })
.map(function(name) {
return {
sourceFile: resolveAppScriptPath(rootDir, name, {'strict': true}),
sourceFile: resolveAppScriptPath(rootDir, name, { strict: true }),
config: componentConfig[name]
};
});