Code coverage report for tasks/serve.js

Statements: 24.36% (19 / 78)      Branches: 0% (0 / 24)      Functions: 0% (0 / 20)      Lines: 25.68% (19 / 74)      Ignored: none     

All files » tasks/ » serve.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 1201 1 1 1 1 1 1 1 1   1                                                                             1           1                 1                 1                       1                       1           1         1               1      
var Promise = require('es6-promise').Promise;
var path = require('path');
var log = require('./utils/log');
var helper = require('./utils/config-helper');
var Browserify = require('./wrappers/browserify.js');
var browserSync = require('browser-sync').create();
var extend = require('util')._extend;
var build = require('./build');
var config, serve = {};
 
function getWatchOptions(options){
    var files = [],
        htmlPaths = [],
        stylesPaths = [],
        scriptsPaths = [];
    config.buildPaths.forEach(function(pathObj, i){
        htmlPaths.push(path.join(pathObj.source, config.globs.html.replace('*.{','**/*.{')));
        stylesPaths.push(path.join(pathObj.source, config.globs.styles.replace('{.,*}','**').replace('!(_)','')));
        scriptsPaths.push(path.join(pathObj.source, config.globs.scripts.replace('{.,*}','**')));
    });
    var chokidarOptions = {
        persistent: true
    };
    var callBack = function(task){
        return function(event, file){
            switch (event) {
                case 'change':
                    log.info(['Watch: `' + file + '` changed'].join('\n'));
                    options.reload = browserSync.reload;
                    build[task](options).catch(log.onError);
                    break;
                default :
                    if (options.verbose){
                        log.info('   * ' + ((event=='add')?'watch':event) + ' ' + file);
                    }
                    break;
            }
        };
    };
    files.push({ match: htmlPaths, fn: callBack('html'), options: chokidarOptions });
    files.push({ match: stylesPaths, fn: callBack('styles'), options: chokidarOptions });
    if (helper.matches(config.tasks.build, ['browserify'])) {
        browserifyWatch(options);
    } else {
        files.push({ match: scriptsPaths, fn: callBack('scripts'), options: chokidarOptions });
    }
    return files;
}
 
function startBrowserSync(options) {
    log.info(' * Started');
    options.files = getWatchOptions(options);
    browserSync.init(options);
}
 
function browserifyWatch(options){
    config.buildPaths.forEach(function(pathObj, i){
        var src = path.join(pathObj.source, config.globs.scripts);
        pathObj.targets.forEach(function(target){
            new Browserify(src, target, options).watch(browserSync);
        });
    });
}
 
function nodeApp(options){
    var nodemon = require('nodemon');
    return new Promise(function(resolve, reject){
        nodemon(options).on('start', function(e){
            resolve(options);
        });
    });
}
 
serve.adhoc = function (baseDir){
    if (baseDir.split('.').pop() === 'js'){
        return serve.nodeApp({
            script: baseDir
        });
    } else {
        return serve.staticApp({
            server: { baseDir : baseDir }
        });
    }
};
 
serve.staticApp = function(options){
    if (!options.server) {
        var targets = '';
        config.buildPaths.forEach(function(pathObj){ targets += ',' + pathObj.targets.join(',');});
        targets = targets.split(',').filter(function removeDuplicates(elem, pos, self) {
            return self.indexOf(elem) === pos && elem !== '';
        });
        options.server = { baseDir : targets };
    }
    return startBrowserSync(options);
};
 
serve.nodeApp = function(options){
    if (!options.proxy) options.proxy = 'http://localhost:3000';
    if (!options.port) options.port = 3001;
    return nodeApp(options).then(startBrowserSync);
};
 
serve.all = function (options){
    options = extend(config[config.tasks.serve] || {}, options);
    return serve[config.tasks.serve](options);
};
 
function exec(task, options){
    config = helper.getConfig();
    options = options || {};
    if (!config.tasks.serve) return Promise.resolve();
    log.info('Server :');
    return serve[task](options);
}
 
module.exports = {
    adhoc: function(options){ return exec('adhoc', options); },
    all:  function(options){ return exec('all', options); }
};