All files / piscosour/lib scullion.js

64.91% Statements 37/57
36.36% Branches 8/22
50% Functions 6/12
68.52% Lines 37/54
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    1x 1x   1x 1x   1x   1x             1x 3x 3x 3x 2x   1x         3x     1x 12x 12x                   12x     1x   12x                       12x           12x 12x             12x 12x   12x     1x 2x 2x 3x 3x   3x   3x 3x 12x         2x                         1x     3x      
'use strict';
 
const fs = require('fs');
const path = require('path');
 
const fsUtils = require('./utils/fsUtils.js');
const logger = require('./logger');
 
const fileName = 'piscosour';
 
const _elements = [
  'plugins',
  'steps',
  'flows',
  'contexts'
];
 
const requireConfig = (moduleName) => {
  let config = undefined;
  try {
    config = require(moduleName);
    logger.trace('reading:', '#green', moduleName);
  } catch (err) {
    Iif (err.code !== 'MODULE_NOT_FOUND') {
      logger.warn(`error reading ${moduleName} =>`, '#red', err);
      throw err;
    }
  } //eslint-disable-line no-empty
  return config;
};
 
const loadConfigs = (rootDir) => {
  const configs = [];
  Iif (fsUtils.exists(rootDir)) {
    fs.readdirSync(rootDir).forEach((dir) => {
      const config = requireConfig(path.join(rootDir, dir, 'config'));
      if (config) {
        config._module = path.join(rootDir, dir);
        configs.push(config);
      }
    });
  }
 
  return configs;
};
 
const loadElements = (recipe, element) => {
 
  const _loadSteps = (config) => {
    let contexts = config.contexts;
    if (!contexts) {
      logger.warn('#yellow', 'step', '#bold', config.name, '#yellow', 'from recipe', '#bold', recipe.name, '#yellow', 'is not available: no contexts declared');
    } else {
      if (contexts === '*') {
        contexts = [ 'default' ];
      }
      recipe[element][config.name] = recipe[element][config.name] || {};
      contexts.forEach(context => recipe[element][config.name][context] = config);
    }
  };
  const _loadFlows = (config) => {
    recipe[element][config.name] = config;
    if (!recipe[element][config.name].type) {
      recipe[element][config.name].type = 'normal';
    }
  };
  const _loadOther = config => recipe[element][config.name] = config;
  const _load = {
    steps: _loadSteps,
    flows: _loadFlows,
    plugins: _loadOther,
    contexts: _loadOther
  };
 
  recipe[element] = recipe[element] || {};
  loadConfigs(path.join(recipe.dir, element)).forEach(config => _load[element](config));
 
  return recipe;
};
 
const cook = function(recipes) {
  logger.trace('#magenta', 'Scullion is cooking all recipes');
  Object.getOwnPropertyNames(recipes).forEach((name) => {
    let recipe = recipes[name];
    logger.trace('cooking:', '#green', recipe.name ? recipe.name : name, 'dir:', recipe.dir);
 
    recipe.config = requireConfig(path.join(recipe.dir, fileName)) || {};
 
    Eif (recipe.name) {
      _elements.forEach((element) => {
        recipe = loadElements(recipe, element);
      });
    }
  });
 
  return {
    recipes: recipes
  };
};
 
/**
 * **Internal:**
 *
 * Prepare the config object to be use by the config module.
 *
 * @returns {{recipes, rootDir}}
 * @module scullion
 */
module.exports = {
  cook: cook,
  get elements() {
    return _elements;
  }
};