Code coverage report for gulp-watch/index.js

Statements: 100% (44 / 44)      Branches: 95.45% (21 / 22)      Functions: 100% (5 / 5)      Lines: 100% (39 / 39)     

All files » gulp-watch/ » 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 832   2                   2 11   10   10 2     8 1 1     8   8   8 1     8 8   8   1 7     1 7   7           7 7   7     8   8 8     8 1     8 8 8   8   1 7 7 7     8    
'use strict';
 
var util = require('gulp-util'),
    PluginError = require('gulp-util').PluginError,
    through = require('through2'),
    batch = require('gulp-batch'),
    File = require('vinyl'),
    getContents = require('./lib/getContents'),
    getStats = require('./lib/getStats'),
    glob2base = require('glob2base'),
    path2glob = require('path2glob');
 
module.exports = function (globs, opts, cb) {
    if (!globs) throw new PluginError('gulp-watch', 'glob argument required');
 
    if (typeof globs === 'string') globs = [ globs ];
 
    if (!Array.isArray(globs)) {
        throw new PluginError('gulp-watch', 'glob should be String or Array, not ' + (typeof globs));
    }
 
    if (typeof opts === 'function') {
        cb = opts;
        opts = {};
    }
 
    if (!opts) opts = {};
 
    var initialStream = through.obj();
 
    if (cb) {
        cb = batch(opts, cb);
    }
 
    var Gaze = require('gaze');
    var gaze = new Gaze(globs);
 
    gaze.on('all', processEvent);
 
    function processEvent(event, filepath) {
        initialStream.write(vinylFromEvent(event, filepath));
    }
 
    function vinylFromEvent(event, filepath) {
        var glob = path2glob(filepath, globs, opts);
 
        var vinyl = new File({
            cwd: opts.cwd || process.cwd(),
            base: opts.base || glob2base(glob),
            path: filepath
        });
 
        log(event, vinyl);
        vinyl.event = event;
 
        return vinyl;
    }
 
    var resultingStream = initialStream.pipe(getStats(opts));
 
    Eif (opts.read !== false) {
        resultingStream = resultingStream.pipe(getContents(opts));
    }
 
    if (cb) {
        resultingStream.on('data', cb);
    }
 
    gaze.on('error', resultingStream.emit.bind(resultingStream, 'error'));
    gaze.on('ready', resultingStream.emit.bind(resultingStream, 'ready'));
    gaze.on('end', resultingStream.emit.bind(resultingStream, 'end'));
 
    resultingStream.close = function () { gaze.close(); };
 
    function log(event, file) {
        var msg = [util.colors.magenta(file.relative), 'was', event];
        if (opts.name) { msg.unshift(util.colors.cyan(opts.name) + ' saw'); }
        util.log.apply(util, msg);
    }
 
    return resultingStream;
};