Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Immediately exits when watching an empty directory #40

Closed
Fauntleroy opened this issue May 10, 2013 · 8 comments
Closed

Immediately exits when watching an empty directory #40

Fauntleroy opened this issue May 10, 2013 · 8 comments

Comments

@Fauntleroy
Copy link

I'm having trouble setting up a watcher for a certain file type in a directory that starts out empty.

var fs = require('fs');
var gaze = require('gaze');
gaze( 'test/**/*.hbs', function(){
    this.on('added',function(path){
        console.log( 'added file at', path );
    });
});
fs.writeFileSync( 'test/test.hbs', 'test' );

Am I just misunderstanding how this works, or has something gone wrong?

@yocontra
Copy link
Contributor

yocontra commented Jul 6, 2013

I can't get this to work either. I have almost identical code

@martinheidegger
Copy link

Same problem here.

@shama
Copy link
Owner

shama commented Oct 19, 2013

I think we should handle this in the implementation of gaze rather than internally. As we would need to detect if no files were matched and then purposely keep the process alive. Which could be annoying and hacky for those who don't want the behavior. Especially since it's easy to keep a process alive with setInterval(function() {}, 200);.

FWIW, I'm planning on forcefully keeping grunt-contrib-watch alive to wait for added files even when the files dont yet exist. I encourage others using this lib to do the same approach if needed. Thanks!

@shama shama closed this as completed Oct 19, 2013
@martinheidegger
Copy link

API-wise this is counter-intuitive:

The workflow looks to be like: Start Gaze -> Wait for files to come/change (.close() gaze manually if isn't needed anymore).

Now the workflow is like: Start Gaze -> Wait for files to come/change + Watch out for possible end event that could occur for without telling gaze to stop.

In other words: I was hoping that gaze would just run forever with any given input pattern - a wonderful thing - but instead I am confronted with: may break or end under certain circumstances making it more of a hazzle to work with.

@shama
Copy link
Owner

shama commented Oct 19, 2013

It's really not that dire, imo:

var interval = setInterval(function() {}, 200); // keep the process alive
require('gaze')('**/*.js', function() {
  this.on('all', function(event, filepath) {
    console.log(filepath + ' has ' + event);
  });
  this.on('end', function() {
    clearInterval(interval); // stop the process
  });
});

or more simply, this.on('end', function() { process.exit(0); }); if that works for your implementation.

We could handle this within gaze as you suggested but doing so means we're limiting the user's control over the process. What if a user wants their process to exit if no files match (as the current library does)? It seems equally as cumbersome for that user to check if no files have been matched then purposely exit the process.

I'm definitely open to discussing this if you feel strongly about it though.

@martinheidegger
Copy link

My point was supposed to be that - in terms of API design - the current version feels harder to read and less intuitive than it could be. But I agree to your point: it is actually a useful information if there is no match to any pattern. However: that information is also useful after you started (to) gaze. How about this API:

var Gaze = require("gaze").Gaze;
var geezer = new Gaze("**/*.js");
geezer.on("no-match", function() {
  geezer.close();
});
geezer.on("all", function() {
  // ...
});

Now the process would elegantly close whenever there is no match happening, no matter if it happens on the startup or after someone deleted some files.

@shama
Copy link
Owner

shama commented Oct 19, 2013

Not a bad idea especially since I'll need that for this issue: gruntjs/grunt-contrib-watch#183 Let me give this some more thought. Reopening.

@shama shama reopened this Oct 19, 2013
@shama shama closed this as completed in bfd0ccc Nov 28, 2013
@shama
Copy link
Owner

shama commented Nov 28, 2013

Added a nomatch event that will fire when no files have been matched and the api will now keep the process alive by default. Let me know of any issues with that implementation. Thanks for the good idea!

floatdrop added a commit to floatdrop/gulp-watch that referenced this issue Apr 17, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants