-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Started implementing as a separate lib
- Loading branch information
Showing
5 changed files
with
219 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}) | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters