Code coverage report for gulp-watch/index.js

Statements: 97.56% (40 / 41)      Branches: 87.5% (21 / 24)      Functions: 100% (7 / 7)      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 83 84 853   3             3 34 23 23     34 1     33   33 33   33 19 19 19     33   33 32 32     33   33 23 23 30     23         23     33 5     1   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');
 
module.exports = function (opts, cb) {
    if (typeof opts !== 'object') {
        cb = opts;
        opts = { };
    }
 
    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 = 0;
        Object.keys(duplex.gaze.watched()).forEach(function (dir) {
            count += duplex.gaze.watched()[dir].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
        };
 
        gulp.src([filepath], 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;
};