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