Skip to content

Instantly share code, notes, and snippets.

@EtienneR
Created March 17, 2016 14:56
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save EtienneR/a7d9e956ace886516299 to your computer and use it in GitHub Desktop.
Save EtienneR/a7d9e956ace886516299 to your computer and use it in GitHub Desktop.
Auto reload your Go webserver with Gulp
const gulp = require('gulp'),
util = require('gulp-util'),
notifier = require('node-notifier'),
sync = require('gulp-sync')(gulp).sync,
reload = require('gulp-livereload'),
child = require('child_process'),
os = require('os');
var server = null;
// Compile application
gulp.task('server:build', function() {
// Build application in the "gobin" folder
var build = child.spawnSync('go', ['install']);
if (build.stderr.length) {
util.log(util.colors.red('Something wrong with this version :'));
var lines = build.stderr.toString()
.split('\n').filter(function(line) {
return line.length
});
for (var l in lines)
util.log(util.colors.red(
'Error (go install): ' + lines[l]
));
notifier.notify({
title: 'Error (go install)',
message: lines
});
}
return build;
});
// Server launch
gulp.task('server:spawn', function() {
// Stop the server
if (server && server !== 'null') {
server.kill();
}
// Application name
if (os.platform() == 'win32') {
// Windows
var path_folder = __dirname.split('\\');
} else {
// Linux / MacOS
var path_folder = __dirname.split('/');
}
var length = path_folder.length;
var app = path_folder[length - parseInt(1)];
// Run the server
if (os.platform() == 'win32') {
server = child.spawn(app + '.exe');
} else {
server = child.spawn(app);
}
// Display terminal informations
server.stderr.on('data', function(data) {
process.stdout.write(data.toString());
});
});
// Watch files
gulp.task('server:watch', function() {
gulp.watch([
'*.go',
'**/*.go',
], sync([
'server:build',
'server:spawn'
], 'server'));
});
gulp.task('default', ['server:build', 'server:spawn', 'server:watch']);
@quantonganh
Copy link

In case someone is trying to convert this to gulp 4: https://gist.github.com/quantonganh/ee8ee0e2c9576acdcbfc56fd627ad07e

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