Skip to content

Commit

Permalink
Started implementing as a separate lib
Browse files Browse the repository at this point in the history
  • Loading branch information
sleeper committed Dec 19, 2012
1 parent 0e2c481 commit bac64ec
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 79 deletions.
19 changes: 19 additions & 0 deletions lib/regarde.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

//
// Observe files changes on disk as per the furnished configuration.
// The +watcherObject+ is an instance of a watcher class that have the same interface
// as Gaze. This is used only for driving easily the tests
//
var Regarde = module.exports = function (grunt, watcherObject) {
var watcher = watcherObject;

if (! watcher) {
var Gaze = require('gaze').Gaze;
watcher = new Gaze();
}

watcher.on('changed', function (filepath) {
grunt.event.emit('regarde:file:changed', filepath);
})
};
51 changes: 38 additions & 13 deletions tasks/regarde.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ module.exports = function (grunt) {
grunt.registerTask('regarde', 'Observe files on the filesystem', function (target) {
var name = this.name;
var config = grunt.config(name);

new Regarde(grunt);

var targets = target ? [target] : Object.keys(config);
var done = this.async();

Expand All @@ -16,22 +19,44 @@ module.exports = function (grunt) {
var name = t[0];
var target = t[1];
var pattern = target.files;
var options = {};

var gaze = new Gaze(pattern, options, function (err, watcher) {
if (err) {
grunt.log.error(err.message);
return done();
}
// On changed/added/deleted
watcher.on('all', function (status, filepath) {
var options = {interval: 10};

// var gaze = new Gaze(pattern, options, function (err, watcher) {
// if (err) {
// grunt.log.error(err.message);
// return done();
// }

// watcher.on('ready', function() {
// console.log("FRED: ready received");
// grunt.event.emit('regarde:init:' + name + ':done');
// });

// // On changed/added/deleted
// watcher.on('all', function (status, filepath) {
// console.log("FRED: detected : " + pattern);
// filepath = path.relative(process.cwd(), filepath);
// grunt.event.emit('regarde:file:' + status, filepath);
// });
// // On watcher error
// watcher.on('error', function (err) { grunt.log.error(err); });

// });

var gaze = new Gaze(pattern, options);
gaze.on('ready', function (w) {
console.log("FRED: ready !!!");
grunt.event.emit('regarde:init:' + name + ':done');
});
gaze.on('error', function (err) { grunt.log.error(err); });
gaze.on('all', function (status, filepath) {
console.log("FRED: detected : " + pattern);
filepath = path.relative(process.cwd(), filepath);
grunt.event.emit('regarde:file:' + status, filepath);
});
// On watcher error
watcher.on('error', function (err) { grunt.log.error(err); });
grunt.event.emit('regarde:init:' + name + ':done');

});


});

});
Expand Down
45 changes: 45 additions & 0 deletions test/helpers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';
var events = require('events');
var path = require('path');
var rimraf = require('rimraf');
var mkdirp = require('mkdirp');
var util = require('util');

// top level exports
var helpers = module.exports;

// Removes, creates and cd into the specified directory. If the current working
// directory is the same as the specified one, then acts as a noop. Meant to be
// used once per mocha suite.
//
// - dir - the directory path to create
//
// Example:
//
// before(helpers.directory('.test'));
//
// Returns a function suitable to use with mocha's before/after hooks.
helpers.directory = function directory(dir) {
return function directory(done) {
process.chdir(path.join(__dirname, '..'));
rimraf(dir, function(err) {
if(err) return done(err);
mkdirp(dir, function(err) {
if(err) return done(err);
process.chdir(dir);
done();
});
});
};
};

helpers.testWatcher = function() {

}
util.inherits(helpers.testWatcher, events.EventEmitter);

// Simulate a file change
//
helpers.testWatcher.prototype.fileChange = function fileChange(file) {
this.emit('changed', file);
}
98 changes: 98 additions & 0 deletions test/test-regarde-task.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
'use strict';
var path = require('path');
var assert = require('assert');
var grunt = require('grunt');
var rimraf = require('rimraf');
var mkdirp = require('mkdirp');
var fs = require('fs');

grunt.task.init([]);
grunt.config.init({});

var opts = grunt.cli.options;
opts.redirect = !opts.silent;

var directory = function directory(dir) {
return function directory(done) {
process.chdir(__dirname);
rimraf(dir, function (err) {
if (err) {
return done(err);
}
mkdirp(dir, function (err) {
if (err) {
return done(err);
}
process.chdir(dir);
done();
});
});
};
};

describe('regarde task', function () {
before(directory('temp'));

it('should check each of the target has a correct config');
it('should forbid config with no task and events false');
it('should filter tasks to keep only strings and arrays');
it('should emit events if no task is given');

// it('should accept file patterns', function (done) {
// grunt.log.muted = true;
// grunt.config.init();
// grunt.config('regarde', {fred: {files: '*.txt', events: true }});
// grunt.file.write('fred.txt', '1');
// grunt.file.write('john.txt', '1');

// var changed = [];

// grunt.event.on('regarde:file:changed', function (file) {
// assert.equal(file, 'fred.txt');
// done();

// // changed.push(file);
// // console.log('FRED: ' + changed.length);
// // if (changed.length === 2) {
// // // changed = changed.sort();
// // // assert.equal(changed[0], "fred.txt");
// // // assert.equal(changed[1], "john.txt");
// // assert(true);
// // done();
// // }
// });

// grunt.task.run('regarde');
// grunt.task.start();

// grunt.event.on('regarde:init:fred:done', function () {
// fs.writeFileSync('fred.txt', '2');
// fs.writeFileSync('john.txt', '2');
// });
// });

it('should send event when a file is modified', function (done) {
grunt.log.muted = true;
grunt.config.init();
grunt.config('regarde', {fred: {files: 'fred.txt', events: true }});
fs.writeFileSync('fred.txt', '1');

grunt.event.on('regarde:file:changed', function (file) {
console.log("FRED: file:changed");
assert.equal(file, 'fred.txt');
done();
});

grunt.task.run('regarde:fred');
grunt.task.start();

grunt.event.on('regarde:init:fred:done', function () {
console.log("FRED: here");
// fs.writeFileSync('fred.txt', '2');
grunt.file.write('fred.txt', '2');
console.log("FRED: written");
});
});

it('should launch a task upon file change when requested');
});
85 changes: 19 additions & 66 deletions test/test-regarde.js
Original file line number Diff line number Diff line change
@@ -1,90 +1,43 @@
'use strict';
var path = require('path');
var assert = require('assert');
var grunt = require('grunt');
var rimraf = require('rimraf');
var mkdirp = require('mkdirp');
var helpers = require('./helpers');
var fs = require('fs');
var grunt = require('grunt');
var Regarde = require('../lib/regarde');

grunt.task.init([]);
grunt.config.init({});

var opts = grunt.cli.options;
opts.redirect = !opts.silent;

var directory = function directory(dir) {
return function directory(done) {
process.chdir(__dirname);
rimraf(dir, function (err) {
if (err) {
return done(err);
}
mkdirp(dir, function (err) {
if (err) {
return done(err);
}
process.chdir(dir);
done();
});
});
};
};
describe('Regarde', function (){
before(helpers.directory('temp'));

describe('regarde task', function () {
before(directory('temp'));
// it('should be able to take a new watched class and use it', function () {
// var called = false;
// var myWatcher = function() { called = true; };
// var r = new Regarde(grunt, myWatcher);
// assert(called);
// });

it('should check each of the target has a correct config');
it('should forbid config with no task and events false');
it('should filter tasks to keep only strings and arrays');
it('should emit events if no task is given');

it('should accept file patterns', function (done) {
grunt.log.muted = true;
grunt.config.init();
grunt.config('regarde', {fred: {files: '*.txt', events: true }});
grunt.file.write('fred.txt', '1');
grunt.file.write('john.txt', '1');

var changed = [];

grunt.event.on('regarde:file:changed', function (file) {
changed.push(file);
console.log('FRED: ' + changed.length);
if (changed.length === 2) {
// changed = changed.sort();
// assert.equal(changed[0], "fred.txt");
// assert.equal(changed[1], "john.txt");
assert(true);
done();
}
});

grunt.task.run('regarde');
grunt.task.start();

grunt.event.on('regarde:init:fred:done', function () {
fs.writeFileSync('fred.txt', '2');
fs.writeFileSync('john.txt', '2');
});
});

it('should send event when a file is modified', function (done) {
grunt.log.muted = true;
grunt.config.init();
grunt.config('regarde', {fred: {files: 'fred.txt', events: true }});
grunt.config('regarde', {fred: {files: '*.txt', events: true }});
grunt.file.write('fred.txt', '1');

grunt.event.on('regarde:file:changed', function (file) {
console.log("FRED: file:changed");
assert.equal(file, 'fred.txt');
done();
});

grunt.task.run('regarde');
grunt.task.start();
var watcher = new helpers.testWatcher();

var r = new Regarde(grunt, watcher);

// Simulate a file change.
watcher.fileChange('fred.txt')

grunt.event.on('regarde:init:fred:done', function () {
fs.writeFileSync('fred.txt', '2');
});
});

it('should launch a task upon file change when requested');
Expand Down

0 comments on commit bac64ec

Please sign in to comment.