Skip to content

Commit

Permalink
watching
Browse files Browse the repository at this point in the history
  • Loading branch information
Contra committed Jul 6, 2013
1 parent f99f59c commit 6495a12
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 2 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
</tr>
</table>

This project is in it's early stages. If something is not working or you would like a new feature please use the issues page.

## Usage

```javascript
Expand Down Expand Up @@ -66,6 +68,16 @@ gulp.task('copy', function(){
// default task gets called when you run the `gulp` command
gulp.task('default', function(){
gulp.run('templates', 'scripts', 'copy');

// watch files and run scripts if they change
gulp.watch("./client/js/**", function(event){
gulp.run('scripts');
});

gulp.watch("./client/templates/**", function(event){
gulp.run('templates');
});

});
```

Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = gulp = {
file: require('./lib/createFileStream'),
folder: require('./lib/createFolderStream'),

watch: require('./lib/watchFile'),
createGlobStream: require('glob-stream').create,
readFile: require('./lib/readFile')
};
22 changes: 22 additions & 0 deletions lib/watchFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var gaze = require('gaze'),
EventEmitter = require('events').EventEmitter;

module.exports = function(glob, cb) {
var out = new EventEmitter();

var watcher = new gaze.Gaze(glob);
watcher.on('error', out.emit.bind(out, 'error'));
watcher.on('all', function(evt, path, old){
var outEvt = {type: evt, path: path, old: old};
out.emit('change', outEvt);
if(cb) cb(outEvt);
});

out.stop = watcher.close;
out.files = watcher.watched;
out.add = watcher.add;
out.remove = watcher.remove;
out._watcher = watcher;

return out;
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"glob-stream":"0.0.3",
"mkdirp":"0.3.5",
"optimist":"0.6.0",
"gulp-util":"*"
"gulp-util":"*",
"gaze":"*"
},
"devDependencies":{
"mocha":"1.12.0",
Expand Down
2 changes: 1 addition & 1 deletion test/file-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var should = require('should');
var join = require('path').join;
require('mocha');

describe('gulp file utilities', function() {
describe('gulp readFile', function() {
describe('readFile()', function() {
it('should return a valid file struct', function(done) {
var fname = join(__dirname, "./fixtures/test.coffee");
Expand Down
36 changes: 36 additions & 0 deletions test/watch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
var gulp = require('../');
var should = require('should');
var join = require('path').join;
var fs = require('fs');
var rimraf = require('rimraf');

require('mocha');

var expectedName = join(__dirname, "./fixtures/_temp.coffee");

after(function(done){
rimraf(expectedName, function(err){
should.not.exist(err);
done();
});
});

describe('gulp watch', function() {
describe('watch()', function() {
it('should return a valid file struct', function(done) {
var fname = join(__dirname, "./fixtures/*.*");

var watcher = gulp.watch(fname);
watcher.on('change', function(evt) {
should.exist(evt);
should.exist(evt.path);
should.exist(evt.type);
evt.type.should.equal('added');
evt.path.should.equal(expectedName);
done();
});

fs.writeFileSync(expectedName, "test");
});
});
});

0 comments on commit 6495a12

Please sign in to comment.