-
Notifications
You must be signed in to change notification settings - Fork 234
/
gulpfile.js
449 lines (393 loc) · 10.4 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
'use strict';
// config is in non-standard location. setting this env var will direct
// node-config to the proper config files.
process.env.NODE_CONFIG_DIR = './test/config';
var gulp = require('gulp');
var wiredep = require('wiredep').stream;
//var sprite = require('css-sprite').stream;
var config = require('config');
var cached = require('gulp-cached');
var es = require('event-stream');
var seq = require('run-sequence');
var lazypipe = require('lazypipe');
var nib = require('nib');
var ngAnnotate = require('gulp-ng-annotate');
var appDir = 'test/app/';
var distDir = 'test/dist/';
var tmpDir = 'test/.tmp/';
var componentSrcDir = 'src/';
var componentDistDir = 'dist/';
// for deployment
var env = (process.env.NODE_ENV || 'development').toLowerCase();
var tag = env + '-' + new Date().getTime();
var DIST_DIR = distDir;
var LIVERELOAD_PORT = 35729;
if (process.env.NODE_ENV) {
DIST_DIR = 'test/dist-'+process.env.NODE_ENV.toLowerCase();
}
// Load plugins
var $ = require('gulp-load-plugins')();
// Sass
gulp.task('sass', function () {
return gulp.src(appDir+'styles/deps.scss')
.pipe(cached('sass'))
.pipe($.rubySass({
style: 'expanded',
loadPath: [appDir+'bower_components']
}))
.pipe($.autoprefixer('last 1 version'))
.pipe(wiredep({
directory: appDir+'/bower_components',
ignorePath: appDir+'/bower_components/'
}))
.pipe(gulp.dest(tmpDir+'/styles'))
.pipe($.size());
});
// JS
gulp.task('js', function () {
return gulp.src(appDir+'scripts/**/*.js')
.pipe(cached('js'))
.pipe($.jshint('.jshintrc'))
.pipe($.jshint.reporter('default'))
.pipe(gulp.dest(tmpDir+'/scripts'))
.pipe($.size());
});
// Bower
gulp.task('bowerjs', function() {
return gulp.src(appDir+'bower_components/**/*.js')
.pipe(gulp.dest(tmpDir+'/bower_components'))
.pipe($.size());
});
gulp.task('bowercss', function() {
return gulp.src(appDir+'bower_components/**/*.css')
.pipe(gulp.dest(tmpDir+'/bower_components'))
.pipe($.size());
});
// TODO: what a mess. maybe move all fonts into one dir?
gulp.task('bower-fonts', function() {
return gulp.src([
appDir+'bower_components/bootstrap-sass-official/assets/fonts/bootstrap/*.*',
appDir+'bower_components/font-awesome/fonts/*.*'
])
.pipe(gulp.dest(tmpDir+'/fonts'))
.pipe($.size());
});
// CoffeeScript
gulp.task('coffee', function() {
return gulp.src(appDir+'scripts/**/*.coffee')
.pipe(cached('coffee'))
.pipe($.coffee({bare: true}))
.on('error', function(e) {
$.util.log(e.toString());
this.emit('end');
})
.pipe(gulp.dest(tmpDir+'/scripts'))
.pipe($.size());
});
gulp.task('component-coffee', function() {
return gulp.src(componentSrcDir+'**/*.coffee')
.pipe(cached('component-coffee'))
.pipe($.coffee({bare: true}))
.on('error', function(e) {
$.util.log(e.toString());
this.emit('end');
})
.pipe(ngAnnotate())
.pipe(gulp.dest(componentDistDir))
.pipe(gulp.dest(tmpDir + 'scripts/'))
.pipe($.uglify())
.pipe($.rename('ng-token-auth.min.js'))
.pipe(gulp.dest(componentDistDir))
.pipe($.size());
});
// Images
gulp.task('images', function () {
return gulp.src(appDir+'images/**/*')
.pipe(gulp.dest(tmpDir+'/images'))
.pipe($.size());
});
gulp.task('css', function() {
return gulp.src(appDir+'styles/**/*.css')
.pipe(gulp.dest(tmpDir+'/styles'))
.pipe($.size());
});
// Stylus
gulp.task('stylus', function() {
return gulp.src(appDir+'styles/main.styl')
.pipe($.stylus({
paths: [appDir+'styles', tmpDir+'/styles'],
//set: ['compress'],
use: [nib()],
import: [
//'sprite',
'globals/*.styl',
'pages/**/*.xs.styl',
'pages/**/*.sm.styl',
'pages/**/*.md.styl',
'pages/**/*.lg.styl',
'degrade.styl'
]
}))
.on('error', function(e) {
$.util.log(e.toString());
this.emit('end');
})
.pipe(gulp.dest(tmpDir+'/styles'))
.pipe($.size());
});
// Clean
gulp.task('clean', function () {
return gulp.src([distDir+'/*', tmpDir+'/*'], {read: false}).pipe($.clean());
});
// Transpile
gulp.task('transpile', [
'stylus',
'coffee',
'component-coffee',
'js',
'css',
'bowerjs',
'bowercss',
'bower-fonts'
]);
// jade -> html
var jadeify = lazypipe()
.pipe($.jade, {
pretty: true
});
// inject global js vars
var injectGlobals = lazypipe()
.pipe($.frep, [
{
pattern: '@@GLOBALS',
replacement: JSON.stringify({
apiUrl: config.API_URL
})
}
]);
// Jade to HTML
gulp.task('base-tmpl', function() {
return gulp.src(appDir+'index.jade')
.pipe($.changed(tmpDir))
.pipe(jadeify())
.pipe(injectGlobals())
.pipe($.inject($.bowerFiles({
paths: {bowerJson: 'test/bower.json'},
read: false
}), {
ignorePath: [appDir],
starttag: '<!-- bower:{{ext}}-->',
endtag: '<!-- endbower-->'
}))
.pipe($.inject(gulp.src(
[
tmpDir+'/views/**/*.js',
tmpDir+'/scripts/**/*.js',
tmpDir+'/styles/**/*.css'
],
{read: false}
), {
ignorePath: [tmpDir],
starttag: '<!-- inject:{{ext}}-->',
endtag: '<!-- endinject-->'
}))
.pipe(gulp.dest(tmpDir))
.pipe($.size());
});
// Jade to JS
gulp.task('js-tmpl', function() {
return gulp.src(appDir+'views/**/*.jade')
.pipe(cached('js-tmpl'))
.pipe(jadeify())
.pipe($.ngHtml2js({
moduleName: 'ngTokenAuthTestPartials'
}))
.pipe(gulp.dest(tmpDir+'/views'));
});
// useref
gulp.task('useref', function () {
$.util.log('running useref');
var jsFilter = $.filter(tmpDir+'/**/*.js');
var cssFilter = $.filter(tmpDir+'/**/*.css');
return es.merge(
gulp.src(tmpDir+'/images/**/*.*', {base: tmpDir}),
gulp.src(tmpDir+'/fonts/**/*.*', {base: tmpDir}),
gulp.src(tmpDir+'/index.html', {base: tmpDir})
.pipe($.useref.assets())
.pipe(jsFilter)
.pipe($.uglify())
.pipe(jsFilter.restore())
.pipe(cssFilter)
.pipe($.minifyCss())
.pipe(cssFilter.restore())
.pipe($.useref.restore())
.pipe($.useref())
)
.pipe(gulp.dest(tmpDir))
.pipe($.if(/^((?!(index\.html)).)*$/, $.rev()))
.pipe(gulp.dest(distDir))
.pipe($.rev.manifest())
.pipe(gulp.dest(tmpDir))
.pipe($.size());
});
// Update file version refs
gulp.task('replace', function() {
var manifest = require('./'+tmpDir+'rev-manifest');
var patterns = [];
for (var k in manifest) {
patterns.push({
pattern: k,
replacement: manifest[k]
});
}
return gulp.src([
distDir+'/*.html',
distDir+'/styles/**/*.css',
distDir+'/scripts/main*.js'
], {base: distDir})
.pipe($.frep(patterns))
.pipe(gulp.dest(distDir))
.pipe($.size());
});
// CDNize
gulp.task('cdnize', function() {
return gulp.src([
distDir+'/*.html',
distDir+'/styles/**/*.css'
], {base: distDir})
.pipe($.cdnizer({
defaultCDNBase: config.STATIC_URL,
allowRev: true,
allowMin: true,
files: ['**/*.*']
}))
.pipe(gulp.dest(distDir))
.pipe($.size());
});
// Deployment
gulp.task('s3', function() {
var headers = {
'Cache-Control': 'max-age=315360000, no-transform, public'
};
var publisher = $.awspublish.create({
key: config.AWS_KEY,
secret: config.AWS_SECRET,
bucket: config.AWS_STATIC_BUCKET_NAME
});
return gulp.src(distDir+'/**/*')
.pipe($.awspublish.gzip())
.pipe(publisher.publish(headers))
.pipe(publisher.sync())
//.pipe(publisher.cache())
.pipe($.awspublish.reporter());
});
// Push to heroku
gulp.task('push', $.shell.task([
'git checkout -b '+tag,
'cp -R '+distDir+' '+DIST_DIR,
'cp test/config/'+env+'.yml test/config/default.yml',
'git add -u .',
'git add .',
'git commit -am "commit for '+tag+' push"',
'git push -f '+env+' '+tag+':master',
'git checkout master',
'git branch -D '+tag,
'rm -rf '+DIST_DIR
]));
// E2E Protractor tests
gulp.task('protractor', function() {
require('coffee-script/register');
return gulp.src('test/e2e/**/*.coffee')
.pipe($.protractor.protractor({
configFile: 'protractor.conf.js'
}))
.on('error', function(e) {
$.util.log(e.toString());
this.emit('end');
});
});
gulp.task('test:e2e', ['protractor'], function() {
gulp.watch('test/e2e/**/*.coffee', ['protractor']);
});
// Watch
gulp.task('watch', function () {
var lr = require('tiny-lr')();
// start node server
$.nodemon({
script: 'test/app.js',
ext: 'html js',
ignore: [],
watch: []
})
.on('restart', function() {
console.log('restarted');
});
// start livereload server
lr.listen(LIVERELOAD_PORT);
// Watch for changes in .tmp folder
gulp.watch([
tmpDir+'/*.html',
tmpDir+'/styles/**/*.css',
tmpDir+'/scripts/**/*.js',
tmpDir+'/images/**/*.*'
], function(event) {
gulp.src(event.path, {read: false})
.pipe($.livereload(lr));
});
// Watch .scss files
gulp.watch(appDir+'styles/**/*.scss', ['sass']);
// Watch .styl files
gulp.watch(appDir+'styles/**/*.styl', ['stylus']);
// Watch sprites
//gulp.watch(appDir+'images/sprites/**/*.png', ['sprites']);
// Watch .js files
gulp.watch(appDir+'scripts/**/*.js', ['js']);
// Watch .coffee files
gulp.watch(appDir+'scripts/**/*.coffee', ['coffee']);
// Watch bower component
gulp.watch(componentSrcDir+'**/*.coffee', ['component-coffee']);
// Watch .jade files
gulp.watch(appDir+'index.jade', ['base-tmpl']);
gulp.watch(appDir+'views/**/*.jade', ['reload-js-tmpl']);
// Watch image files
gulp.watch(appDir+'images/**/*', ['images']);
// Watch bower files
gulp.watch(appDir+'bower_components/*', ['bowerjs', 'bowercss']);
});
// Composite tasks
// TODO: refactor when gulp adds support for synchronous tasks.
// https://github.com/gulpjs/gulp/issues/347
gulp.task('build-dev', function(cb) {
seq(
'clean',
//'sprites',
'images',
'sass',
'transpile',
'js-tmpl',
'base-tmpl',
cb
);
});
gulp.task('dev', function(cb) {
seq('build-dev', 'watch', cb);
});
gulp.task('reload-js-tmpl', function(cb) {
seq('js-tmpl', 'base-tmpl', cb);
});
gulp.task('build-prod', function(cb) {
seq(
'build-dev',
'useref',
'replace',
//'cdnize',
//'s3',
cb
);
});
gulp.task('deploy', function(cb) {
if (!process.env.NODE_ENV) {
throw 'Error: you forgot to set NODE_ENV';
}
seq('build-prod', 'push', cb);
});