watch.js 2.4 KB

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