Code coverage report for lib/builder.js

Statements: 64.42% (67 / 104)      Branches: 58.06% (36 / 62)      Functions: 68.42% (13 / 19)      Lines: 64.42% (67 / 104)     

All files » lib/ » builder.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 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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197          1                       1           1   1 1   1 1 1     1 1 1   1 1   1       1 1   1           1     1 1   1   1         1 1 1           1     1 13 13         13 13 13 13 13       14 1 1 1 1   13           13       13                 13                                                                 13       13 17 17     13 21 21 12   21 21 21       13 13 13 1 1 1 1 1 1 1             1 1     12            
/*
Copyright (c) 2012, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
http://yuilibrary.com/license/
*/
var path = require('path'),
    log = require('./log'),
    noop = function () {},
    util = require('./util'),
    shifter = require('./index'),
    timer = require('timethat'),
    spawn = require('child_process').spawn,
    Stack = require('./stack').Stack,
    pack = require('./pack'),
    fs = require('fs'),
    smodule = require('./module');
 
var has = function (opt, name) {
    return opt.some(function (v) {
        return (v === name);
    });
};
 
exports.reset = smodule.reset;
 
var prebuild = function (jobs, options, callback) {
    var stack = new Stack();
 
    jobs.forEach(function (job) {
        log.info('shifting build for: ' + job);
        var args = [],
            child;
 
        Eif (options['build-dir']) {
            args.push('--build-dir');
            args.push(options['build-dir']);
        }
        Eif (options['global-config'] === false) {
            args.push('--no-global-config');
        }
        Iif (options.lint) {
            args.push('--lint');
            args.push(options.lint);
        }
        Eif (options.lint === false) {
            args.push('--no-lint');
        }
        Iif (options.uglify) {
            args.push('--uglify');
            if (options.semi === false) {
                args.push('--no-semi');
            }
        }
        Iif (options.coverage === false) {
            args.push('--no-coverage');
        }
        Eif (options.istanbul) {
            args.push('--istanbul');
        }
        args.unshift(util.shifter);
 
        child = spawn(process.execPath, args, {
            cwd: path.join(shifter.cwd(), '../', job),
            stdio: 'inherit'
        });
 
        child.on('exit', stack.add(function (code) {
            log.info('build for ' + job + ' complete, downshifting');
            Iif (code) {
                log.error('prebuild ' + job + ' failed, exited with code ' + code + ', hitting the brakes, fix it and try again!');
            }
        }));
    });
 
    stack.done(callback);
};
 
exports.start = function (json, options, buildCallback) {
    log.info('putting the hammer down');
    var stack = new Stack(),
        start = new Date(),
        hasSkin = false,
        buildStat,
        end = function () {
            var end = new Date();
            log.info('done racing, the gears are toast');
            log.info('finished in ' + timer.calc(start, end) + ', pretty fast huh?');
            Eif (buildCallback) {
                buildCallback();
            }
        },
        post = function (json, callback) {
            if (json.postbuilds && options.exec) {
                log.info('found a postbuild, shifting it');
                prebuild(json.postbuilds, options, function () {
                    delete json.postbuilds;
                    post(json, callback);
                });
            } else Iif (json.postexec && options.exec) {
                smodule.exec(json.postexec, function () {
                    delete json.postexec;
                    post(json, callback);
                });
            } else {
                callback();
            }
        };
 
    Iif (json.prebuilds && options.exec) {
        log.info('found a prebuild, shifting it');
        prebuild(json.prebuilds, options, function () {
            delete json.prebuilds;
            exports.start(json, options);
        });
        return;
    }
 
    Iif (json.exec && options.exec) {
        log.info('found a global exec, shifting it');
        buildStat = fs.statSync(options.buildFile);
        smodule.exec(json.exec, function () {
            delete json.exec;
            var json2,
                buildStat2 = fs.statSync(options.buildFile);
            if (buildStat2.mtime > buildStat.mtime) {
                log.warn('build.json has changed after exec, reloading..');
                try {
                    //Not using `require` here because it will be cached and we need a new copy
                    json2 = JSON.parse(fs.readFileSync(options.buildFile, 'utf8'));
                } catch (e) {
                    console.log(e.stack);
                    log.error('hitting the brakes! failed to parse ' + options.buildFileName + ', syntax error?');
                }
 
                if (pack.valid(json2)) {
                    pack.munge(json2, options, function (json2, options) {
                        delete json2.exec;
                        delete json2.prebuilds;
                        exports.start(json2, options);
                    });
                } else {
                    log.error('hitting the brakes, your ' + buildFileName + ' file is invalid, please fix it!');
                }
            } else {
                exports.start(json, options);
            }
        });
        return;
    }
 
    Iif (options.modules) {
        log.info('limiting to: ' + options.modules);
    }
 
    hasSkin = Object.keys(json.builds).some(function (name) {
        var mod = json.builds[name];
        return (mod.skinnable || (mod.config && mod.config.skinnable));
    });
 
    Object.keys(json.builds).forEach(function (name) {
        var mod = json.builds[name];
        if (!mod.skinnable && !(mod.config && mod.config.skinnable) && mod.assets !== false) {
            mod.assets = !hasSkin;
        }
        Eif (!options.modules || has(options.modules, name)) {
            log.info('shifting into gear for ' + name);
            smodule.build(mod, name, options, stack.add(noop));
        }
    });
 
    stack.done(function () {
        var rollups = [];
        if (json.rollups) {
            log.info('build has rollup builds, shifting them now');
            Object.keys(json.rollups).forEach(function (name) {
                var mod = json.rollups[name];
                Eif (!options.modules || has(options.modules, name)) {
                    log.info('shifting into gear for ' + name);
                    mod.buildDir = options['build-dir'];
                    rollups.push({
                        mod: mod,
                        name: name,
                        options: options
                    });
                }
            });
            smodule.rollup(rollups, function () {
                post(json, end);
            });
        } else {
            post(json, end);
        }
 
    });
};