Skip to content

Commit af24f38

Browse files
committed
Merge pull request #9 from MattSurabian/live-reload
Live reload
2 parents d344a64 + f10d98a commit af24f38

File tree

3 files changed

+42
-14
lines changed

3 files changed

+42
-14
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
This is an implementation of DuckHunt in javascript using HTML5 audio.
66

7-
All of the game logic is in the duckhunt directory. This project uses [gulp](http://gulpjs.com/) to build two concatenated js files;
7+
All of the game logic is in the duckhunt directory. This project uses [gulp](http://gulpjs.com/) to build two concatenated js files;
88
one representing all of our game logic the other representing necessary javascript library dependencies.
99

1010
This refactor of the game relies on custom events to control game flow which has cut down a bit on the "animation callback hell"
1111
faced in version 1.
1212

13-
To work with this project on your own simply clone this git repo into a directory, and run `npm install` inside that
14-
directory. The package.json file included in this repo helps npm install all the necessary node module dependencies. Make your edits
15-
to the code and run `gulp`. The default gulp task will lint the javascript, concatenate, and minify it into the build
16-
directory.
13+
## Working With This Repo
14+
15+
1. Clone the repo into a directory of your choice
16+
1. `cd` into that directory and run `npm install`
17+
1. Use the `gulp dev` task during active development. This task automatically builds all necessary JS files and triggers the [livereload browser extension](http://livereload.com/extensions/) to do its thing and reload the page when changes are detected in the `lib` and `duckhunt` directories.
18+
1. If you want to manually cut a build of the JS the default gulp task will lint, concatenate, and minify the project's javascript files it into the build directory.

gulpfile.js

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,23 @@ var concat = require('gulp-concat');
33
var uglify = require('gulp-uglify');
44
var jshint = require('gulp-jshint');
55
var gutil = require('gulp-util');
6+
var livereload = require('gulp-livereload');
7+
8+
// Command line option:
9+
// --fatal=[warning|error|off]
10+
var fatalLevel = require('yargs').argv.fatal;
11+
12+
var ERROR_LEVELS = ['error', 'warning'];
13+
14+
function isFatal(level) {
15+
return ERROR_LEVELS.indexOf(level) <= ERROR_LEVELS.indexOf(fatalLevel || 'error');
16+
}
617

718
// Handle an error based on its severity level.
819
// Log all levels, and exit the process for fatal levels.
920
function handleError(level, error) {
1021
gutil.log(gutil.colors.red(error.message));
11-
if (level === 'error') {
22+
if (isFatal(level)) {
1223
process.exit(1);
1324
}
1425
}
@@ -29,19 +40,32 @@ gulp.task('duckhunt', function() {
2940
.on('error', handleError.bind(this, 'error'))
3041
.pipe(concat('duckhunt.min.js'))
3142
.pipe(uglify())
32-
.pipe(gulp.dest('./build/'));
43+
.pipe(gulp.dest('./build/'))
44+
.pipe(livereload());
3345
});
3446

3547
gulp.task('libs', function() {
3648
return gulp.src([
37-
'./lib/jquery.js',
38-
'./lib/underscore.js',
39-
'./lib/jquery.spritely.js',
40-
'./lib/jquery.color.js',
41-
'./lib/fastclick.js'
49+
'lib/jquery.js',
50+
'lib/underscore.js',
51+
'lib/jquery.spritely.js',
52+
'lib/jquery.color.js',
53+
'lib/fastclick.js'
4254
]).pipe(concat('libs.min.js'))
4355
.pipe(uglify())
44-
.pipe(gulp.dest('./build/'));
56+
.pipe(gulp.dest('./build/'))
57+
.pipe(livereload());
58+
});
59+
60+
gulp.task('dev', function() {
61+
// no fatal errors during active development by default
62+
// this prevents this task from exiting unexpectedly
63+
fatalLevel = fatalLevel || 'off';
64+
65+
livereload.listen();
66+
gulp.watch('duckhunt/*.js', ['duckhunt']);
67+
gulp.watch('lib/*.js', ['libs']);
68+
4569
});
4670

4771
gulp.task('default', ['libs', 'duckhunt']);

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
"gulp": "^3.9.0",
2424
"gulp-concat": "^2.5.2",
2525
"gulp-jshint": "^1.11.0",
26+
"gulp-livereload": "^3.8.0",
2627
"gulp-uglify": "^1.2.0",
27-
"gulp-util": "^3.0.5"
28+
"gulp-util": "^3.0.5",
29+
"yargs": "^3.10.0"
2830
}
2931
}

0 commit comments

Comments
 (0)