watch.js 2.5 KB

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