Code coverage report for lib/pack.js

Statements: 100% (53 / 53)      Branches: 100% (20 / 20)      Functions: 100% (12 / 12)      Lines: 100% (53 / 53)     

All files » lib/ » pack.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 102 103 104              1             1   1 11       1 32 46 92 92 2 2 2 14         32 46 46 46     32     1 18 18 18 30   18     1 13   13 13 10 10 18 17 17 17   1 1 1   16 40 16     16 2 3 2             3     13 20     13 3 6 4 4         13        
/*
Copyright (c) 2012, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
http://yuilibrary.com/license/
*/
//We are looping data, no need for this
/*jshint forin: false */
var path = require('path'),
    fs = require('fs'),
    log = require('./log'),
    args = require('./args'),
    shifter = require('./index'),
    exists = require('./util').exists;
 
exports.bad = 'BAD_FILE_REMOVE_WHEN_FIXED';
 
exports.valid = function (json) {
    return !(json[exports.bad]);
};
 
/*jshint forin: false */
var flatten = function (json) {
    var walker = function (i) {
        return function (l) {
            var m, o;
            if (json[i][l]) {
                m = flatten(json[i][l]);
                delete json[i][l];
                for (o in m) {
                    json[o] = flatten(m[o]);
                }
            }
        };
    }, item;
    for (item in json) {
        ['submodules', 'plugins'].forEach(walker(item));
        delete json[item].condition;
        delete json.condition;
    }
 
    return json;
};
 
var mix = function (s, r) {
    var i;
    r = r || {};
    for (i in s) {
        r[i] = s[i];
    }
    return r;
};
 
exports.munge = function (json, options, callback) {
    var meta = path.join(shifter.cwd(), 'meta');
 
    exists(meta, function (yes) {
        if (yes) {
            var files = fs.readdirSync(meta), mod;
            files.forEach(function (file) {
                if (path.extname(file) === '.json') {
                    log.info('munging in loader meta data into build.json');
                    try {
                        mod = flatten(require(path.join(meta, file)));
                    } catch (e) {
                        console.log(e.stack);
                        log.error('hitting the brakes! failed to parse ' + file + ', syntax error?');
                        return;
                    }
                    Object.keys(json.builds).forEach(function (name) {
                        if (mod[name]) {
                            json.builds[name].config = mix(mod[name], json.builds[name].config);
                        }
                    });
                    if (json.rollups) {
                        Object.keys(json.rollups).forEach(function (name) {
                            if (mod[name]) {
                                json.rollups[name].config = mix(mod[name], json.rollups[name].config);
                            }
                        });
                    }
                }
            });
        } else {
            log.warn('down shifting, can\'t find a meta directory');
        }
        //Cleanup..
        Object.keys(json.builds).forEach(function (name) {
            json.builds[name].config = json.builds[name].config || {};
        });
 
        if (json.shifter) {
            Object.keys(json.shifter).forEach(function (a) {
                if (!args.has(a)) {
                    log.info('override config found for ' + a);
                    options[a] = json.shifter[a];
                }
            });
        }
 
        callback(json, options);
    });
 
};