watch.js 2.4 KB

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