All files / piscosour/lib globalScullion.js

64% Statements 16/25
33.33% Branches 2/6
100% Functions 0/0
64% Lines 16/25
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    1x 1x   1x   1x 1x 1x 1x 1x   1x           1x 1x                   1x               1x   1x     1x     3x    
'use strict';
 
const fs = require('fs');
const path = require('path');
 
const _ = require('lodash');
 
const chef = require('./chef');
const logger = require('./logger');
const scullion = require('./scullion');
const fsUtils = require('./utils/fsUtils.js');
const versionUtils = require('./utils/versionUtils.js');
 
module.exports = {
  cacheEnabled: false,
  cacheFile() {
    return path.join(chef.getModuleDir(), chef.cacheFile);
  },
  _getRecipes(options) {
    options = options ? options : {isGlobal: true, isTest: false};
    return scullion.cook(chef.getRecipes(options));
  },
  writeCache() {
    const cooked = this._getRecipes();
    logger.trace('#green', 'writing', 'cached file', '#green', this.cacheFile());
    fs.writeFileSync(this.cacheFile(), JSON.stringify(cooked, null, 2));
    return cooked;
  },
  getCookedRecipes(options) {
    let cooked;
    Iif (this.cacheEnabled) {
      if (fsUtils.exists(this.cacheFile())) {
        logger.trace('#green', 'reading', 'cached file', '#green', this.cacheFile());
        cooked = fsUtils.readConfig(this.cacheFile());
      } else {
        cooked = this.writeCache();
      }
    } else {
      cooked = this._getRecipes(options);
    }
    return cooked;
  },
  cook(options) {
    return _.merge(scullion.cook(chef.getLocalRecipes()), this.getCookedRecipes(options));
  },
  get elements() {
    return scullion.elements;
  }
};