watch.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. module.exports = function(config, grunt) {
  2. 'use strict';
  3. var gaze = require('gaze');
  4. var path = require('path');
  5. var firstRun = true;
  6. var done;
  7. var lastTime;
  8. grunt.registerTask('watch', function() {
  9. done = this.async();
  10. lastTime = new Date().getTime();
  11. if (firstRun === false) {
  12. grunt.log.writeln('Watch resuming');
  13. return;
  14. }
  15. gaze(config.srcDir + '/**/*', function(err, watcher) {
  16. console.log('Gaze watchers setup');
  17. watcher.on('all', function(evtName, filepath) {
  18. filepath = path.relative(process.cwd(), filepath);
  19. // ignore multiple changes at once
  20. var now = new Date().getTime();
  21. if (now - lastTime < 100) {
  22. return;
  23. }
  24. lastTime = now;
  25. var newPath;
  26. grunt.log.writeln('File Changed: ', filepath);
  27. if (/(\.html)|(\.json)$/.test(filepath)) {
  28. newPath = filepath.replace(/^public/, 'public_gen');
  29. grunt.log.writeln('Copying to ' + newPath);
  30. grunt.file.copy(filepath, newPath);
  31. }
  32. if (/(\.js)$/.test(filepath)) {
  33. newPath = filepath.replace(/^public/, 'public_gen');
  34. grunt.log.writeln('Copying to ' + newPath);
  35. grunt.file.copy(filepath, newPath);
  36. grunt.task.run('jshint');
  37. grunt.task.run('jscs');
  38. }
  39. if (/(\.scss)$/.test(filepath)) {
  40. grunt.task.run('clean:css');
  41. grunt.task.run('css');
  42. }
  43. if (/(\.ts)$/.test(filepath)) {
  44. newPath = filepath.replace(/^public/, 'public_gen');
  45. grunt.log.writeln('Copying to ' + newPath);
  46. grunt.file.copy(filepath, newPath);
  47. grunt.task.run('exec:tslint');
  48. }
  49. done();
  50. firstRun = false;
  51. grunt.task.run('watch');
  52. });
  53. });
  54. });
  55. };