Code coverage report for lib/index.js

Statements: 76.42% (81 / 106)      Branches: 60% (36 / 60)      Functions: 85.71% (12 / 14)      Lines: 76.42% (81 / 106)     

All files » lib/ » 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 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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217          1                                           1 121     1 6 6 6 3 3 3 3 3           1 3       3     1 13         13   13 13     13 13     13 13   13   13 13 13   13     13         13 3 3 3 3   3 3 11 11 11       11 1               13           13 1     13 6     13 13 13     13 13 13 13     13 13 11 11         11 11 11 11                 11 11 11 11 11 11               2   2   3 3           2 2         2   2 2 2     2 2 2                                              
/*
Copyright (c) 2012, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
http://yuilibrary.com/license/
*/
var log = require('./log'),
    fs = require('fs'),
    vm = require('vm'),
    path = require('path'),
    pack = require('./pack'),
    args = require('./args'),
    util = require('./util'),
    find = util.find,
    CWD = process.cwd(),
    queue = [],
    buildRunning = false,
    exists = util.exists,
    contextForRunInContext = vm.createContext({
        require: require,
        module: require('module'),
        console: {
            log: function () {}
        },
        window: {},
        document: {}
    });
 
exports.cwd = function() {
    return CWD;
};
 
var runQueue = function() {
    Eif (!buildRunning) {
        var item = queue.pop();
        if (item) {
            buildRunning = true;
            exports.init(item.opts, function() {
                buildRunning = false;
                item.callback();
                runQueue();
            });
        }
    }
};
 
exports.add = function(opts, callback) {
    queue.push({
        opts: opts,
        callback: callback
    });
    runQueue();
};
 
exports.init = function (opts, initCallback) {
    var options = args.defaults(opts),
        watch,
        buildFile = options.config,
        buildFileName;
 
    log.reset(options);
 
    Eif (options.cwd) {
        CWD = options.cwd;
    }
 
    Eif (!buildFile) {
        buildFile = options['yui-module'] ? options['yui-module'] : path.join(CWD, 'build.json');
    }
 
    globalBuildDir = false;
    buildRunning = true;
 
    buildFileName = path.basename(buildFile);
 
    options.buildFile = buildFile;
    options.buildFileName = buildFileName;
    Eif (!options.recursive) {
        // The build-dir should not be relative to cwd if running recursively.
        options['build-dir'] = path.resolve(exports.cwd(), options['build-dir']);
    }
 
    Iif (options.version || options.help) {
        require('./help');
        return;
    }
 
    if (options['global-config']) {
        log.info('racing to find the closest .shifter.json file');
        find(CWD, '.shifter.json', function(err, file) {
            Eif (file) {
                log.info('woohoo, found a config here: ' + file);
 
                var json = JSON.parse(fs.readFileSync(file, 'utf8'));
                Object.keys(json).forEach(function(key) {
                    Eif (!args.has(key)) {
                        log.info('override config found for ' + key);
                        options[key] = json[key];
 
                        // Special case to determine whether we should resolve
                        // the build directory relative to the global config.
                        if (key === 'build-dir') {
                            options['build-dir'] = path.resolve(path.dirname(file), json[key]);
                        }
                    }
                });
            }
        });
    }
 
    Iif (options.watch) {
        watch = require('./watch');
        watch.start(options);
        return;
    }
 
    if (options.quiet) {
        log.quiet();
    }
 
    if (options.silent) {
        log.silent();
    }
 
    log.info('revving up');
    Eif (!options.walk) {
        log.info('looking for ' + buildFileName + ' file');
    }
 
    exists(buildFile, function (yes) {
        var json, walk, ant, mods, builder;
        Eif (yes) {
            Iif (options.ant) {
                log.error('already has a ' + buildFileName + ' file, hitting the brakes');
            }
            log.info('found ' + buildFileName + ' file, shifting');
            if (path.extname(buildFileName) === '.json') {
                try {
                    json = require(buildFile);
                } catch (e) {
                    console.log(e.stack);
                    log.error('hitting the brakes! failed to parse ' + buildFileName + ', syntax error?');
                }
                Eif (pack.valid(json)) {
                    log.info('putting the hammer down, let\'s build this thing!');
                    pack.munge(json, options, function (json, options) {
                        Iif (options.list) {
                            mods = Object.keys(json.builds).sort();
                            log.info('This module includes these builds:');
                            console.log(mods.join(', '));
                            if (json.rollups) {
                                log.info('and these rollups');
                                console.log(Object.keys(json.rollups).join(', '));
                            }
                        } else {
                            builder = require('./builder');
                            builder.reset();
                            builder.start(json, options, function() {
                                buildRunning = false;
                                Eif (initCallback) {
                                    initCallback();
                                }
                            });
                        }
                    });
                } else {
                    log.error('hitting the brakes, your ' + buildFileName + ' file is invalid, please fix it!');
                }
            } else Eif (path.extname(buildFileName) === '.js') {
                // probably a row module
                contextForRunInContext.YUI = {
                    add: function (name, fn, version, config) {
                        mods = mods || {};
                        mods[name] = {
                            yuifile: buildFile,
                            config: config || {}
                        };
                    }
                };
                try {
                    vm.runInContext(fs.readFileSync(buildFile, 'utf8'),
                            contextForRunInContext, buildFile);
                } catch (e) {
                    log.error('hitting the brakes, your ' + buildFileName + ' file is invalid, please fix it!');
                }
                Eif (mods) {
                    // raw yui module without build.json
                    builder = require('./builder');
                    builder.reset();
                    builder.start({
                        builds: mods
                    }, options, function() {
                        buildRunning = false;
                        Eif (initCallback) {
                            initCallback();
                        }
                    });
                }
            } else {
                log.error('hitting the brakes, your ' + buildFileName + ' file is invalid, please fix it!');
            }
        } else {
            if (options.walk) {
                walk = require('./walk');
                walk.run(options, initCallback);
            } else {
                log.warn('no ' + buildFileName + ' file, downshifting to convert ant files');
                ant = require('./ant');
                ant.process(options, function () {
                    if (!options.ant) {
                        exports.init(options, initCallback);
                    }
                });
            }
        }
    });
};