watch_task.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. var fs = require('fs')
  2. , FileList = require('filelist').FileList;
  3. var THROTTLE = 5000;
  4. /**
  5. @name WatchTask
  6. @constructor
  7. @description Watches specified files for changes, and runs a
  8. set of tasks each time there's a change
  9. @param {String} [name] Name of the WatchTask -- defaults to 'watch'
  10. if not specified
  11. @param {Array} taskNames The list of tasks to run with each change
  12. @param {Function} definition Function to set up the WatchTask --
  13. invoked on `this` after the initial construction
  14. */
  15. var WatchTask = function () {
  16. var self = this
  17. , args = Array.prototype.slice.call(arguments)
  18. , arg
  19. , definition
  20. , taskNames
  21. , name
  22. , last = (new Date()).getTime() - THROTTLE;
  23. args = args.filter(function (a) {
  24. return !!a;
  25. });
  26. while ((arg = args.shift())) {
  27. if (typeof arg == 'string') {
  28. name = arg;
  29. }
  30. else if (typeof arg == 'object' && Array.isArray(arg)) {
  31. taskNames = arg;
  32. }
  33. else if (typeof arg == 'function') {
  34. definition = arg;
  35. }
  36. }
  37. if (!(taskNames && taskNames.length)) {
  38. throw new Error('Watch task needs some tasks to run');
  39. }
  40. name = name || 'watch';
  41. definition = definition || function () {};
  42. if (jake.Task[name]) {
  43. throw new Error('WatchTask named "' + name + '" already exists. ' +
  44. 'Please use a different name.');
  45. }
  46. this.watchTasks = Array.isArray(taskNames) ? taskNames : [taskNames];
  47. this.watchFiles = new FileList();
  48. this.rootTask = task('watchTasks', this.watchTasks);
  49. this.throttle = THROTTLE;
  50. this.watchFiles.include(WatchTask.DEFAULT_INCLUDE_FILES);
  51. this.watchFiles.exclude(WatchTask.DEFAULT_EXCLUDE_FILES);
  52. if (typeof definition == 'function') {
  53. definition.call(this);
  54. }
  55. desc('Runs these tasks: ' + this.watchTasks.join(', '));
  56. task(name, function () {
  57. console.log('WatchTask started for: ' + self.watchTasks.join(', '));
  58. jake.watch('.', {includePattern: /.+/}, function (filePath) {
  59. var fileMatch = self.watchFiles.toArray().some(function (item) {
  60. return item == filePath;
  61. });
  62. if (fileMatch && ((new Date()).getTime() - last) > self.throttle) {
  63. last = (new Date()).getTime();
  64. self.rootTask.reenable(true);
  65. self.rootTask.invoke();
  66. }
  67. });
  68. });
  69. };
  70. WatchTask.DEFAULT_INCLUDE_FILES = [
  71. './**/*.js'
  72. , './**/*.coffee'
  73. , './**/*.css'
  74. , './**/*.less'
  75. , './**/*.scss'
  76. ];
  77. WatchTask.DEFAULT_EXCLUDE_FILES = [];
  78. if (fs.existsSync('node_modules')) {
  79. WatchTask.DEFAULT_EXCLUDE_FILES.push('node_modules/**');
  80. }
  81. if (fs.existsSync('.git')) {
  82. WatchTask.DEFAULT_EXCLUDE_FILES.push('.git/**');
  83. }
  84. exports.WatchTask = WatchTask;