Skip to content

Commit

Permalink
Merge pull request #59 from blue-jay/live-reload
Browse files Browse the repository at this point in the history
Live reload
  • Loading branch information
josephspurrier authored Feb 16, 2017
2 parents 4d8caa6 + 8aa2fe9 commit 7f0ca66
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ thumbs.db
# Blue Jay Files
env.json
*.exe
*.exe~
blueprint
114 changes: 103 additions & 11 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
// Modules
var gulp = require('gulp');
var favicon = require ('gulp-real-favicon');
var fs = require('fs');
var runSequence = require('run-sequence'); // Using until gulp v4 is released
var gulp = require('gulp');
var favicon = require('gulp-real-favicon');
var fs = require('fs');
var runSequence = require('run-sequence'); // Using until gulp v4 is released
var reload = require('gulp-livereload');
var sync = require('gulp-sync')(gulp).sync;
var child = require('child_process');
var util = require('gulp-util');
var path = require('path');
var os = require('os');

// Enviroment variables
var env = JSON.parse(fs.readFileSync('./env.json'))
var folderAsset = env.Asset.Folder;
var folderView = env.View.Folder;
var folderView = env.View.Folder;

// Other variables
var faviconData = folderAsset + '/dynamic/favicon/data.json';

// Application server
var server = null;

// SASS Task
gulp.task('sass', function() {
var sass = require('gulp-sass');
Expand All @@ -24,7 +33,8 @@ gulp.task('sass', function() {
// Available for outputStyle: expanded, nested, compact, compressed
.pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))
.pipe(ext('.min.css'))
.pipe(gulp.dest(folderAsset + '/static/css/'));
.pipe(gulp.dest(folderAsset + '/static/css/'))
.pipe(reload());
});

// JavaScript Task
Expand All @@ -39,7 +49,8 @@ gulp.task('javascript', function() {
min:'.min.js'
}
}))
.pipe(gulp.dest(folderAsset + '/static/js/'));
.pipe(gulp.dest(folderAsset + '/static/js/'))
.pipe(reload());
});

// jQuery Task
Expand Down Expand Up @@ -145,14 +156,95 @@ gulp.task('favicon-update', function(done) {
});
});

// Watch
gulp.task('watch', function() {
// Monitor Go files for changes
gulp.task('server:watch', function() {
// Restart application
gulp.watch([
'*/**/*.tmpl',
'env.json'
], ['server:spawn']);

// Rebuild and restart application server
gulp.watch([
'*.go',
'*/**/*.go'
], [
'server:build',
'server:spawn'
]);
});

// Build application from source
gulp.task('server:build', function() {
var build = child.spawnSync('go', ['build']);
if (build.stderr.length) {
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 build): ' + lines[l]
));
notifier.notify({
title: 'Error (go build)',
message: lines
});
}
return build;
});

// Spawn an application process
gulp.task('server:spawn', function() {
if (server)
server.kill();

// Get the application name based on the folder
var appname = path.basename(__dirname);

// Spawn application server
if (os.platform() == 'win32') {
server = child.spawn(appname + '.exe');
} else {
server = child.spawn('./' + appname);
}

// Trigger reload upon server start
server.stdout.once('data', function() {
reload.reload('/');
});

// Pretty print server log output
server.stdout.on('data', function(data) {
var lines = data.toString().split('\n')
for (var l in lines)
if (lines[l].length)
util.log(lines[l]);
});

// Print errors to stdout
server.stderr.on('data', function(data) {
process.stdout.write(data.toString());
});
});

// Main watch function.
gulp.task('watch', ['server:build'], function() {
// Start the listener (use with the LiveReload Chrome Extension)
reload.listen();

// Watch the assets
gulp.watch(folderAsset + '/dynamic/sass/**/*.scss', ['sass']);
gulp.watch(folderAsset + '/dynamic/js/*.js', ['javascript']);

return gulp.start(sync([
'server:watch',
'server:spawn'
]));
});

// Init - every task
gulp.task('init', ['sass', 'javascript', 'jquery', 'bootstrap', 'underscore', 'favicon']);
gulp.task('init', ['sass', 'javascript', 'jquery', 'bootstrap', 'underscore', 'favicon', 'server:build']);

// Default - only run the tasks that change often
gulp.task('default', ['sass', 'javascript']);
gulp.task('default', ['sass', 'javascript', 'server:build']);
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@
"private": true,
"devDependencies": {
"bootstrap": "^3.3.6",
"child_process": "^1.0.2",
"gulp": "^3.9.1",
"gulp-concat": "^2.6.0",
"gulp-ext-replace": "^0.3.0",
"gulp-livereload": "^3.8.1",
"gulp-minify": "0.0.13",
"gulp-real-favicon": "^0.2.1",
"gulp-sass": "^2.3.2",
"gulp-sync": "^0.1.4",
"gulp-util": "^3.0.8",
"jquery": "1.9.1 - 2",
"os": "^0.1.1",
"path": "^0.12.7",
"run-sequence": "^1.2.2",
"underscore": "^1.8.3"
}
Expand Down

0 comments on commit 7f0ca66

Please sign in to comment.