Code coverage report for express-dustjs/index.js

Statements: 100% (48 / 48)      Branches: 85% (17 / 20)      Functions: 100% (11 / 11)      Lines: 100% (48 / 48)      Ignored: none     

All files » express-dustjs/ » index.js
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 1021 1 1 1   1   1         1   1   1 3 3 3 3 2 1 1     1 1     1         1 1     1     3 1     3       1     1   1 1     1 9   9 9     9   9 4 4   4 4       9 2   1       9 9 2     7           1     5      
var Promise = require('core-js-pure/features/promise')
var fs = require('fs')
var path = require('path')
var merge = require('merge')
 
var cache = {}
 
var defaultOptions = {
  cache: true,
  ext: 'dust'
}
 
var defaultDust = require('dustjs-linkedin')
 
var dust = defaultDust
 
function readContentFromPaths (paths, name, ext) {
  return new Promise(function (resolve, reject) {
    var _path = paths.shift()
    fs.readFile(path.resolve(_path, name) + ext, function (err, content) {
      if (err) {
        if (paths.length) {
          resolve(readContentFromPaths(paths, name, ext))
          return
        }
 
        reject(err)
        return
      }
 
      resolve(content.toString())
    })
  })
}
 
module.exports = function (dustjs) {
  return module.exports.bind(dustjs)
}
 
Object.defineProperty(module.exports, 'bind', {
  enumerable: true,
  value: function (dustjs) {
    if (typeof dustjs !== 'object' || dustjs == null) {
      dust = require('dustjs-linkedin')
    }
 
    return module.exports
  }
})
 
Object.defineProperty(module.exports, 'engine', {
  emuerable: true,
  value: function (opts) {
    var dustOptions = merge.recursive(true, defaultOptions, opts)
 
    Eif (dustOptions.useHelpers) {
      require('dustjs-helpers')
    }
 
    return function (file, options, callback) {
      var compiler
 
      Eif (dustOptions.cache) {
        compiler = cache[file]
      }
 
      var ext = '.' + (options.ext || dustOptions.ext)
 
      if (typeof compiler !== 'function') {
        var content = fs.readFileSync(file).toString()
        compiler = dust.compileFn(content)
 
        Eif (dustOptions.cache) {
          cache[file] = compiler
        }
      }
 
      dust.onLoad = function (name, callback) {
        readContentFromPaths([].concat(options.settings.views), name, ext)
          .then(function (content) {
            callback(null, content)
          }, callback)
      }
 
      compiler(options, function (err, content) {
        if (err) {
          return callback(err)
        }
 
        callback(null, content)
      })
    }
  }
})
 
Object.defineProperty(module.exports, '_', {
  enumerable: true,
  get: function () {
    return dust
  }
})