@@ -23,6 +23,9 @@ var http = require('http');
23
23
var open = require ( 'open' ) ;
24
24
var path = require ( 'path' ) ;
25
25
26
+ var execFile = require ( 'child_process' ) . execFile ;
27
+ var fs = require ( 'fs' ) ;
28
+
26
29
/**
27
30
* Tasks:
28
31
*
@@ -45,170 +48,208 @@ var path = require('path');
45
48
gulp . task ( 'default' , [ 'build' ] ) ;
46
49
gulp . task ( 'build' , [ 'css' , 'less' , 'bad-scripts' , 'workers' , 'lint' , 'scripts' , 'resources' , 'html' , 'docs' ] ) ;
47
50
gulp . task ( 'develop' , [ 'build' , 'serve' , 'livereload' ] ) ;
51
+ gulp . task ( 'tdd' , [ 'serve-specs' , 'livereload-tests' ] ) ;
48
52
49
53
50
54
/**
51
55
* path globs / expressions for targets below
52
56
*/
53
57
54
58
var paths = {
55
- main : 'js/client.js' ,
56
- sources : 'js/**/*.js' ,
57
- jsx : 'js/**/*.jsx' ,
58
- badScripts : [ 'vendor/bluebird.js' , 'vendor/laz-perf.js' ] ,
59
- workers : 'workers/**' ,
60
- resources : 'resources/**' ,
61
- css : 'less/**/*.css' ,
62
- less : 'less/style.less' ,
63
- jade : 'client/**/*.jade' ,
64
- html : '*.html' ,
65
- docs : 'docs/**/*' ,
66
- build : './build/'
59
+ main : 'js/client.js' ,
60
+ sources : 'js/**/*.js' ,
61
+ jsx : 'js/**/*.jsx' ,
62
+ specs : 'test/spec/specs.js' ,
63
+ badScripts : [ 'vendor/bluebird.js' , 'vendor/laz-perf.js' ] ,
64
+ workers : 'workers/**' ,
65
+ resources : 'resources/**' ,
66
+ css : 'less/**/*.css' ,
67
+ less : 'less/style.less' ,
68
+ jade : 'client/**/*.jade' ,
69
+ html : '*.html' ,
70
+ docs : 'docs/**/*' ,
71
+ build : './build/'
67
72
} ;
68
73
69
74
70
75
//clean build directory
71
76
gulp . task ( 'clean' , function ( ) {
72
- return gulp . src ( paths . client . build , { read : false } )
73
- . pipe ( clean ( ) ) ;
77
+ return gulp . src ( paths . client . build , { read : false } )
78
+ . pipe ( clean ( ) ) ;
74
79
} ) ;
75
80
76
81
gulp . task ( 'resources' , function ( ) {
77
- return gulp . src ( paths . resources )
78
- . pipe ( gulp . dest ( paths . build ) ) ;
82
+ return gulp . src ( paths . resources )
83
+ . pipe ( gulp . dest ( paths . build ) ) ;
79
84
} ) ;
80
85
81
86
// lint all of our js source files
82
87
gulp . task ( 'lint' , function ( ) {
83
- return gulp . src ( paths . sources )
88
+ return gulp . src ( paths . sources )
84
89
. pipe ( jshint ( {
85
- "smarttabs" : true
86
- } ) )
90
+ "smarttabs" : true
91
+ } ) )
87
92
. pipe ( jshint . reporter ( 'default' ) ) ;
88
93
} ) ;
89
94
90
- var startServer = function ( cb ) {
91
- var devApp , devServer , devAddress , devHost , url , log = gutil . log , colors = gutil . colors ;
92
- devApp = connect ( ) ;
93
- devApp . use ( connect . logger ( 'dev' ) ) ;
94
- devApp . use ( connect . static ( paths . build ) ) ;
95
- devServer = http . createServer ( devApp ) . listen ( 8000 ) ;
96
- devServer . on ( 'error' , function ( error ) {
97
- log ( colors . underline ( colors . red ( 'ERROR' ) ) + ' Unable to start server!' ) ;
98
- cb ( error ) ;
99
- } ) ;
100
-
101
- devServer . on ( 'listening' , function ( ) {
102
- devAddress = devServer . address ( ) ;
103
- devHost = devAddress . address === '0.0.0.0' ? 'localhost' : devAddress . address ;
104
- url = 'http://' + devHost + ':' + devAddress . port + '/index.html' ;
105
-
106
- log ( '' ) ;
107
- log ( 'Started dev server at ' + colors . magenta ( url ) ) ;
108
- open ( url ) ;
109
- cb ( ) ;
110
- } ) ;
95
+ var startServer = function ( path , cb ) {
96
+ var devApp , devServer , devAddress , devHost , url , log = gutil . log , colors = gutil . colors ;
97
+ devApp = connect ( ) ;
98
+ devApp . use ( connect . logger ( 'dev' ) ) ;
99
+ devApp . use ( connect . static ( path ) ) ;
100
+ devServer = http . createServer ( devApp ) . listen ( 8000 ) ;
101
+ devServer . on ( 'error' , function ( error ) {
102
+ log ( colors . underline ( colors . red ( 'ERROR' ) ) + ' Unable to start server!' ) ;
103
+ cb ( error ) ;
104
+ } ) ;
105
+
106
+ devServer . on ( 'listening' , function ( ) {
107
+ devAddress = devServer . address ( ) ;
108
+ devHost = devAddress . address === '0.0.0.0' ? 'localhost' : devAddress . address ;
109
+ url = 'http://' + devHost + ':' + devAddress . port + '/' + ' index.html';
110
+
111
+ log ( '' ) ;
112
+ log ( 'Started dev server at ' + colors . magenta ( url ) ) ;
113
+ open ( url ) ;
114
+ cb ( ) ;
115
+ } ) ;
111
116
} ;
112
117
113
118
gulp . task ( 'serve' , [ 'build' ] , function ( cb ) {
114
- startServer ( cb ) ;
119
+ startServer ( 'build' , cb ) ;
120
+ } ) ;
121
+
122
+ gulp . task ( 'serve-specs' , [ 'build-specs' ] , function ( cb ) {
123
+ startServer ( 'test' , cb ) ;
115
124
} ) ;
116
125
117
126
gulp . task ( 'watch' , [ 'build' ] , function ( ) {
118
- // watch all our dirs and reload if any build stuff changes
119
- //
120
- gulp . watch ( paths . sources , [ 'lint' , 'scripts' ] ) ;
121
- gulp . watch ( paths . html , [ 'html' ] ) ;
122
- gulp . watch ( paths . less , [ 'less' ] ) ;
123
- gulp . watch ( paths . docs , [ 'docs' ] ) ;
124
- gulp . watch ( paths . workers , [ 'workers' ] ) ;
125
- gulp . watch ( paths . resources , [ 'resources' ] ) ;
126
- } )
127
+ // watch all our dirs and reload if any build stuff changes
128
+ //
129
+ gulp . watch ( paths . sources , [ 'lint' , 'scripts' ] ) ;
130
+ gulp . watch ( paths . html , [ 'html' ] ) ;
131
+ gulp . watch ( paths . less , [ 'less' ] ) ;
132
+ gulp . watch ( paths . docs , [ 'docs' ] ) ;
133
+ gulp . watch ( paths . workers , [ 'workers' ] ) ;
134
+ gulp . watch ( paths . resources , [ 'resources' ] ) ;
135
+ } ) ;
127
136
128
137
gulp . task ( 'livereload' , [ 'watch' ] , function ( ) {
129
- var server = livereload ( ) ;
130
- return gulp . watch ( path . join ( paths . build , "/**/*" ) , function ( evt ) {
131
- server . changed ( evt . path ) ;
132
- } ) ;
138
+ var server = livereload ( ) ;
139
+ return gulp . watch ( path . join ( paths . build , "/**/*" ) , function ( evt ) {
140
+ server . changed ( evt . path ) ;
141
+ } ) ;
142
+ } ) ;
143
+
144
+ gulp . task ( 'livereload-tests' , [ 'watch-specs' ] , function ( ) {
145
+ var server = livereload ( ) ;
146
+
147
+ return gulp . watch ( "test/build/**/*" , function ( evt ) {
148
+ server . changed ( evt . path ) ;
149
+ } ) ;
133
150
} ) ;
134
151
135
152
gulp . task ( 'bad-scripts' , function ( ) {
136
- return gulp . src ( paths . badScripts )
137
- . pipe ( concat ( "bad.js" ) )
138
- . pipe ( gulp . dest ( paths . build ) ) ;
153
+ return gulp . src ( paths . badScripts )
154
+ . pipe ( concat ( "bad.js" ) )
155
+ . pipe ( gulp . dest ( paths . build ) ) ;
156
+ } ) ;
157
+
158
+ // build client side js app
159
+ gulp . task ( 'build-specs' , function ( ) {
160
+ return gulp . src ( [ paths . specs ] )
161
+ . pipe ( browserify ( ) )
162
+ . on ( "error" , gutil . log )
163
+ . on ( "error" , gutil . beep )
164
+ . pipe ( gulp . dest ( "test/build" ) ) ;
165
+ } ) ;
166
+
167
+
168
+ gulp . task ( 'watch-specs' , function ( ) {
169
+ gulp . watch ( [ paths . sources , 'test/spec/**/*.js' ] , [ 'build-specs' ] ) ;
139
170
} ) ;
140
171
141
172
// build client side js app
142
173
gulp . task ( 'scripts' , function ( ) {
143
- return gulp . src ( [ paths . main , paths . jsx ] )
144
- . pipe ( browserify ( {
145
- debug : gulp . env . production ,
146
- transform : [ 'reactify' ]
147
- } ) )
148
- . on ( "error" , gutil . log )
149
- . on ( "error" , gutil . beep )
150
- . pipe ( gulp . dest ( paths . build ) ) ;
174
+ return gulp . src ( [ paths . main , paths . jsx ] )
175
+ . pipe ( browserify ( {
176
+ debug : gulp . env . production ,
177
+ transform : [ 'reactify' ]
178
+ } ) )
179
+ . on ( "error" , gutil . log )
180
+ . on ( "error" , gutil . beep )
181
+ . pipe ( gulp . dest ( paths . build ) ) ;
151
182
} ) ;
152
183
153
184
gulp . task ( 'css' , function ( ) {
154
- return gulp . src ( paths . css ) .
155
- pipe ( concat ( 'all.css' ) ) .
156
- pipe ( gulp . dest ( path . join ( paths . build , 'css' ) ) ) ;
185
+ return gulp . src ( paths . css ) .
186
+ pipe ( concat ( 'all.css' ) ) .
187
+ pipe ( gulp . dest ( path . join ( paths . build , 'css' ) ) ) ;
157
188
} ) ;
158
189
159
190
gulp . task ( 'less' , function ( ) {
160
- return gulp . src ( paths . less ) .
161
- pipe ( less ( {
162
- paths : [ './less/' ]
163
- } ) ) .
164
- pipe ( gulp . dest ( path . join ( paths . build , 'css' ) ) ) ;
191
+ return gulp . src ( paths . less ) .
192
+ pipe ( less ( {
193
+ paths : [ './less/' ]
194
+ } ) ) .
195
+ pipe ( gulp . dest ( path . join ( paths . build , 'css' ) ) ) ;
165
196
} ) ;
166
197
167
198
gulp . task ( 'html' , function ( ) {
168
- return gulp . src ( paths . html ) .
169
- pipe ( gulp . dest ( paths . build ) ) ;
199
+ return gulp . src ( paths . html ) .
200
+ pipe ( gulp . dest ( paths . build ) ) ;
170
201
} ) ;
171
202
172
203
gulp . task ( 'clean' , function ( ) {
173
- return gulp . src ( paths . build , { read : false } )
174
- . pipe ( clean ( ) ) ;
204
+ return gulp . src ( paths . build , { read : false } )
205
+ . pipe ( clean ( ) ) ;
175
206
} ) ;
176
207
177
- gulp . task ( 'uglify-built' , [ 'build' ] , function ( ) {
178
- return gulp . src ( path . join ( paths . build , 'client.js' ) ) .
179
- pipe ( uglify ( { outSourceMap : true } ) ) .
180
- pipe ( gulp . dest ( paths . build ) ) ;
208
+ gulp . task ( 'optimize' , [ 'build' ] , function ( cb ) {
209
+ var input = path . join ( paths . build , 'client.js' ) ;
210
+ var tmp = path . join ( paths . build , 'client.tmp.js' ) ;
211
+
212
+ execFile ( 'java' , [
213
+ '-jar' , 'vendor/closure-compiler/compiler.jar' ,
214
+ '--js' , input ,
215
+ '--language_in' , 'ECMASCRIPT5' ,
216
+ '--compilation_level' , 'SIMPLE_OPTIMIZATIONS' ,
217
+ '--js_output_file' , tmp ] ,
218
+ { maxBuffer : ( 1000 * 4096 ) } ,
219
+ function ( err , stdout , stderr ) {
220
+ if ( err )
221
+ return cb ( err ) ;
222
+
223
+ fs . unlinkSync ( input ) ;
224
+ fs . renameSync ( tmp , input ) ;
225
+
226
+ cb ( ) ;
227
+ } ) ;
181
228
} ) ;
182
229
183
230
gulp . task ( 'docs' , function ( ) {
184
- return gulp . src ( paths . docs ) .
185
- pipe ( gulp . dest ( path . join ( paths . build , 'docs' ) ) ) ;
231
+ return gulp . src ( paths . docs ) .
232
+ pipe ( gulp . dest ( path . join ( paths . build , 'docs' ) ) ) ;
186
233
} ) ;
187
234
188
235
gulp . task ( 'workers' , function ( ) {
189
- return gulp . src ( paths . workers ) .
190
- pipe ( gulp . dest ( path . join ( paths . build , 'workers' ) ) ) ;
236
+ return gulp . src ( paths . workers ) .
237
+ pipe ( gulp . dest ( path . join ( paths . build , 'workers' ) ) ) ;
191
238
} ) ;
192
239
193
- gulp . task ( 'prod-react-built' , [ 'build' ] , function ( ) {
194
- return gulp . src ( path . join ( paths . build , 'index.html' ) ) .
195
- pipe ( htmlreplace ( {
196
- reactjs : '//cdnjs.cloudflare.com/ajax/libs/react/0.10.0/react-with-addons.min.js'
197
- } ) ) .
198
- pipe ( gulp . dest ( paths . build ) ) ;
199
- } ) ;
240
+ gulp . task ( 'prod-build' , [ 'build' , 'optimize' ] ) ;
200
241
201
- gulp . task ( 'publish' , [ 'build' , 'uglify-built' , ' prod-react-built '] , function ( ) {
202
- var homeDir = process . env [ 'HOME' ] ;
203
- var settings = require ( path . join ( homeDir , ".aws.json" ) ) ;
242
+ gulp . task ( 'publish' , [ 'prod-build ' ] , function ( ) {
243
+ var homeDir = process . env [ 'HOME' ] ;
244
+ var settings = require ( path . join ( homeDir , ".aws.json" ) ) ;
204
245
205
- settings . bucket = "plas.io" ;
246
+ settings . bucket = "plas.io" ;
206
247
207
- var publisher = awspublish . create ( settings ) ;
248
+ var publisher = awspublish . create ( settings ) ;
208
249
209
- return gulp . src ( paths . build + "/**/*" )
210
- . pipe ( publisher . publish ( ) )
211
- . pipe ( publisher . sync ( ) )
212
- . pipe ( awspublish . reporter ( ) ) ;
250
+ return gulp . src ( paths . build + "/**/*" )
251
+ . pipe ( publisher . publish ( ) )
252
+ . pipe ( publisher . sync ( ) )
253
+ . pipe ( awspublish . reporter ( ) ) ;
213
254
} ) ;
214
255
0 commit comments