watch.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. if (!grunt.option('skip-ts-compile')) {
  10. grunt.log.writeln('We recommoned starting with: grunt watch --force --skip-ts-compile')
  11. grunt.log.writeln('Then do incremental typescript builds with: grunt exec:tswatch')
  12. }
  13. done = this.async();
  14. lastTime = new Date().getTime();
  15. if (firstRun === false) {
  16. grunt.log.writeln('Watch resuming');
  17. return;
  18. }
  19. gaze(config.srcDir + '/**/*', function(err, watcher) {
  20. console.log('Gaze watchers setup');
  21. watcher.on('all', function(evtName, filepath) {
  22. filepath = path.relative(process.cwd(), filepath);
  23. // ignore multiple changes at once
  24. var now = new Date().getTime();
  25. if (now - lastTime < 100) {
  26. return;
  27. }
  28. lastTime = now;
  29. var newPath;
  30. grunt.log.writeln('File Changed: ', filepath);
  31. if (/(\.html)|(\.json)$/.test(filepath)) {
  32. newPath = filepath.replace(/^public/, 'public_gen');
  33. grunt.log.writeln('Copying to ' + newPath);
  34. grunt.file.copy(filepath, newPath);
  35. }
  36. if (/(\.js)$/.test(filepath)) {
  37. newPath = filepath.replace(/^public/, 'public_gen');
  38. grunt.log.writeln('Copying to ' + newPath);
  39. grunt.file.copy(filepath, newPath);
  40. grunt.task.run('jshint');
  41. grunt.task.run('jscs');
  42. }
  43. if (/(\.scss)$/.test(filepath)) {
  44. grunt.task.run('clean:css');
  45. grunt.task.run('css');
  46. }
  47. if (/(\.ts)$/.test(filepath)) {
  48. newPath = filepath.replace(/^public/, 'public_gen');
  49. grunt.log.writeln('Copying to ' + newPath);
  50. grunt.file.copy(filepath, newPath);
  51. if (grunt.option('skip-ts-compile')) {
  52. grunt.log.writeln('Skipping ts compile, run grunt exec:tswatch to start typescript watcher')
  53. } else {
  54. grunt.task.run('exec:tscompile');
  55. }
  56. grunt.config('tslint.source.files.src', filepath);
  57. grunt.task.run('exec:tslintfile');
  58. }
  59. done();
  60. firstRun = false;
  61. grunt.task.run('watch');
  62. });
  63. });
  64. });
  65. };