Skip to content

Commit

Permalink
Ignore initial add events by default
Browse files Browse the repository at this point in the history
  • Loading branch information
floatdrop committed Jan 23, 2015
1 parent 0280dfb commit 377fc4f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 57 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ List of events, that should be watched by gulp-watch. Contains [event names from
Type: `String`
Default: `undefined`

Use explicit base path for files from `glob`.
Use explicit base path for files from `glob`. Read more about `base` and `cwd` in [gulpjs docs](https://github.com/gulpjs/gulp/blob/master/docs/API.md#options).

#### options.name
Type: `String`
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module.exports = function (globs, opts, cb) {
cb = cb || function () {};

opts.events = opts.events || ['add', 'change', 'unlink'];
if (opts.ignoreInitial === undefined) { opts.ignoreInitial = true; }

var baseForced = !!opts.base;
var outputStream = new Duplex({objectMode: true, allowHalfOpen: true});
Expand Down
17 changes: 2 additions & 15 deletions test/callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,9 @@ describe('callback', function () {
it('should be called on add event', function (done) {
w = watch(fixtures('*.js'), function (file) {
file.relative.should.eql('index.js');
file.event.should.eql('add');
file.event.should.eql('change');
done();
});
});

it('should be called on add event', function (done) {
w = watch(fixtures('*.js'), function (file) {
if (file.event === 'add') {
touch(fixtures('index.js'))();
}

if (file.event === 'change') {
file.relative.should.eql('index.js');
done();
}
});
}).on('ready', touch(fixtures('index.js')));
});

it('should be called on add event in new directory', function (done) {
Expand Down
19 changes: 8 additions & 11 deletions test/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,26 @@ describe('log', function () {
w = watch(fixtures('*.js'), {verbose: true});
w.once('data', function () {
gutilStub.log.calledOnce.should.be.eql(true);
strip(gutilStub.log.firstCall.args.join(' ')).should.eql('index.js was added');
strip(gutilStub.log.firstCall.args.join(' ')).should.eql('index.js was changed');
done();
});
}).on('ready', touch(fixtures('index.js')));
});

it('should print relative file name', function (done) {
w = watch(fixtures('**/*.js'), {verbose: true});
w.on('data', function (file) {
if (file.relative === 'folder/index.js') {
strip(gutilStub.log.secondCall.args.join(' ')).should.eql('folder/index.js was added');
done();
}
});
w.once('data', function () {
strip(gutilStub.log.firstCall.args.join(' ')).should.eql('folder/index.js was changed');
done();
}).on('ready', touch(fixtures('folder/index.js')));
});

it('should print custom watcher name', function (done) {
w = watch(fixtures('*.js'), { name: 'Watch', verbose: true });
w.on('ready', touch(fixtures('index.js')));
w.once('data', function () {
gutilStub.log.calledOnce.should.be.eql(true);
strip(gutilStub.log.firstCall.args.join(' ')).should.eql('Watch saw index.js was added');
strip(gutilStub.log.firstCall.args.join(' ')).should.eql('Watch saw index.js was changed');
done();
});
}).on('ready', touch(fixtures('index.js')));
});

});
44 changes: 14 additions & 30 deletions test/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,61 +30,45 @@ describe('stream', function () {
it('should emit added file', function (done) {
w = watch('test/fixtures/*.js');
w.on('data', function (file) {
file.relative.should.eql('index.js');
file.relative.should.eql('new.js');
file.event.should.eql('add');
done();
});
}).on('ready', touch(fixtures('new.js')));
});

it('should emit change event on file change', function (done) {
w = watch(fixtures('*.js'));
w.on('ready', touch(fixtures('index.js')));
w.on('data', function (file) {
if (file.event === 'change') {
file.relative.should.eql('index.js');
done();
}
file.relative.should.eql('index.js');
done();
});
});

it('should emit changed file with stream contents', function (done) {
w = watch(fixtures('*.js'), { buffer: false });
w.on('data', function (file) {
if (file.event === 'add') {
touch(fixtures('index.js'))();
}
if (file.event === 'change') {
file.contents.should.have.property('readable', true);
done();
}
});
file.contents.should.have.property('readable', true);
done();
}).on('ready', touch(fixtures('index.js')));
});

it('should emit changed file with stats', function (done) {
w = watch(fixtures('*.js'), { buffer: false });
w.on('data', function (file) {
if (file.event === 'add') {
touch(fixtures('index.js'))();
}
if (file.event === 'change') {
file.should.have.property('stat');
done();
}
});
file.should.have.property('stat');
done();
}).on('ready', touch(fixtures('index.js')));
});

it('should emit deleted file with stats', function (done) {
touch(fixtures('created.js'), function () {
w = watch(fixtures('*.js'), { buffer: false });
w.on('data', function (file) {
if (file.event === 'add' && file.relative === 'created.js') {
fs.unlinkSync(fixtures('created.js'));
}

if (file.event === 'unlink') {
file.should.have.property('contents', null);
done();
}
file.should.have.property('contents', null);
done();
}).on('ready', function () {
fs.unlinkSync(fixtures('created.js'));
});
})();
});
Expand Down

0 comments on commit 377fc4f

Please sign in to comment.