-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpfile.js
110 lines (90 loc) · 3.07 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* jshint node: true */
'use strict';
// Import gulp through gulp-help
var gulp = require ('gulp-help')(require ('gulp'), {hideEmpty: true, hideDepsMessage: true});
// And lots of useful plugins
var gutil = require ('gulp-util');
var cached = require ('gulp-cached');
var serve = require ('gulp-serve');
var livereload = require ('gulp-livereload');
var embedlr = require ('gulp-embedlr');
var jshint = require ('gulp-jshint');
// Stylish jshint plugin for nicer command line output
var stylish = require ('jshint-stylish');
// Filesystem (almost always needed)
var fs = require ('fs');
// Lazypipe for performance
var lazypipe = require ('lazypipe');
// Minimist for command line parsing
var minimist = require ('minimist');
// Process command line arg for enabling stylish jshint output
// Use --basic for basic output, suitable for IDE parsing
var argv = minimist (process.argv.slice (2), {'boolean': 'basic'});
// Set true when using the long running 'gulp serve' command
var isWatching = false;
/**
* Default task, show the help
*/
gulp.task ('default', ['help']);
/**
* Generate index.html in .tmp folder, embedding the livereload script tag
*/
gulp.task ('index', function () {
return gulp.src ('./index.html')
.pipe (embedlr ())
.pipe (gulp.dest ('./.tmp/'))
.pipe (_livereload ())
;
});
/**
* Run jshint on all the files
*/
gulp.task ('jshint', 'Run jshint over the source .js files', [], function () {
return gulp.src ([
'./gulpfile.js',
'./src/**/*.js',
'./res/**/*.js',
'./config.js',
'./main.js'
])
.pipe (cached ('jshint')) // cache in memory
.pipe (_jshint (!argv.basic)) // pass in command line arg for 'stylish'
.pipe (_livereload ()); // pass to livereload
}, {options: {'basic': 'Use the basic processor for output, default is stylish'}});
/**
* Serve up the index.html and the rest of the project .js files (for debug running).
* This is our version of 'cocos run -p web', but with livereload, and jshinting.
*/
gulp.task ('serve', 'Serve the project files on port 8000, including livereload', ['watch']);
gulp.task ('static', serve ({
port: 8000,
root: ['./.tmp', '.']
}));
gulp.task ('watch', ['static', 'index', 'jshint'], function () {
isWatching = true;
livereload ({ start: true });
gulp.watch (['./*.js', 'src/**/*.js', 'res/**/*.js'], { interval: 2000 }, ['jshint']).on ('change', function (evt) {
if (evt.type !== 'changed') {
gulp.start ('index');
}
});
gulp.watch ('./index.html', ['index']);
});
/**
* Jshint with either default or stylish reporter (default works better in SlickEdit
* whereas stylish better in build logs)
*/
function _jshint (isStylish) {
var jshintfile = './.jshintrc';
var jshintSettings = JSON.parse (fs.readFileSync (jshintfile, 'utf8'));
return lazypipe ()
.pipe (jshint, jshintSettings)
.pipe (jshint.reporter, isStylish ? stylish : 'default') ();
}
/**
* Livereload (or noop if not run by watch)
*/
function _livereload () {
return lazypipe ()
.pipe (isWatching ? livereload : gutil.noop) ();
}