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 | 8
8
8
23
23
3
3
23
3
3
3
3
23
23
23
23
23
10
9
13
22
22
22
22
22
22
22
22
22
12
22
18
18
22
22
22
14
14
14
2
2
14
14
16
16
22
5
1
1
1
1
33
6
33
33
33
22
1
33
19
19
14
14
1
36
22
1
12
12
12
8
7
7
7
13
32
24
7
1
5
5
4
4
8
1
17
17
17
17
15
8
| 'use strict';
var Duplex = require('stream').Duplex,
batch = require('gulp-batch'),
fs = require('vinyl-fs'),
path = require('path'),
gutil = require('gulp-util'),
minimatch = require('minimatch'),
glob2base = require('glob2base');
module.exports = function (opts, cb) {
var Gaze = require('gaze');
if (typeof opts !== 'object') {
cb = opts;
opts = { };
}
if (Array.isArray(cb)) {
var tasks = cb;
var gulp = require('gulp');
cb = function() {
gulp.start.apply(gulp, tasks);
}.bind(gulp);
}
opts.emit = opts.emit || 'one';
opts.glob = opts.glob || [];
if (typeof opts.glob === 'string') { opts.glob = [ opts.glob ]; }
var duplex = new Duplex({ objectMode: true, allowHalfOpen: true });
if (cb) {
if (typeof cb !== 'function') { throw new Error('Provided callback is not a function: ' + cb); }
cb = batch(opts, cb.bind(duplex));
} else {
cb = function () { };
}
duplex.gaze = new Gaze(opts.glob, opts.gaze || {});
duplex._write = function _write(file, encoding, done) {
duplex.gaze.add(file.path, function () {
if (!opts.silent && opts.verbose) { logEvent('added to watch', file.path, opts); }
done();
});
memorizeProperties(file);
if (opts.passThrough !== false) { passThrough(file); }
};
duplex._read = function _read() { };
duplex.on('finish', function () {
if (!opts.silent) { fileCount(duplex.gaze, function (err, count) { logEvent('added from pipe', count, opts); }); }
});
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.gaze.on('ready', duplex.emit.bind(duplex, 'ready'));
duplex.gaze.on('all', function (event, filepath) {
if (!opts.silent) { logEvent(event, filepath, opts); }
var glob = [ filepath ];
if (opts.emit === 'all') {
glob = glob.concat(opts.glob);
glob = glob.concat(duplex.gaze._patterns);
}
if (event === 'deleted') { passThrough(voidFile(filepath, event)); }
fs.src(glob, opts).on('data', function (file) {
if (file.path === filepath) { file.event = event; }
passThrough(file);
});
});
if (opts.glob.length && opts.emitOnGlob !== false) {
fs.src(opts.glob, opts).on('data', passThrough);
}
function voidFile(filepath, event) {
return {
path: filepath,
event: event,
isNull: function () { return true; }
};
}
function passThrough(file) {
if (!opts.silent && opts.verbose) {
logEvent('passed through', file.path, opts);
}
restoreProperties(file, opts);
cb(file);
duplex.push(file);
}
var pathCache = {};
function restoreProperties(file) {
if (pathCache[file.path]) {
if (file.base) { file.base = pathCache[file.path].base; }
if (file.cwd) { file.cwd = pathCache[file.path].cwd; }
} else {
file.base = opts.base || calculateBase(opts.glob, file) || file.base;
memorizeProperties(file);
}
}
function memorizeProperties(file) {
pathCache[file.path] = {
base: file.base,
cwd: file.cwd
};
}
return duplex;
};
function logEvent(event, filepath, opts) {
var msg = [gutil.colors.magenta(path.basename(filepath)), 'was', event];
if (opts.name) { msg.unshift(gutil.colors.cyan(opts.name) + ' saw'); }
gutil.log.apply(gutil, msg);
}
module.exports.getWatchedFiles = function getWatchedFiles(gaze, cb) {
var dirs = gaze._watched;
var files = [];
Object.keys(dirs).forEach(function (dir) {
dirs[dir].forEach(function (file) {
if (file[file.length - 1] !== '/') {
files.push(file);
}
});
});
cb(null, files);
};
function fileCount(gaze, cb) {
module.exports.getWatchedFiles(gaze, function (err, files) {
if (err) { return cb(err); }
var count = files.length;
cb(null, count + (count === 1 ? ' file' : ' files'));
});
}
module.exports.fileCount = fileCount;
function calculateBase(globs, file) {
if (typeof globs === 'string') { globs = [ globs ]; }
for (var i = globs.length - 1; i >= 0; i--) {
var p = path.relative(file.cwd || process.cwd(), file.path);
if (!minimatch(p, globs[i])) { continue; }
return glob2base({
minimatch: new minimatch.Minimatch(globs[i])
});
}
}
module.exports.calculateBase = calculateBase;
|