Skip to content

Instantly share code, notes, and snippets.

@emiloberg
Last active February 28, 2021 14:34
Show Gist options
  • Save emiloberg/2a6f1539c84603a18878 to your computer and use it in GitHub Desktop.
Save emiloberg/2a6f1539c84603a18878 to your computer and use it in GitHub Desktop.
Restart node.js and reload Chrome tab(s) when files change
/**
* This Gulpfile will monitor files and restart node.js
* and reload the Chrome browser tab(s) when files changes.
*
* Dependencies:
* gulp npm install -g gulp (obviously as this is a gulp script)
* gulp-nodemon npm install gulp-nodemon
* chrome-cli brew install chrome-cli (https://github.com/prasmussen/chrome-cli)
*
* Installation
* Make sure you have all dependencies
* Place this Gulpfile.js in the root of your project
*
* Running
*
* Run "gulp" to start node.js. Now when you edit files, node will restart and chrome will
* reload the corrresponding tabs.
*/
var waitBeforeReload = 300; // Time it takes for the server to restart
var tabPattern = /\[(?:\d+\:+)*(\d+)\] Node\-RED/g; // Name (<title>) of the tab. First subgroup is id of tab.
var nodemonProperties = {
script: 'red.js', // node file to run
ext: 'html js' // files to monitor for change
};
/**
* Script
*
*/
var gulp = require('gulp');
var nodemon = require('gulp-nodemon');
var exec = require('child_process').exec;
gulp.task('default', function () {
nodemon(nodemonProperties)
.on('change', function () {
setTimeout(function() {
execute('chrome-cli list tabs', function(chromeTabs) {
var match;
while (match = tabPattern.exec(chromeTabs)) {
execute('chrome-cli reload -t ' + match[1]);
}
});
}, waitBeforeReload);
});
});
function execute(command, callback){
exec(command, function(error, stdout, stderr){
if(typeof callback === 'function') {
callback(stdout);
}
});
}
@brianrhea
Copy link

You just saved me 8 minutes a day, every day for the next year. Thanks for giving me back days of my life!

@Treeless
Copy link

A note to other people coming across this. chrome-cli only works with OSX. No windows support.

@ogrotten
Copy link

@Treeless thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment