Skip to content

Commit 10ebc59

Browse files
committed
fix: file become exists, emit change event
1 parent e98cdde commit 10ebc59

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

index.js

+16
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ proto.add = function (fullpath) {
4848
event: null,
4949
path: fullpath,
5050
stat: null,
51+
errorMessage: null
5152
};
5253
this._checkPath(fullpath);
5354
}
@@ -72,8 +73,23 @@ proto._checkPath = function (fullpath) {
7273
let that = this;
7374
fs.lstat(fullpath, function (err, stat) {
7475
if (err) {
76+
if (info.errorMessage === err.message) {
77+
// ignore this error
78+
return;
79+
}
80+
info.errorMessage = err.message;
7581
return that.emit('stat-error', err);
7682
}
83+
84+
if (info.errorMessage) {
85+
// error gone, need to emit change
86+
info.errorMessage = null;
87+
info.stat = stat;
88+
info.event = 'change';
89+
that.emit('change', info);
90+
return;
91+
}
92+
7793
if (!info.stat) {
7894
info.stat = stat;
7995
return;

test/changing.test.js

+46-7
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,25 @@ const changing = require('../');
2222
const fixtures = path.join(__dirname, 'fixtures');
2323

2424
describe('changing.test.js', function () {
25-
afterEach(function () {
25+
function cleanup() {
2626
fs.writeFileSync(path.join(fixtures, 'foo.js'), 'bar\n');
27-
});
27+
try {
28+
fs.unlinkSync(path.join(fixtures, '.tmpfile'));
29+
} catch (_) {
30+
// ignore error
31+
}
32+
}
33+
34+
beforeEach(cleanup);
35+
afterEach(cleanup);
2836

2937
it('should create watch without options', function () {
3038
var watcher = changing();
3139
watcher.close();
3240
});
3341

3442
it('should watching fixtures/foo.js change', function (done) {
35-
let watcher = changing({ interval: '1s' });
43+
let watcher = changing({ interval: 500 });
3644
watcher.add(path.join(fixtures, 'foo.js'));
3745
watcher.add(path.join(fixtures, 'foo.js'));
3846
watcher.on('change', function (info) {
@@ -47,20 +55,51 @@ describe('changing.test.js', function () {
4755

4856
setTimeout(function () {
4957
fs.writeFileSync(path.join(fixtures, 'foo.js'), 'bar update\n');
50-
}, 1500);
58+
}, 1000);
5159
});
5260

5361
it('should got stat-error when watch filepath not exists', function (done) {
54-
let watcher = changing({ interval: '1s' });
62+
let watcher = changing({ interval: '100ms' });
5563
watcher.add(path.join(fixtures, 'foo.js-not-exists'));
5664
watcher.add(path.join(fixtures, 'foo.js-not-exists'));
5765
watcher.on('change', function () {
5866
throw new Error('should not run this');
5967
});
6068
watcher.on('stat-error', function (err) {
6169
assert.equal(err.code, 'ENOENT');
62-
watcher.close();
63-
done();
70+
// and emit once
71+
setTimeout(function () {
72+
watcher.close();
73+
done();
74+
}, 200);
75+
});
76+
});
77+
78+
it('should watching fixtures/.tmpfile become exists', function (done) {
79+
let watcher = changing({ interval: 100 });
80+
watcher.add(path.join(fixtures, '.tmpfile'));
81+
watcher.on('change', function (info) {
82+
assert.equal(info.event, 'change');
83+
assert.equal(info.path, path.join(fixtures, '.tmpfile'));
84+
assert(info.stat);
85+
86+
setTimeout(function () {
87+
// remove it and emit stat-error again
88+
fs.unlinkSync(path.join(fixtures, '.tmpfile'));
89+
watcher.on('stat-error', function (err) {
90+
assert.equal(err.code, 'ENOENT');
91+
92+
// and emit once
93+
setTimeout(function () {
94+
watcher.close();
95+
done();
96+
}, 200);
97+
});
98+
}, 200);
6499
});
100+
101+
setTimeout(function () {
102+
fs.writeFileSync(path.join(fixtures, '.tmpfile'), 'tmpfile update\n');
103+
}, 200);
65104
});
66105
});

0 commit comments

Comments
 (0)