-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
392 lines (357 loc) · 10.8 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
/* Copyright 2014(c) The Ontario Institute for Cancer Research. All rights reserved.
*
* This program and the accompanying materials are made available under the terms of the GNU Public
* License v3.0. You should have received a copy of the GNU General Public License along with this
* program. If not, see <http://www.gnu.org/licenses/>.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* jshint node: true */
'use strict';
var gulp = require('gulp'),
fs = require('fs'),
g = require('gulp-load-plugins')({lazy: false}),
noop = g.util.noop,
es = require('event-stream'),
queue = require('streamqueue'),
lazypipe = require('lazypipe'),
stylish = require('jshint-stylish'),
bower = require('./bower'),
mainBowerFiles = require('main-bower-files'),
url = require('url'),
proxy = require('proxy-middleware'),
gulpif = require('gulp-if'),
isWatching = false,
Server = require('karma').Server;
// When we have a URL that matches ^/api/, wejust forward, otherwise we
// do the pushstate thing. This allows the embedded service/nodemon to
// pick up immediately.
var conditionalPushStateMiddleware = function(req, res, next) {
var pathname = url.parse(req.url).pathname;
if (req.url === '/') {
fs.createReadStream('.tmp/index.html').pipe(res);
} else if (!pathname.match(/^\/api\//) && !pathname.match(/\.(jpe?g|png|css|js|html?|woff|ttf|svg|map)$/i)) {
fs.createReadStream('.tmp/index.html').pipe(res);
} else {
next();
}
};
// Proxy for /api/ from 3000 -> 3001.
var proxyOptions = url.parse('http://localhost:3001/api');
proxyOptions.route = '/api';
var htmlminOpts = {
removeComments: true,
collapseWhitespace: true,
removeEmptyAttributes: false,
collapseBooleanAttributes: true,
removeRedundantAttributes: true
};
/**
* JS Hint
*/
gulp.task('jshint', function () {
return gulp.src([
'./gulpfile.js',
'./src/app/**/*.js'
])
.pipe(g.cached('jshint'))
.pipe(jshint('./.jshintrc'))
.pipe(livereload());
});
/**
* CSS
*/
gulp.task('clean-css', function () {
return gulp.src('./.tmp/css').pipe(g.clean());
});
gulp.task('styles', ['clean-css'], function () {
return gulp.src([
'./src/app/**/*.scss',
'!./src/app/**/_*.scss'
])
.pipe(g.sass())
.pipe(gulp.dest('./.tmp/css/'))
.pipe(g.cached('built-css'))
.pipe(livereload());
});
gulp.task('styles-dist', ['styles'], function () {
return cssFiles().pipe(dist('css', bower.name));
});
gulp.task('csslint', ['styles'], function () {
return cssFiles()
.pipe(g.cached('csslint'))
.pipe(g.csslint('./.csslintrc'));
//.pipe(g.csslint.reporter()); Appears to have been deprecated
});
/**
* CoffeeScript
*/
gulp.task('coffee', function () {
return gulp.src([
'./src/app/**/*.*coffee'
])
.pipe(g.coffee())
.pipe(gulp.dest('./.tmp/src/app'));
});
gulp.task('coffee-service', function () {
return gulp.src([
'./src/service/**/*.*'
])
.pipe(gulpif(/[.](?:lit)?coffee$/, g.coffee()))
.pipe(gulp.dest('./.tmp/src/service'));
});
gulp.task('service-dist', function() {
return serviceFiles().pipe(gulp.dest('./dist'));
});
gulp.task('common', function() {
return gulp.src([
'./src/common/**/*.js'
])
.pipe(gulp.dest('./.tmp/src/app'))
.pipe(gulp.dest('./.tmp/src/service'));
});
/**
* Scripts
*/
gulp.task('scripts-dist', ['templates-dist'], function () {
return appFiles().pipe(dist('js', bower.name, {ngmin: true}));
});
/**
* Templates
*/
gulp.task('templates', function () {
return templateFiles().pipe(buildTemplates());
});
gulp.task('templates-dist', function () {
return templateFiles({min: true}).pipe(buildTemplates());
});
/**
* Vendors
*/
gulp.task('vendors', function () {
var bowerStream = gulp.src(mainBowerFiles()).pipe(g.ignore.exclude('bower_components/bootstrap/**/*.*'));
var bootstrapStream = gulp.src('bower_components/bootstrap/less/bootstrap.less');
var fontsStream = gulp.src('bower_components/bootstrap/fonts/*.*');
var lessPaths = [ 'src/app/less', 'bower_components/bootstrap/less' ];
return es.merge(
es.merge(
bootstrapStream.pipe(g.rename({dirname: 'src/app/less'})).pipe(g.less({paths: lessPaths})),
bowerStream.pipe(g.filter('**/*.css'))
).pipe(dist('css', 'vendors')),
bowerStream.pipe(g.filter('**/*.js')).pipe(dist('js', 'vendors')),
fontsStream.pipe(gulp.dest('./dist/statics/fonts'))
);
});
/**
* Index
*/
gulp.task('index', index);
gulp.task('build-all', ['styles', 'templates', 'common', 'coffee', 'coffee-service'], index);
function index () {
var opt = {read: false};
return gulp.src('./src/app/index.html')
.pipe(g.inject(gulp.src(mainBowerFiles(opt), {read: false}), {ignorePath: 'bower_components', starttag: '<!-- inject:vendor:{{ext}} -->'}))
.pipe(g.inject(es.merge(appFiles(), cssFiles(opt)), {ignorePath: ['.tmp', 'src/app']}))
.pipe(gulp.dest('./src/app/'))
.pipe(g.embedlr())
.pipe(gulp.dest('./.tmp/'))
.pipe(livereload());
}
/**
* Assets
*/
gulp.task('assets', function () {
return gulp.src('./src/app/assets/**')
.pipe(gulp.dest('./dist/statics'));
});
/**
* Dist
*/
gulp.task('dist', ['vendors', 'assets', 'styles-dist', 'scripts-dist', 'service-dist'], function () {
return gulp.src('./src/app/index.html')
.pipe(g.inject(gulp.src('./dist/statics/vendors.min.{js,css}', {read: false}), {ignorePath: 'dist', starttag: '<!-- inject:vendor:{{ext}} -->'}))
.pipe(g.inject(gulp.src('./dist/statics/' + bower.name + '.min.{js,css}', {read: false}), {ignorePath: 'dist'}))
.pipe(g.htmlmin(htmlminOpts))
.pipe(gulp.dest('./dist/statics/'));
});
/**
* Static file server
*/
gulp.task('statics', g.serve({
port: 3000,
root: ['./.tmp', './.tmp/src/app', './src/app', './bower_components'],
middlewares: [conditionalPushStateMiddleware, proxy(proxyOptions)]
}));
/**
* Watch
*/
gulp.task('serve', ['watch']);
gulp.task('watch', ['statics', 'default'], function () {
isWatching = true;
// Initiate livereload server:
g.livereload();
gulp.watch('./src/app/**/*.*coffee', ['coffee']).on('change', function (evt) {
if (evt.type !== 'changed') {
gulp.start('index');
}
});
gulp.watch('./src/app/**/*.js', ['jshint']).on('change', function (evt) {
if (evt.type !== 'changed') {
gulp.start('index');
}
});
gulp.watch('./src/common/**/*.js', ['jshint', 'common']).on('change', function (evt) {
if (evt.type !== 'changed') {
gulp.start('index');
}
});
gulp.watch('./src/service/**/*.*coffee', ['coffee-service']);
gulp.watch('./src/app/index.html', ['index']);
gulp.watch(['./src/app/**/*.html', '!./src/app/index.html'], ['templates']);
gulp.watch(['./src/app/**/*.scss'], ['csslint']).on('change', function (evt) {
if (evt.type !== 'changed') {
gulp.start('index');
}
});
g.nodemon({ script: '.tmp/src/service/main.js', watch: ['.tmp/src/service'] })
.on('restart', function () {
console.log('restarted!');
});
});
/**
* Default task
*/
gulp.task('default', ['lint', 'build-all']);
/**
* Lint everything
*/
gulp.task('lint', ['jshint', 'csslint']);
/**
* Test
*/
gulp.task('test', ['templates', 'coffee'], function (done) {/*
return testFiles()
.pipe(g.karma({
configFile: 'karma.conf.js',
action: 'run'
}));*/
new Server({
configFile: __dirname + '/karma.conf.js',
action: 'run'
}, done).start();
});
/**
* Inject all files for tests into karma.conf.js
* to be able to run `karma` without gulp.
*/
gulp.task('karma-conf', ['templates'], function () {
return gulp.src('./karma.conf.js')
.pipe(g.inject(testFiles(), {
starttag: 'files: [',
endtag: ']',
addRootSlash: false,
transform: function (filepath, file, i, length) {
return ' \'' + filepath + '\'' + (i + 1 < length ? ',' : '');
}
}))
.pipe(gulp.dest('./'));
});
/**
* Test files
*/
function testFiles() {
return new queue({objectMode: true})
.queue(gulp.src(mainBowerFiles()).pipe(g.filter('**/*.js')))
.queue(gulp.src('./bower_components/angular-mocks/angular-mocks.js'))
.queue(appFiles())
.queue(gulp.src(['./src/app/**/*_test.js', './.tmp/src/app/**/*_test.js']))
.done();
}
/**
* All CSS files as a stream
*/
function cssFiles (opt) {
return gulp.src('./.tmp/css/**/*.css', opt);
}
/**
* All AngularJS application files as a stream
*/
function appFiles () {
var files = [
'./.tmp/' + bower.name + '-templates.js',
'./.tmp/src/app/**/*.js',
'!./.tmp/src/app/**/*_test.js',
'./src/app/**/*.js',
'!./src/app/**/*_test.js'
];
return gulp.src(files)
.pipe(g.angularFilesort());
}
/**
* All AngularJS templates/partials as a stream
*/
function templateFiles (opt) {
return gulp.src(['./src/app/**/*.html', '!./src/app/index.html'], opt)
.pipe(opt && opt.min ? g.htmlmin(htmlminOpts) : noop());
}
/**
* The service files as a stream
*/
function serviceFiles() {
return gulp.src(['./package.json', './.tmp/src/service/**/*.js', '!./.tmp/src/service/**/*_test.js']);
}
/**
* Build AngularJS templates/partials
*/
function buildTemplates () {
return lazypipe()
.pipe(g.ngHtml2js, {
moduleName: bower.name + '-templates',
prefix: '/' + bower.name + '/',
stripPrefix: '/src/app'
})
.pipe(g.concat, bower.name + '-templates.js')
.pipe(gulp.dest, './.tmp')
.pipe(livereload)();
}
/**
* Concat, rename, minify
*
* @param {String} ext
* @param {String} name
* @param {Object} opt
*/
function dist (ext, name, opt) {
opt = opt || {};
return lazypipe()
.pipe(g.concat, name + '.' + ext)
.pipe(gulp.dest, './dist/statics')
.pipe(opt.ngAnnotate ? g.ngAnnotate : noop)
.pipe(opt.ngAnnotate ? g.rename : noop, name + '.annotated.' + ext)
.pipe(opt.ngAnnotate ? gulp.dest : noop, './dist/statics')
.pipe(ext === 'js' ? g.uglify : g.cleanCss)
.pipe(g.rename, name + '.min.' + ext)
.pipe(gulp.dest, './dist/statics')();
}
/**
* Livereload (or noop if not run by watch)
*/
function livereload () {
return lazypipe()
.pipe(isWatching ? g.livereload : noop)();
}
/**
* Jshint with stylish reporter
*/
function jshint (jshintfile) {
return lazypipe()
.pipe(g.jshint, jshintfile)
.pipe(g.jshint.reporter, stylish)();
}