watch.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. // copy ts file also used by source maps
  48. //changes changed file source to that of the changed file
  49. grunt.config('typescript.build.src', filepath);
  50. grunt.config('tslint.source.files.src', filepath);
  51. grunt.task.run('typescript:build');
  52. grunt.task.run('exec:tslint');
  53. }
  54. done();
  55. firstRun = false;
  56. grunt.task.run('watch');
  57. });
  58. });
  59. });
  60. };