Automatically reload the gulpfile.js on change

I found it quite annoying to restart gulp whenever I made changes to the gulpfile. So I added a few lines to restart gulp automatically and thought they might be useful for others as well.

var spawn = require('child_process').spawn;

gulp.task('auto-reload', function() {
	var process;

	function restart() {
		if (process) {
			process.kill();
		}

		process = spawn('gulp', ['default'], {stdio: 'inherit'});
	}

	gulp.watch('gulpfile.js', restart);
	restart();
});

When you now run gulp auto-reload this task will spawn a child process that runs gulp default (which starts several watchers in my case) and restarts it when you've made a change to the gulpfile.js.