Code coverage report for gulp-watch/index.js

Statements: 95.92% (47 / 49)      Branches: 85.71% (24 / 28)      Functions: 100% (8 / 8)      Lines: 100% (46 / 46)     

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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 973   3             1 23 23 23 30   23     3 34 23 23     34   34 1     33   33 33   33 19 19 19     33   33 32 32     33   33 23   23         23     33 5     1   13 13 13   13             13   13   13         33 16 16     33   33    
'use strict';
 
var Duplex = require('stream').Duplex,
    batch = require('gulp-batch'),
    Gaze = require('gaze'),
    gulp = require('gulp'),
    path = require('path'),
    gutil = require('gulp-util');
 
function getWatchedFiles(gaze) {
    var files = [];
    var watched = gaze.watched();
    Object.keys(watched).forEach(function (dir) {
        files = files.concat(watched[dir]);
    });
    return files;
}
 
module.exports = function (opts, cb) {
    if (typeof opts !== 'object') {
        cb = opts;
        opts = { };
    }
 
    opts.emit = opts.emit || 'one';
 
    if (cb && typeof cb !== 'function') {
        throw new Error('Provided callback is not a function: ' + cb);
    }
 
    var pathMap = {};
 
    var duplex = new Duplex({ objectMode: true, allowHalfOpen: true });
    duplex.gaze = new Gaze(opts.glob);
 
    duplex._write = function _write(file, encoding, done) {
        pathMap[file.path] = { cwd: file.cwd, base: file.base };
        duplex.gaze.add(file.path, done.bind(null, null));
        if (opts.passThrough !== false) { duplex.push(file); }
    };
 
    duplex._read = function _read() { };
 
    duplex.close = function () {
        duplex.gaze.on('end', duplex.emit.bind(duplex, 'end'));
        duplex.gaze.close();
    };
 
    duplex.gaze.on('error', duplex.emit.bind(duplex, 'error'));
 
    duplex.on('finish', function () {
        var count = getWatchedFiles(duplex.gaze).length;
 
        gutil.log(
            (opts.name ? gutil.colors.cyan(opts.name) + ' is watching': 'Watching'),
            gutil.colors.cyan(count),
            (count === 1 ? 'file...' : 'files...'));
 
        process.nextTick(duplex.emit.bind(duplex, 'ready'));
    });
 
    if (opts.glob) {
        process.nextTick(duplex.end.bind(duplex));
    }
 
    function createFile(done, event, filepath) {
 
        var msg = [gutil.colors.magenta(path.basename(filepath)), 'was', event];
        Iif (opts.name) { msg.unshift(gutil.colors.cyan(opts.name) + ' saw'); }
        gutil.log.apply(gutil, msg);
 
        var options = {
            buffer: opts.buffer,
            read: opts.read,
            cwd: pathMap[filepath] ? pathMap[filepath].cwd : undefined,
            base: pathMap[filepath] ? pathMap[filepath].base : undefined
        };
 
        var glob = [filepath];
 
        Iif (opts.emit === 'all') { glob = getWatchedFiles(duplex.gaze); }
 
        gulp.src(glob, options)
            .on('data', done)
            .on('error', duplex.emit.bind(duplex, 'error'));
    }
 
    if (cb) {
        cb = batch(opts, cb.bind(duplex));
        cb.domain.on('error', duplex.emit.bind(duplex, 'error'));
    }
 
    duplex.gaze.on('all', createFile.bind(null, cb || duplex.push.bind(duplex)));
 
    return duplex;
};