Nav apraksta

watch.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * grunt-contrib-watch
  3. * http://gruntjs.com/
  4. *
  5. * Copyright (c) 2014 "Cowboy" Ben Alman, contributors
  6. * Licensed under the MIT license.
  7. */
  8. var path = require('path');
  9. var Gaze = require('gaze').Gaze;
  10. var _ = require('lodash');
  11. var waiting = 'Waiting...';
  12. var changedFiles = Object.create(null);
  13. var watchers = [];
  14. module.exports = function(grunt) {
  15. 'use strict';
  16. var taskrun = require('./lib/taskrunner')(grunt);
  17. // Default date format logged
  18. var dateFormat = function(time) {
  19. grunt.log.writeln(String(
  20. 'Completed in ' +
  21. time.toFixed(3) +
  22. 's at ' +
  23. (new Date()).toString()
  24. ).cyan + ' - ' + waiting);
  25. };
  26. // When task runner has started
  27. taskrun.on('start', function() {
  28. Object.keys(changedFiles).forEach(function(filepath) {
  29. // Log which file has changed, and how.
  30. grunt.log.ok('File "' + filepath + '" ' + changedFiles[filepath] + '.');
  31. });
  32. // Reset changedFiles
  33. changedFiles = Object.create(null);
  34. });
  35. // When task runner has ended
  36. taskrun.on('end', function(time) {
  37. if (time > 0) {
  38. dateFormat(time);
  39. }
  40. });
  41. // When a task run has been interrupted
  42. taskrun.on('interrupt', function() {
  43. grunt.log.writeln('').write('Scheduled tasks have been interrupted...'.yellow);
  44. });
  45. // When taskrun is reloaded
  46. taskrun.on('reload', function() {
  47. taskrun.clearRequireCache(Object.keys(changedFiles));
  48. grunt.log.writeln('').writeln('Reloading watch config...'.cyan);
  49. });
  50. grunt.registerTask('watch', 'Run predefined tasks whenever watched files change.', function(target) {
  51. var self = this;
  52. var name = self.name || 'watch';
  53. // Close any previously opened watchers
  54. watchers.forEach(function(watcher) {
  55. watcher.close();
  56. });
  57. watchers = [];
  58. // Never gonna give you up, never gonna let you down
  59. if (grunt.config([name, 'options', 'forever']) !== false) {
  60. taskrun.forever();
  61. }
  62. // If a custom dateFormat function
  63. var df = grunt.config([name, 'options', 'dateFormat']);
  64. if (typeof df === 'function') {
  65. dateFormat = df;
  66. }
  67. if (taskrun.running === false) { grunt.log.writeln(waiting); }
  68. // initialize taskrun
  69. var targets = taskrun.init(name, {target: target});
  70. targets.forEach(function(target, i) {
  71. if (typeof target.files === 'string') { target.files = [target.files]; }
  72. // Process into raw patterns
  73. var patterns = _.chain(target.files).flatten().map(function(pattern) {
  74. return grunt.config.process(pattern);
  75. }).value();
  76. // Validate the event option
  77. if (typeof target.options.event === 'string') {
  78. target.options.event = [target.options.event];
  79. }
  80. // Set cwd if options.cwd.file is set
  81. if (typeof target.options.cwd !== 'string' && target.options.cwd.files) {
  82. target.options.cwd = target.options.cwd.files;
  83. }
  84. // Create watcher per target
  85. watchers.push(new Gaze(patterns, target.options, function(err) {
  86. if (err) {
  87. if (typeof err === 'string') { err = new Error(err); }
  88. grunt.log.writeln('ERROR'.red);
  89. grunt.fatal(err);
  90. return taskrun.done();
  91. }
  92. // Log all watched files with --verbose set
  93. if (grunt.option('verbose')) {
  94. var watched = this.watched();
  95. Object.keys(watched).forEach(function(watchedDir) {
  96. watched[watchedDir].forEach(function(watchedFile) {
  97. grunt.log.writeln('Watching ' + path.relative(process.cwd(), watchedFile) + ' for changes.');
  98. });
  99. });
  100. }
  101. // On changed/added/deleted
  102. this.on('all', function(status, filepath) {
  103. // Skip events not specified
  104. if (!_.contains(target.options.event, 'all') &&
  105. !_.contains(target.options.event, status)) {
  106. return;
  107. }
  108. filepath = path.relative(process.cwd(), filepath);
  109. // Skip empty filepaths
  110. if (filepath === '') {
  111. return;
  112. }
  113. // If Gruntfile.js changed, reload self task
  114. if (target.options.reload || /gruntfile\.(js|coffee)/i.test(filepath)) {
  115. taskrun.reload = true;
  116. }
  117. // Emit watch events if anyone is listening
  118. if (grunt.event.listeners('watch').length > 0) {
  119. grunt.event.emit('watch', status, filepath, target.name);
  120. }
  121. // Group changed files only for display
  122. changedFiles[filepath] = status;
  123. // Add changed files to the target
  124. if (taskrun.targets[target.name]) {
  125. if (!taskrun.targets[target.name].changedFiles) {
  126. taskrun.targets[target.name].changedFiles = Object.create(null);
  127. }
  128. taskrun.targets[target.name].changedFiles[filepath] = status;
  129. }
  130. // Queue the target
  131. if (taskrun.queue.indexOf(target.name) === -1) {
  132. taskrun.queue.push(target.name);
  133. }
  134. // Run the tasks
  135. taskrun.run();
  136. });
  137. // On watcher error
  138. this.on('error', function(err) {
  139. if (typeof err === 'string') { err = new Error(err); }
  140. grunt.log.error(err.message);
  141. });
  142. }));
  143. });
  144. });
  145. };