Skip to content

Commit

Permalink
Disable fs.watch() for dirs/files specified by "ignore" option
Browse files Browse the repository at this point in the history
  • Loading branch information
seaoak authored and Thomas Parisot committed Jan 17, 2020
1 parent 297fdec commit 9fe5ca6
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/box/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ function Box(ctx, base, options) {
this.Cache = ctx.model('Cache');
this.File = this._createFileClass();
this.ignore = ctx.config.ignore;
if (ctx.config.ignore) {
const targets = Array.isArray(ctx.config.ignore) ? ctx.config.ignore : [ctx.config.ignore];
this.options.ignored = (this.options.ignored || []).concat(targets.map(s => toRegExp(ctx, s)).filter(x => x));
}
}

require('util').inherits(Box, EventEmitter);
Expand All @@ -52,6 +56,20 @@ function getHash(path) {
});
}

function toRegExp(ctx, arg) {
if (!arg) return null;
if (typeof arg !== 'string') {
ctx.log.warn('A value of "ignore:" section in "_config.yml" is not invalid (not a string)');
return null;
}
const result = micromatch.makeRe(arg);
if (!result) {
ctx.log.warn('A value of "ignore:" section in "_config.yml" can not be converted to RegExp:' + arg);
return null;
}
return result;
}

Box.prototype._createFileClass = function() {
const ctx = this.context;

Expand Down
72 changes: 72 additions & 0 deletions test/scripts/box/box.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,78 @@ describe('Box', () => {
});
});

it('watch() - update with simple "ignore" option', () => {
const box = newBox('test', {ignore: '**/ignore_me'});
const path1 = 'a.txt';
const path2 = 'b.txt';
const src1 = pathFn.join(box.base, path1);
const src2 = pathFn.join(box.base, 'ignore_me', path2);
const cacheId1 = 'test/' + path1;
const cacheId2 = 'test/ignore_me/' + path2;
const Cache = box.Cache;
const processor = sinon.spy();

box.addProcessor(processor);

let file;
return Promise.all([
fs.writeFile(src1, 'a'),
Cache.insert({_id: cacheId1})
]).then(() => Promise.all([
fs.writeFile(src2, 'b'),
Cache.insert({_id: cacheId2})
])).then(() => box.watch()).then(() => fs.appendFile(src1, 'aaa')).delay(500).then(() => {
file = processor.lastCall.args[0];

file.source.should.eql(src1);
file.path.should.eql(path1);
file.type.should.eql('update');
file.params.should.eql({});
}).then(() => fs.appendFile(src2, 'bbb')).delay(500).then(() => {
const file2 = processor.lastCall.args[0];
file2.should.eql(file); // not changed
}).finally(() => {
box.unwatch();
return fs.rmdir(box.base);
});
});

it('watch() - update with complex "ignore" option', () => {
const box = newBox('test', {ignore: ['**/ignore_me', '**/ignore_me_too']});
const path1 = 'a.txt';
const path2 = 'b.txt';
const src1 = pathFn.join(box.base, path1);
const src2 = pathFn.join(box.base, 'ignore_me', path2);
const cacheId1 = 'test/' + path1;
const cacheId2 = 'test/ignore_me/' + path2;
const Cache = box.Cache;
const processor = sinon.spy();

box.addProcessor(processor);

let file;
return Promise.all([
fs.writeFile(src1, 'a'),
Cache.insert({_id: cacheId1})
]).then(() => Promise.all([
fs.writeFile(src2, 'b'),
Cache.insert({_id: cacheId2})
])).then(() => box.watch()).then(() => fs.appendFile(src1, 'aaa')).delay(500).then(() => {
file = processor.lastCall.args[0];

file.source.should.eql(src1);
file.path.should.eql(path1);
file.type.should.eql('update');
file.params.should.eql({});
}).then(() => fs.appendFile(src2, 'bbb')).delay(500).then(() => {
const file2 = processor.lastCall.args[0];
file2.should.eql(file); // not changed
}).finally(() => {
box.unwatch();
return fs.rmdir(box.base);
});
});

it('watch() - watcher has started', () => {
const box = newBox();

Expand Down

0 comments on commit 9fe5ca6

Please sign in to comment.