Code coverage report for lib/processManifest.js

Statements: 100% (25 / 25)      Branches: 100% (13 / 13)      Functions: 100% (6 / 6)      Lines: 100% (25 / 25)      Ignored: none     

All files » lib/ » processManifest.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  1 1 1               1 6           6 2 2   6   6 5       5 5 1         4                         4 123 12                 4 29 27 23     29     4   1      
'use strict';
var _        = require('lodash');
var traverse = require('traverse');
var obj      = require('object-path');
 
/**
 * processManifest
 *
 * @param {Object} json
 * @return {Object}
 */
module.exports = function (json) {
  var defaults = {
    paths: {
      source: 'assets/',
      dist: 'dist/'
    }
  };
  var err = function (msg) {
    msg = msg || 'file seems to be malformed';
    throw new Error('Manifest File Error: ' + msg);
  };
  var required = ['dependencies'];
 
  if(_.isPlainObject(json)) {
    json = _.defaults(json, defaults);
 
 
    // check to see if the JSON data has the minimum needed properties 
    _.forEach(required, function (req) {
      if(!obj.has(json, req)) {
        err('missing "'+req+'" property');
      }
    });
 
    // add fonts property if it doesn't exist already
    json.dependencies = _.merge({
      fonts: {
        files: ['fonts/**/*']
      },
      images: {
        files: ['images/**/*']
      }
    }, json.dependencies);
 
    // users can specify files as either
    // * an array of file paths
    // * a string with a single file path
    // this function converts all strings to arrays
    traverse(json.dependencies).forEach(function (node) {
      if(this.isLeaf && !_.isArray(node) && !_.isArray(this.parent.node)) {
        this.update([node]);
      }
    });
 
    // users can specify their file paths as 
    // * "path/to/file.js"
    // and it will be processed into
    // * "assets/path/to/file.js"
    // users can set the "external" property to true to not append the dir
    _.forOwn(json.dependencies, function (dependency, name) {
      if(!dependency.external) {
        json.dependencies[name].files = _.map(dependency.files, function (file) {
          return json.paths.source + file;
        });
      }
      return dependency;
    });
 
    return json;
  } else {
    err();
  }
};