-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
343 lines (302 loc) · 8.9 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
var args = require('yargs').argv;
var config = require('./gulp.config')();
var del = require('del');
var gulp = require('gulp');
var Server = require('karma').Server;
var $ = require('gulp-load-plugins')({lazy: true});
/**
* yargs variables can be passed in to alter the behavior, when present.
* Example: gulp serve-dev
*
* --verbose : Various tasks will produce more output to the console.
*/
/**
* List the available gulp tasks
*/
gulp.task('help', $.taskListing);
gulp.task('default', ['help']);
/**
* vet the code and create coverage report
* @return {Stream}
*/
gulp.task('vet', function() {
log('Analyzing source with JSHint and JSCS');
return gulp
.src(config.alljs)
.pipe($.if(args.verbose, $.print()))
.pipe($.jshint())
.pipe($.jshint.reporter('jshint-stylish', {verbose: true}))
.pipe($.jscs());
});
/**
* Compile sass to css
* @return {Stream}
*/
gulp.task('styles', [/*'clean-styles'*/], function(done) {
log('Compiling SASS --> CSS');
return gulp
.src(config.sass)
.pipe($.sass({
errLogToConsole: true
}))
.pipe(gulp.dest(config.temp));
});
/**
* Copy ionic fonts
* @return {Stream}
*/
gulp.task('fonts', ['clean-fonts'], function() {
log('Copying fonts');
return gulp
.src(config.fonts)
.pipe(gulp.dest(config.build + 'fonts'));
});
/**
* Compress images
* @return {Stream}
*/
gulp.task('images', ['clean-images'], function() {
log('Compressing and copying images');
return gulp
.src(config.images)
.pipe($.imagemin({optimizationLevel: 4}))
.pipe(gulp.dest(config.build + 'assets/images'));
});
/**
* Create $templateCache from the html templates
* @return {Stream}
*/
gulp.task('templatecache', ['clean-code'], function() {
log('Creating an AngularJS $templateCache');
return gulp
.src(config.htmltemplates)
.pipe($.if(args.verbose, $.bytediff.start()))
.pipe($.minifyHtml({empty: true}))
.pipe($.if(args.verbose, $.bytediff.stop(bytediffFormatter)))
.pipe($.angularTemplatecache(
config.templateCache.file,
config.templateCache.options
))
.pipe(gulp.dest(config.temp));
});
/**
* Wire-up the bower dependencies
* @return {Stream}
*/
gulp.task('wiredep', function() {
log('Wiring the bower dependencies into the html');
var wiredep = require('wiredep').stream;
var options = config.getWiredepDefaultOptions();
return gulp
.src(config.index)
.pipe(wiredep(options))
.pipe(inject(config.js, '', config.jsOrder))
.pipe(gulp.dest(config.src));
});
gulp.task('inject', ['wiredep', 'styles', 'templatecache'], function() {
log('Wire up css into the html, after files are ready');
return gulp
.src(config.index)
.pipe(inject(config.css))
.pipe(gulp.dest(config.src));
});
gulp.task('watch', function() {
gulp.watch(config.sass, ['styles']);
gulp.watch(config.alljs, ['vet']);
});
/**
* Build everything
* This is separate so we can run tests on
* optimize before handling image or fonts
*/
gulp.task('build', ['optimize', 'images', 'fonts'], function() {
log('Building everything');
var msg = {
title: 'gulp build',
subtitle: 'Deployed to the build folder'
};
del(config.temp);
log(msg);
});
/**
* Optimize all files, move to a build folder,
* and inject them into the new index.html
* @return {Stream}
*/
gulp.task('optimize', ['inject', 'test'], function() {
log('Optimizing the js, css, and html');
var assets = $.useref.assets();
var templateCache = config.temp + config.templateCache.file;
return gulp
.src(config.index)
.pipe($.plumber())
.pipe(inject(templateCache, 'templates'))
.pipe(assets)
.pipe($.if('*.css', $.minifyCss()))
// Fix ionic fonts path
.pipe($.if('*.css', $.cssUrlAdjuster({
replace: ['../lib/ionic/fonts','../fonts'],
})))
.pipe($.if('**/' + config.optimized.app, $.ngAnnotate({add: true})))
.pipe($.if('**/' + config.optimized.app, $.uglify()))
.pipe($.if('**/' + config.optimized.lib, $.uglify()))
.pipe(assets.restore())
.pipe($.useref())
.pipe(gulp.dest(config.build));
});
/**
* Remove all files from the build and temp folder
* @param {Function} done - callback when complete
*/
gulp.task('clean', ['clean-code'], function(done) {
var delconfig = [].concat(config.build + '**/*', config.temp);
log('Cleaning: ' + $.util.colors.blue(delconfig));
del(delconfig, done);
});
/**
* Remove all fonts from the build folder
* @param {Function} done - callback when complete
*/
gulp.task('clean-fonts', function(done) {
clean(config.build + 'fonts/**/*.*', done);
});
/**
* Remove all images from the build folder
* @param {Function} done - callback when complete
*/
gulp.task('clean-images', function(done) {
clean(config.build + 'images/**/*.*', done);
});
/**
* Remove all styles from the build and temp folders
* @param {Function} done - callback when complete
*/
gulp.task('clean-styles', function(done) {
var files = [].concat(
config.temp + '**/*.css',
config.build + 'styles/**/*.css'
);
clean(files, done);
});
/**
* Remove all js and html from the build and temp folders
* @param {Function} done - callback when complete
*/
gulp.task('clean-code', function(done) {
var files = [].concat(
config.temp + '**/*.js',
config.build + 'js/**/*.js',
config.build + '**/*.html'
);
clean(files, done);
});
/**
* Run specs once and exit
* To start servers and run midway specs as well:
* gulp test --startServers
* @return {Stream}
*/
gulp.task('test', ['vet', 'templatecache'], function(done) {
var excludeFiles = [];
new Server({
configFile: __dirname + '/karma.conf.js',
exclude: excludeFiles,
singleRun: true
}, function() {
done();
}).start();
});
gulp.task('install', ['git-check'], function() {
return $.bower.commands.install()
.on('log', function(data) {
$.gutil.log('bower', $.gutil.colors.cyan(data.id), data.message);
});
});
gulp.task('git-check', function(done) {
if (!$.shelljs.which('git')) {
console.log(
' ' + $.gutil.colors.red('Git is not installed.'),
'\n Git, the version control system, is required to download Ionic.',
'\n Download git here:', $.gutil.colors.cyan('http://git-scm.com/downloads') + '.',
'\n Once git is installed, run \'' + $.gutil.colors.cyan('gulp install') + '\' again.'
);
process.exit(1);
}
done();
});
////////////////
/**
* Formatter for bytediff to display the size changes after processing
* @param {Object} data - byte data
* @return {String} Difference in bytes, formatted
*/
function bytediffFormatter(data) {
var difference = (data.savings > 0) ? ' smaller.' : ' larger.';
return data.fileName + ' went from ' +
(data.startSize / 1000).toFixed(2) + ' kB to ' +
(data.endSize / 1000).toFixed(2) + ' kB and is ' +
formatPercent(1 - data.percent, 2) + '%' + difference;
}
/**
* Format a number as a percentage
* @param {Number} num Number to format as a percent
* @param {Number} precision Precision of the decimal
* @return {String} Formatted perentage
*/
function formatPercent(num, precision) {
return (num * 100).toFixed(precision);
}
/**
* Delete all files in a given path
* @param {Array} path - array of paths to delete
* @param {Function} done - callback when complete
*/
function clean(path, done) {
log('Cleaning: ' + $.util.colors.blue(path));
del(path, done);
}
/**
* Inject files in a sorted sequence at a specified inject label
* @param {Array} src glob pattern for source files
* @param {String} label The label name
* @param {Array} order glob pattern for sort order of the files
* @returns {Stream} The stream
*/
function inject(src, label, order) {
var options = {
read: false,
relative: true
};
if (label) {
options.name = 'inject:' + label;
}
return $.inject(orderSrc(src, order), options);
}
/**
* Order a stream
* @param {Stream} src The gulp.src stream
* @param {Array} order Glob array pattern
* @returns {Stream} The ordered stream
*/
function orderSrc (src, order) {
//order = order || ['**/*'];
return gulp
.src(src)
.pipe($.if(order, $.order(order)));
}
/**
* Log a message or series of messages using chalk's blue color.
* Can pass in a string, object or array.
*/
function log(msg) {
if (typeof(msg) === 'object') {
for (var item in msg) {
if (msg.hasOwnProperty(item)) {
$.util.log($.util.colors.blue(msg[item]));
}
}
} else {
$.util.log($.util.colors.blue(msg));
}
}
module.exports = gulp;