All files / piscosour/lib chef.js

85.14% Statements 63/74
68.75% Branches 44/64
90.91% Functions 10/11
84.72% Lines 61/72
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152    1x 1x 1x 1x 1x 1x 1x 1x 1x     1x   1x   2x     2x   2x     1x 104x         1x 104x 104x   104x 13x     13x     104x 30x   104x 13x                           13x 7x         1x 1x         1x 1x 1x 1x             1x     1x 2x 2x 2x 2x 2x 2x               2x 2x 2x 2x 2x   2x   2x     2x           2x 24x   2x 54x   2x 1x 1x 26x         2x     1x                                   1x
'use strict';
 
const path = require('path');
const fs = require('fs');
const logger = require('./logger');
const fsUtils = require('./utils/fsUtils');
const constants = require('./utils/constants');
const versionUtils = require('./utils/versionUtils');
const pwd = process.cwd();
const home = require('os').homedir();
const semver = require('semver');
let toolVersion;
 
const chef = function() {
 
  const _getModuleDir = function(isTest) {
    let dir;
    Iif (isTest) {
      dir = process.cwd();
    } else {
      dir = path.parse(path.parse(process.mainModule.filename).dir).dir;
    }
    return dir;
  };
 
  const toolRangeVersion = (pkgModule) => {
    return pkgModule.devDependencies && pkgModule.devDependencies[constants.toolName] ?
      pkgModule.devDependencies[constants.toolName] : pkgModule.dependencies &&
      pkgModule.dependencies[constants.toolName] ? pkgModule.dependencies[constants.toolName] : '';
  };
 
  const addRecipe = (recipes, rootDir, isGlobal, isLocal) => {
    const pkgModule = fsUtils.readConfig(path.join(rootDir, constants.npmFile));
    const pkgToolRangeVersion = toolRangeVersion(pkgModule);
 
    const satisfies = (version, range) => {
      Iif (version && version.indexOf('-') >= 0) {
        version = version.split('-')[0];
      }
      return semver.satisfies(version, range);
    };
 
    if (pkgModule.name) {
      logger.trace('processing:', '#green', pkgModule.name, constants.toolName,'version ->', pkgToolRangeVersion);
    }
    if (pkgModule.keywords && pkgModule.keywords.indexOf(constants.toolKeyword) >= 0 && recipes.module.name !== pkgModule.name) {
      Iif ((pkgModule.name === constants.toolName && isLocal || satisfies(toolVersion, pkgToolRangeVersion)) && !recipes[pkgModule.name]) {
        logger.trace(pkgModule.name, '- recipe detected! - version:', pkgModule.version);
        recipes[pkgModule.name] = {
          name: pkgModule.name,
          version: pkgModule.version,
          description: pkgModule.description,
          dir: rootDir
        };
        if (isGlobal && pkgModule.dependencies !== undefined) {
          Object.getOwnPropertyNames(pkgModule.dependencies).forEach((name) => {
            addRecipe(recipes, path.join(rootDir.substring(0, rootDir.lastIndexOf(path.sep)), name), isGlobal, isLocal);
            addRecipe(recipes, path.join(rootDir, 'node_modules', name), isGlobal, isLocal);
          });
        }
      } else if (pkgModule.dependencies && pkgModule.dependencies[constants.toolName] && !semver.satisfies(toolVersion, pkgToolRangeVersion)) {
        logger[isLocal ? 'error' : 'trace'](isLocal ? '#red' : '#yellow', isLocal ? 'ERROR' : 'WARNING:', '#green', pkgModule.name, constants.toolName, 'version ->', pkgToolRangeVersion, 'is not compatible with:', toolVersion);
      }
    }
  };
 
  const getLocalRecipes = function() {
    const recipes = {
      configLocal: {
        dir: path.join(pwd, constants.toolLocalDir)
      }
    };
    let file = path.join(pwd, constants.toolLocalDir, constants.toolFile);
    Eif (fsUtils.exists(file)) {
      logger.trace('detecting:', '#green', file);
      recipes.configLocal = {
        name: `${constants.toolName}-local`,
        description: `Local ${constants.toolName} Recipe`,
        version: '-',
        dir: path.join(pwd, constants.toolLocalDir)
      };
    }
    return recipes;
  };
 
  const getRecipes = function(options) {
    const rootDir = _getModuleDir(options.isTest);
    const recipes = {};
    let file = path.join(home, constants.toolLocalDir, constants.toolFile);
    Eif (fsUtils.exists(file)) {
      logger.trace('detecting:', '#green', file);
      recipes.userConfig = {
        name: `${constants.toolName}-user`,
        description: `User ${constants.toolName} Recipe`,
        version: '-',
        dir: path.join(home, constants.toolLocalDir)
      };
    }
 
    file = path.join(rootDir, constants.npmFile);
    Eif (fsUtils.exists(file)) {
      logger.trace('detecting:', '#green', file, '#magenta', 'getting all sons...');
      const pkg = fsUtils.readConfig(file);
      Iif (pkg.name === constants.toolName) {
        toolVersion = pkg.version;
      } else Iif (pkg.dependencies && pkg.dependencies[constants.toolName]) {
        toolVersion = versionUtils.getVersion(pkg.dependencies[constants.toolName]);
      } else Iif (pkg.devDependencies && pkg.devDependencies[constants.toolName]) {
        toolVersion = versionUtils.getVersion(pkg.devDependencies[constants.toolName]);
      }
      recipes.module = {
        name: pkg.name,
        version: pkg.version,
        description: pkg.description,
        dir: rootDir
      };
      Eif (pkg.dependencies) {
        Object.getOwnPropertyNames(pkg.dependencies).forEach((name) => addRecipe(recipes, path.join(rootDir, 'node_modules', name), options.isGlobal, true));
      }
      Eif (pkg.devDependencies) {
        Object.getOwnPropertyNames(pkg.devDependencies).forEach((name) => addRecipe(recipes, path.join(rootDir, 'node_modules', name), options.isGlobal, true));
      }
      if (options.isGlobal) {
        const dir = require('global-modules');
        fs.readdirSync(dir).forEach((subdir) => {
          addRecipe(recipes, path.join(dir, subdir), options.isGlobal);
        });
      }
    }
 
    return recipes;
  };
 
  return {
    getRecipes: getRecipes,
    getLocalRecipes: getLocalRecipes,
    getModuleDir: _getModuleDir,
    cacheFile: 'scullion.json'
  };
};
 
/**
 * **Internal:**
 *
 * Read all toolFile.json files of every recipes imported in one module and prepare one json object with all the information.
 *
 * This module is used only in module **config.js**
 *
 * @returns {{getRecipes: getRecipes}}
 * @module Chef
 */
module.exports = chef();