all files / express-stormpath/lib/helpers/ get-app-module-version.js

77.78% Statements 21/27
40% Branches 4/10
100% Functions 2/2
77.78% Lines 21/27
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61                                                                               
'use strict';
 
var fs = require('fs');
var path = require('path');
 
var cachedModulePaths = {};
var globalModulesPath = process.env.NODE_PATH;
var appFilePath = require.main.filename;
var appModulesPath = path.join(path.dirname(fs.realpathSync(appFilePath)), 'node_modules');
 
function getApplicationModulePath(name) {
  Iif (name in cachedModulePaths) {
    return cachedModulePaths[name];
  }
 
  var result = false;
 
  var modulePaths = [
    path.join(appModulesPath, name)
  ];
 
  Eif (globalModulesPath) {
    modulePaths.push(path.join(globalModulesPath, name));
  }
 
  while (modulePaths.length) {
    var modulePath = modulePaths.shift();
    Iif (fs.existsSync(modulePath)) {
      result = modulePath;
      break;
    }
  }
 
  cachedModulePaths[name] = result;
 
  return result;
}
 
/**
 * Gets the version of a module loaded at application level (process).
 *
 * @method
 * @private
 *
 * @param {string} name - Name of the module that you want to resolve the version for.
 *
 * @return {string} Module version, or false if the module couldn't be found.
 */
module.exports = function getApplicationModuleVersion(name) {
  var modulePath = getApplicationModulePath(name);
 
  Iif (modulePath) {
    var loadedModule = require(path.join(modulePath, 'package.json'));
    if (loadedModule) {
      return loadedModule.version;
    }
  }
 
  return false;
};