All files engine.js

100% Statements 153/153
100% Branches 34/34
100% Functions 12/12
100% Lines 153/153

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 153 1541x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 15x 15x 15x 15x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 3x 10x 4x 4x 3x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 24x 24x 24x 24x 14x 14x 14x 14x 14x 24x 24x 24x 24x 24x 24x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 4x 4x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 1x 1x 1x  
"use strict"
import _ from 'lodash';
import async from 'async';
import cheerio from 'cheerio';
import mkdirp from 'mkdirp';
import f from 'file';
import fs from 'fs';
import functions from './functions.js';
import jazz from 'jazz';
import p from 'path';
import util from 'util';
 
const DIRNAME = p.dirname(import.meta.url).replace('file://', '');
 
/**
 * class Engine
 */
class Engine {
 
  /**
   * @param {String} opts: optional
   * - ext: template file extension, files without this extension will be ignored.
   */
  constructor(opts) {
    opts = opts || {};
    this.ext = opts.ext || 'html';
    this.version = JSON.parse(fs.readFileSync(p.join(DIRNAME, '../package.json'))).version;
  }
 
  /**
   * Compile all template files (identified by file extension) in a directory.
   * This can be done in parallel because compiling a template does not depend on other templates.
   *
   * @param {String} dir: directory containing the templates
   * @param {Function} cb: standard cb(err, result) callback
   */
  compile(dir, cb) {
 
    const tasks = {},
      self = this;
 
    function _task(base, file) {
      const baseFile = p.join(base, file);
      const baseUrlPath = baseFile.replace(/\\/g, '/');
      tasks[baseUrlPath.substr(baseUrlPath.indexOf('/') + 1)] = function (cb) {
        fs.readFile(baseFile, 'utf8', function (err, result) {
          cb(err, jazz.compile(result));
        });
      };
    }
 
    f.walkSync(dir, function (base, dirs, files) {
      files.forEach(function (file) {
        if (file.match(new RegExp('\\.' + self.ext))) {
          _task(base, file);
        }
      });
    });
 
    async.parallel(tasks, cb);
  }
 
  /**
   * Compile all template files (identified by file extension) in a directory.
   * This can be done in parallel because compiling a template does not depend on other templates.
   *
   * @param {String} dir: output directory where website files will be written to
   * @param {Object} templates: compiled jazz templates
   * @param {Object} params: template parameters and functions
   * @param {Function} cb: standard cb(err, result) callback
   */
  merge(dir, templates, params, cb) {
 
    const self = this;
 
    // merge a set of params to a set of templates
    function _process(templates, params, cb) {
      const _templates = _.extend(templates, {}), // process template copies, not the originals
        tasks = {};
 
      _.keys(_templates).forEach(function (key) {
        tasks[key] = function (cb) {
          _templates[key].process(params, function (data) {
            cb(null, data);
          });
        };
      });
 
      async.parallel(tasks, function (err, results) {
        cb(results);
      });
    }
 
    const tasks = {};
 
    _.keys(templates.pages).forEach(function (page) {
      tasks[page] = function (cb) {
 
        let pageParams = _.extend(_.extend({}, params), functions(page, templates, params));
        let pageContent;
 
        function _mergePartials(cb) {
          _process(templates.partials, pageParams, result => {
            pageParams.partials = result;
            pageParams = _.extend(pageParams, functions(page, templates, pageParams));
            _process(templates.partials, pageParams, result => {
              pageParams.partials = result;
              cb();
            });
          });
        }
 
        function _mergePage(cb) {
          _process({ currpage: templates.pages[page] }, pageParams, result => {
            pageParams.content = result.currpage;
            cb();
          });
        }
 
        function _mergeLayout(cb) {
          const layout = (pageParams.sitemap && pageParams.sitemap[page] && pageParams.sitemap[page].layout) ? pageParams.sitemap[page].layout : 'default.html';
          _process({ currlayout: templates.layouts[layout] }, pageParams, result => {
            const $ = cheerio.load(result.currlayout);
            $('head').append(util.format('<meta name="generator" content="AE86 %s" />', self.version));
            pageContent = $.html();
            cb();
          });
        }
 
        function _writePage(cb) {
          // eslint-disable-next-line no-useless-escape
          mkdirp.sync(p.join(dir, page).replace(/(\/[^\/]+$|\\[^\\]+$)/, ''));
          fs.writeFile(p.join(dir, page), pageContent, 'utf8', function (err) {
            if (!err) {
              console.log('+ creating %s', p.join(dir, page));
            }
            cb(err);
          });
        }
 
        async.series([_mergePartials, _mergePage, _mergeLayout, _writePage], cb);
      };
    });
 
    async.parallel(tasks, function (err, results) {
      cb(err, _.keys(results));
    });
  }
}
 
export {
  Engine as default
};