Created
March 17, 2016 14:56
-
-
Save EtienneR/a7d9e956ace886516299 to your computer and use it in GitHub Desktop.
Auto reload your Go webserver with Gulp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In case someone is trying to convert this to gulp 4: https://gist.github.com/quantonganh/ee8ee0e2c9576acdcbfc56fd627ad07e