Code coverage report for asset-builder/lib/Manifest.js

Statements: 100% (24 / 24)      Branches: 100% (2 / 2)      Functions: 100% (5 / 5)      Lines: 100% (24 / 24)      Ignored: none     

All files » asset-builder/lib/ » Manifest.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            1 1 1 1 1 1                             1 2 2     2 2 2 2                     1 1                 1 2                                                               1 2 3 3 2   3 3      
/**
 * Manifest
 * @module
 */
'use strict';
 
var _               = require('lodash');
var readManifest    = require('./readManifest');
var processManifest = require('./processManifest');
var buildGlobs      = require('./buildGlobs');
var Dependency      = require('./Dependency');
var mainBowerFiles  = require('main-bower-files');
 
/**
 * Manifest
 *
 * @class
 * @param {String} path File path to the manifest JSON file
 * @param {Object} options
 * @property {Object} config the original config from the manifest JSON file
 * @property {Object} dependencies the original dependencies property from the manifest JSON file
 * @property {Object} globs [see buildGlobs.globs property]{@link module:lib/buildGlobs~buildGlobs}
 * @property {Object} paths
 * @property {Object} paths.source the path to the asset source directory. Includes a trailing slash.
 * @property {Object} paths.dist path to the build directory. Includes a trailing slash.
 */
var Manifest = module.exports = function(path, options) {
  var manifest = processManifest(readManifest(path));
  var bower = mainBowerFiles(_.pick(options, [
    'paths'
  ]));
  this.config = manifest.config;
  this.dependencies = manifest.dependencies;
  this.globs = new buildGlobs(manifest.dependencies, bower, options).globs;
  this.paths = manifest.paths;
};
 
/**
 * forEachDependency
 * loops through the dependencies of the specified type and calls the callback for each dependency.
 *
 * @param {String} type The type of dependencies you want to loop through. These can be
 * @param {Function} callback Callback called per iteration with the arguments (value, index|key, collection)
 * @see {@link https://lodash.com/docs#forEach}
 */
Manifest.prototype.forEachDependency = function(type, callback) {
  _.forEach(this.globs[type], callback);
};
 
/**
 * getDependency
 *
 * @param {String} name
 * @return {Object}
 */
Manifest.prototype.getDependency = function(name) {
  return _.find([].concat(this.globs.js, this.globs.css), {name: name});
};
 
/**
 * getProjectGlobs
 * gets
 *
 * @return {Object} returns an object with properties corresponding to
 * @example
 * // if you had a manifest with a dependencies property that looked like:
 * {
 *   "app.js": {
 *     files: [ "scripts/main.js" ],
 *     vendor: [ "vendor/hello-world.js" ]
 *   }
 *   "app.js": {
 *     files: [ "styles/main.css" ],
 *     vendor: [ "vendor/hello-world.css" ]
 *   }
 * }
 * // then
 * manifest.getProjectGlobs();
 * // will output
 * {
 *   "js": [
 *     "scripts/main.js"
 *   ],
 *   "css": [
 *     "styles/main.css"
 *   ]
 * }
 */
Manifest.prototype.getProjectGlobs = function() {
  return _.reduce(this.dependencies, function(result, dep, key) {
    var type = Dependency.parseType(key);
    if (!_.isArray(result[type])) {
      result[type] = [];
    }
    result[type] = result[type].concat(dep.files);
    return result;
  }, {});
};