Comments (13)
Did you mean something like this:
watch('myDir/**/*.js', function(filename) {
});
That's possible, even without any changes to the node-watch
itself.
Pseudo code;
function watchByPattern(pattern, options, callback) {
// Parsing the pattern into two parts:
// 1. directory or filename.
// 2. the wildcard used for filtering.
//
// For example:
//
// Given the pattern "myDir/**/*.js", will return
// the directory 'myDir' and the wildcard "**/*.js".
var segment = patternParser(pattern);
// Filter by wildcard, using package "minimatch".
var filterByWildCard = function(wildcard, callback) {
return function(filename) {
if (minimatch(filename, wildcard)) {
callback(filename);
}
}
}
watch(
segment.dir,
options,
filterByWildCard(segment.wildcard, callback)
);
}
from node-watch.
Thanks.
But i've got some problems with this code.
First of all, there can i do my own actions?
I've got this code
function watchByPattern(options, callback) {
// Filter by wildcard, using package "minimatch".
var filterByWildCard = function(wildcard, callback) {
return function(filename) {
if (minimatch(filename, wildcard)) {
// callback(filename);
}
}
}
watch(
'./markup',
options,
filterByWildCard('**/**/*.scss', callback),
function() {
gulp.start('scss');
}
);
}
watchByPattern();
Is it correct? It doesn't work( Could you help me?
from node-watch.
Missing the minimatch
module.
Well, there is the full example:
var minimatch = require('minimatch');
var watch = require('node-watch');
function watchByPattern(pattern, callback) {
var filterByWildCard = function(wildcard, fn) {
return function(filename) {
if (minimatch(filename, wildcard)) {
fn(filename);
}
}
}
watch(
'./markup',
filterByWildCard(pattern, callback)
);
}
watchByPattern('**/*.scss', function(filename) {
console.log(filename);
});
from node-watch.
Thanks a lot)
from node-watch.
Excuse me, there is another problem. If i need to watch for js files too, i have to write something like that:
watchByPattern('**/*.scss', function(filename) {
console.log(filename);
});
watchByPattern('**/*.js', function(filename) {
console.log(filename);
});
So, the second watchByPattern doesn't work. Just no events are hired. Only the first works.
Env: Mac, OS X 10.9.4 Node.JS Version 0.10.26
from node-watch.
Sorry, it's a bug on re-watching a directory.
Try this:
var minimatch = require('minimatch');
var watch = require('node-watch');
var watchByPattern = (function() {
var patterns = [];
var ready = true;
return function(pattern, callback) {
patterns.push({
wildcard: pattern,
callback: callback
});
if (ready) {
ready = false;
watch('./markup', function(filename) {
patterns.forEach(function(pat) {
if (minimatch(filename, pat.wildcard)) {
pat.callback(filename);
}
});
});
}
}
}());
watchByPattern('**/*.scss', function(filename) {
console.log(filename);
});
watchByPattern('**/*.js', function(filename) {
console.log(filename);
});
from node-watch.
You are really great man) Thanks.
But there is one more question, please. I need to watch not only markup directory, so i write with:
var watchByPattern = (function() {
var patterns = [];
var ready = true;
return function(dir, pattern, callback) {
patterns.push({
wildcard: pattern,
callback: callback
});
if (ready) {
ready = false;
watch(dir, function(filename) {
patterns.forEach(function(pat) {
if (minimatch(filename, pat.wildcard)) {
pat.callback(filename);
}
});
});
}
}
}());
But it doesn't work, if i do with:
watchByPattern('./markup', '**/*.scss', function(filename) {
console.log(filename);
});
watchByPattern('./images', '*.png', function(filename) {
console.log(filename);
});
if dirs are similar — everything is ok, but when they are different — no events are hired.
There is a solution — different watchByPattern functions for each dir, but i think, that it can be done more easy) Could you help me?
from node-watch.
OK.
var minimatch = require('minimatch');
var watch = require('node-watch');
var watchByPattern = (function() {
var parsePattern = function(input) {
var current = './';
var segments = input.split(/\*/);
return {
dir: (/^\*/.test(input) ? current : (segments[0] || current)),
pat: ('*' + segments.slice(1).join('*'))
}
}
var watchStack = {}
return function(pattern, fn) {
var input = parsePattern(pattern);
var stack = { pat: input.pat, callback: fn };
if (watchStack[input.dir]) {
watchStack[input.dir].push(stack);
} else {
watchStack[input.dir] = [stack];
watch(input.dir, function(filename) {
watchStack[input.dir].forEach(function(stack) {
if (minimatch(filename, stack.pat)) {
stack.callback(filename);
}
});
});
};
}
}());
Usage:
watchByPattern('./markup/**/*.js', function(name) {
//
});
watchByPattern('**/*.js', function(name) {
//
});
from node-watch.
This code is quite basic, as you can see there is even no options passed inside, and the parsePattern
function, which I mentioned early, has not been fully tested.
Anyway, hope this inspired you a bit.
from node-watch.
Thank you very much) You're only one made working watcher, which can watch for new and deleted files correctly.
I'll watch for your project)
from node-watch.
Hi. I've just improved your lats code. There are you can processing unwatch path. It could be a single string of an array of strings. May be it can be usefull for somebody)
var minimatch = require('minimatch'), // Service module for node-watch
watch = require('node-watch'); // Watcher
// Watcher by node-watch
var watchByPattern = (function() {
var watchStack = {};
var parsePattern = function(input, filter) {
var current = './',
filterSegment = '',
segments = input.split(/\*/),
dir = '',
pat = '',
filterPattern = '';
if (/^\*/.test(input)) {
dir = current
} else {
dir = segments[0] || current;
}
pat = '*' + segments.slice(1).join('*');
filterPattern = filter || false;
if (filterPattern) {
if (filterPattern instanceof Array) {
for (var i = 0; i < filterPattern.length; i++ ) {
filterSegment = filterPattern[i].split(/\*/);
filterPattern[i] = '*' + filterSegment.slice(1).join('*');
}
} else {
filterSegment = filter.split(/\*/);
filterPattern = '*' + filterSegment.slice(1).join('*');
}
}
return {
dir: dir,
pat: pat,
filter: filterPattern
}
};
var patternProcessing = function(pattern, filter, fn) {
var input = parsePattern(pattern, filter),
stack = { pat: input.pat, callback: fn, filter: input.filter };
if (watchStack[input.dir]) {
watchStack[input.dir].push(stack);
} else {
watchStack[input.dir] = [stack];
watch(input.dir, function(filename) {
watchStack[input.dir].forEach(function(stack) {
var useFilter = false;
if (minimatch(filename, stack.pat)) {
if (stack.filter) {
if (stack.filter instanceof Array) {
stack.filter.forEach(function(filterItem) {
if (minimatch(filename, filterItem)) {
useFilter = true;
}
});
if (useFilter) {
return;
} else {
stack.callback(filename);
}
} else {
if (minimatch(filename, stack.filter)) {
return;
} else {
stack.callback(filename);
}
}
} else {
stack.callback(filename);
}
}
});
});
}
};
return function(pattern, filter, fn) {
if (pattern instanceof Array) {
pattern.forEach(function(patternItem) {
patternProcessing(patternItem, filter, fn);
});
} else {
patternProcessing(pattern, filter, fn);
}
}
}());
module.exports = watchByPattern;
And the simple watcher code with unwatch path (watchByPattern is a requre of watchByPattern):
watchByPattern('./markup/modules/**/*.js', './markup/modules/**/moduleData.js', function() {
gulp.start('concat-plugins-libs-and-modules-lint-modules-js');
});
from node-watch.
The indention looks not graceful :D
from node-watch.
Yeah, i need mooore indetions)
But what do you think about code?)
from node-watch.
Related Issues (20)
- Follow symlink HOT 2
- watch dir bug HOT 2
- Calling close() before the watcher is ready does nothing HOT 2
- Avoid synchronous fs calls HOT 1
- Catch errors when adding watchers for child directories HOT 1
- Catch error when use Node 14.15.3 on Linux SysTem HOT 2
- I got this error at first use lib in nodejs using express HOT 2
- node-watch is not working recursive in Ubuntu under WSL HOT 5
- when watch a dir that mount by nfs, if not work HOT 2
- New ES syntax HOT 2
- Only pass skip on initial recurse
- Travis CI is no longer free, try to switch to GitHub CI
- Fix tests on Windows
- fs.watch() not detecting changes to file from git operation HOT 3
- images update problem HOT 3
- Can TEMP_DIR able to change ?
- Is there a way, or a best practice to scan this large amount of files for processing ?
- Add ignore option HOT 7
- multiple events for a file sent over network HOT 2
- High CPU consumption on window service HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-watch.