watch.js 2.5 KB

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