Browse Source

Added muting to cut down on 'file change' spam

Harry Hedger 9 years ago
parent
commit
aa22882306
1 changed files with 30 additions and 17 deletions
  1. 30 17
      website/watch.js

+ 30 - 17
website/watch.js

@@ -1,4 +1,4 @@
-var fs = require('fs');
+var createStream = require('fs').createWriteStream;
 var glob = require('multi-glob').glob;
 var chalk = require('chalk');
 var notifier = require('node-notifier');
@@ -14,7 +14,10 @@ var pattern = 'src/examples/**/' + src;
 glob([pattern, 'website/' + pattern], function(err, files) {
   if (err) throw new Error(err);
 
-  var mute = false;
+  console.log('--> Watching examples..');
+  console.log('--> Pre-building ' + files.length + ' files..')
+
+  var muted = [];
 
   files.forEach(function(file) {
     var watcher = browserify(file, {
@@ -29,37 +32,47 @@ glob([pattern, 'website/' + pattern], function(err, files) {
 
     watcher
       .on('update', bundle)
-      .on('error', handleError)
+      .on('error', onError)
       .on('log', function(msg) {
-        console.log(chalk.green('✓ done:'), chalk.bold(file), chalk.gray.dim(msg));
-        mute = false;
+        console.info(chalk.green('✓ done:'), chalk.green(file), chalk.gray.dim('(' + msg + ')'));
+      })
+      .on('file', function(file, id, parent) {
+        muted = muted.filter(function(mutedId) {
+          return id !== mutedId;
+        });
       });
 
     bundle();
 
-    function bundle(id) {
-      if (id && !mute) {
-        console.log(chalk.cyan('change:'), chalk.bold(id[0]));
-        mute = true;
-      }
+    function bundle(ids) {
+      ids = ids || [];
+      ids.forEach(function(id) {
+        if (!isMuted(id, muted)) {
+          console.info(chalk.cyan('change:'), id);
+          muted.push(id);
+        }
+      });
 
       var output = file.replace(src, dest);
-      var bundle = watcher.bundle();
+      var bundle = watcher.bundle()
+      .on('error', onError)
       bundle.pipe(createStream(output));
       bundle.pipe(createStream(output.replace('src', 'public')));
     }
   });
 });
 
-function createStream(filepath) {
-  return fs.createWriteStream(filepath);
-}
-
-function handleError(err) {
-  console.log(chalk.red('✗ error:'), err.message);
+function onError(err) {
+  console.error(chalk.red('✗ error:'), chalk.red(err.message));
   notifier.notify({
     'title': 'Build failed:',
     'message': err.message
   })
   this.emit('end');
 }
+
+function isMuted(id, list) {
+  return list.reduce(function(prev, curr) {
+    return prev || (curr === id);
+  }, false);
+}