From 43df90a4c8545743303601b25facb187892fb59f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Tue, 5 May 2015 11:22:08 +0200 Subject: [PATCH] compiler: Simplify verifyModelDefinitions() Refactor `verifyModelDefinitions()` to use the new function `fixFileExtension` instead of removed `filterValidSourceFiles()`. --- lib/compiler.js | 41 +++++++---------------------------------- 1 file changed, 7 insertions(+), 34 deletions(-) diff --git a/lib/compiler.js b/lib/compiler.js index 20c34c5..3538fe9 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -276,25 +276,6 @@ function sortByInheritance(instructions) { }); } -/** - * Returns matching files with a supported `sourceFile` extension like - * `.js` or `.coffee` - * @param {Array} allFiles - * @param {string} basename - * @returns {Array} - */ -function filterValidSourceFiles(allFiles, basename) { - var base, ext, validFileType; - return allFiles - .filter(function(f) { - ext = path.extname(f); - base = path.basename(f, ext); - validFileType = (ext !== '.node') && (ext !== '.json') && - ((typeof require.extensions[ext]) === 'function'); - return validFileType && (base === basename); - }); -} - function verifyModelDefinitions(rootDir, modelDefinitions) { if (!modelDefinitions || modelDefinitions.length < 1) { return undefined; @@ -303,22 +284,14 @@ function verifyModelDefinitions(rootDir, modelDefinitions) { var registry = {}; modelDefinitions.forEach(function(definition, idx) { if (definition.sourceFile) { - try { - var fullPath = path.resolve(rootDir, definition.sourceFile); - var basename = path.basename(fullPath, path.extname(fullPath)); - var files = tryReadDir(path.dirname(fullPath)); - var sourceFile = filterValidSourceFiles(files, basename)[0]; - - definition.sourceFile = path.join(path.dirname(fullPath), sourceFile); - definition.sourceFile = require.resolve(definition.sourceFile); - } catch (err) { - debug('Model source code not found: %s - %s', - definition.sourceFile, - err.code || err); - definition.sourceFile = undefined; + var fullPath = path.resolve(rootDir, definition.sourceFile); + definition.sourceFile = fixFileExtension( + fullPath, + tryReadDir(path.dirname(fullPath)), + true); + if (!definition.sourceFile) { + debug('Model source code not found: %s - %s', definition.sourceFile); } - } else { - definition.sourceFile = undefined; } debug('Found model "%s" - %s %s',