Skip to content

Commit ab3302b

Browse files
committed
allow adding/removing files, cf browserify#56
1 parent 9567993 commit ab3302b

File tree

1 file changed

+39
-17
lines changed

1 file changed

+39
-17
lines changed

index.js

+39-17
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function watchify (b, opts) {
1414
var pkgcache = b._options.packageCache;
1515
var changingDeps = {};
1616
var pending = false;
17-
17+
1818
b.on('dep', function (dep) {
1919
if (typeof dep.id === 'string') {
2020
cache[dep.id] = dep;
@@ -23,25 +23,25 @@ function watchify (b, opts) {
2323
watchFile(dep.file);
2424
}
2525
});
26-
26+
2727
b.on('file', function (file) {
2828
watchFile(file);
2929
});
30-
30+
3131
b.on('package', function (pkg) {
3232
watchFile(path.join(pkg.__dirname, 'package.json'));
3333
});
34-
34+
3535
b.on('reset', reset);
3636
reset();
37-
37+
3838
function reset () {
3939
var time = null;
4040
var bytes = 0;
4141
b.pipeline.get('record').on('end', function () {
4242
time = Date.now();
4343
});
44-
44+
4545
b.pipeline.get('wrap').push(through(write, end));
4646
function write (buf, enc, next) {
4747
bytes += buf.length;
@@ -58,28 +58,50 @@ function watchify (b, opts) {
5858
this.push(null);
5959
}
6060
}
61-
61+
6262
var fwatchers = {};
6363
var fwatcherFiles = {};
64-
64+
65+
if (opts.glob) {
66+
var w = chokidar.watch(opts.glob, {ignoreInitial: true, persistent: true});
67+
w.setMaxListeners(0);
68+
w.on('error', b.emit.bind(b, 'error'));
69+
w.on('add', function (file) {
70+
b._options.entries.push(file);
71+
b.constructor.call(b, b._options);
72+
invalidate(file);
73+
});
74+
w.on('unlink', function (file) {
75+
var i = b._options.entries.map(function (str) {
76+
return path.resolve(str);
77+
}).indexOf(path.resolve(file));
78+
if (i === -1)
79+
return;
80+
b._options.entries.splice(i, 1);
81+
b.constructor.call(b, b._options);
82+
invalidate(file);
83+
});
84+
fwatchers.glob = w;
85+
}
86+
6587
b.on('transform', function (tr, mfile) {
6688
tr.on('file', function (file) {
6789
watchDepFile(mfile, file);
6890
});
6991
});
70-
92+
7193
function watchFile (file) {
7294
fs.lstat(file, function (err, stat) {
7395
if (err || stat.isDirectory()) return;
7496
watchFile_(file);
7597
});
7698
}
77-
99+
78100
function watchFile_ (file) {
79101
if (!fwatchers[file]) fwatchers[file] = [];
80102
if (!fwatcherFiles[file]) fwatcherFiles[file] = [];
81103
if (fwatcherFiles[file].indexOf(file) >= 0) return;
82-
104+
83105
var w = chokidar.watch(file, {persistent: true});
84106
w.setMaxListeners(0);
85107
w.on('error', b.emit.bind(b, 'error'));
@@ -89,7 +111,7 @@ function watchify (b, opts) {
89111
fwatchers[file].push(w);
90112
fwatcherFiles[file].push(file);
91113
}
92-
114+
93115
function watchDepFile(mfile, file) {
94116
if (!fwatchers[mfile]) fwatchers[mfile] = [];
95117
if (!fwatcherFiles[mfile]) fwatcherFiles[mfile] = [];
@@ -104,7 +126,7 @@ function watchify (b, opts) {
104126
fwatchers[mfile].push(w);
105127
fwatcherFiles[mfile].push(file);
106128
}
107-
129+
108130
function invalidate (id) {
109131
if (cache) delete cache[id];
110132
if (fwatchers[id]) {
@@ -115,22 +137,22 @@ function watchify (b, opts) {
115137
delete fwatcherFiles[id];
116138
}
117139
changingDeps[id] = true
118-
140+
119141
// wait for the disk/editor to quiet down first:
120142
if (!pending) setTimeout(function () {
121143
pending = false;
122144
b.emit('update', Object.keys(changingDeps));
123145
changingDeps = {};
124-
146+
125147
}, opts.delay || 600);
126148
pending = true;
127149
}
128-
150+
129151
b.close = function () {
130152
Object.keys(fwatchers).forEach(function (id) {
131153
fwatchers[id].forEach(function (w) { w.close() });
132154
});
133155
};
134-
156+
135157
return b;
136158
}

0 commit comments

Comments
 (0)