forked from CodeSeven/toastr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
172 lines (150 loc) · 4.3 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
/* jshint node:true, camelcase:false */
var gulp = require('gulp');
var karma = require('karma').server;
var merge = require('merge-stream');
var plug = require('gulp-load-plugins')();
var paths = {
js: './toastr.js',
less: './toastr.less',
report: './report',
build: './build'
};
var log = plug.util.log;
/**
* List the available gulp tasks
*/
gulp.task('help', plug.taskListing);
/**
* Lint the code, create coverage report, and a visualizer
* @return {Stream}
*/
gulp.task('analyze', function () {
log('Analyzing source with JSHint and JSCS');
var jshint = analyzejshint([paths.js]);
var jscs = analyzejscs([paths.js]);
return merge(jshint, jscs);
});
/**
* Minify and bundle the app's JavaScript
* @return {Stream}
*/
gulp.task('js', function () {
log('Bundling, minifying, and copying the app\'s JavaScript');
return gulp
.src(paths.js)
.pipe(plug.sourcemaps.init())
.pipe(plug.bytediff.start())
.pipe(plug.uglify({}))
.pipe(plug.bytediff.stop(bytediffFormatter))
.pipe(plug.sourcemaps.write('.'))
.pipe(plug.rename(function (path) {
if (path.extname === '.js') {
path.basename += '.min';
}
}))
.pipe(gulp.dest(paths.build));
});
/**
* Minify and bundle the CSS
* @return {Stream}
*/
gulp.task('css', function () {
log('Bundling, minifying, and copying the app\'s CSS');
return gulp.src(paths.less)
.pipe(plug.less())
.pipe(gulp.dest(paths.build))
.pipe(plug.bytediff.start())
.pipe(plug.minifyCss({}))
.pipe(plug.bytediff.stop(bytediffFormatter))
.pipe(plug.rename('toastr.min.css'))
.pipe(gulp.dest(paths.build));
});
/**
* Build js and css
*/
gulp.task('default', ['js', 'css'], function () {
log('Analyze, Build CSS and JS');
});
/**
* Remove all files from the build folder
* One way to run clean before all tasks is to run
* from the cmd line: gulp clean && gulp build
* @return {Stream}
*/
gulp.task('clean', function (cb) {
log('Cleaning: ' + plug.util.colors.blue(paths.report));
log('Cleaning: ' + plug.util.colors.blue(paths.build));
var delPaths = [paths.build, paths.report];
del(delPaths, cb);
});
/**
* Run specs once and exit
* To start servers and run midway specs as well:
* gulp test --startServers
* @return {Stream}
*/
gulp.task('test', function (done) {
startTests(true /*singleRun*/, done);
});
////////////////
/**
* Execute JSHint on given source files
* @param {Array} sources
* @param {String} overrideRcFile
* @return {Stream}
*/
function analyzejshint(sources, overrideRcFile) {
var jshintrcFile = overrideRcFile || './.jshintrc';
log('Running JSHint');
return gulp
.src(sources)
.pipe(plug.jshint(jshintrcFile))
.pipe(plug.jshint.reporter('jshint-stylish'));
}
/**
* Execute JSCS on given source files
* @param {Array} sources
* @return {Stream}
*/
function analyzejscs(sources) {
log('Running JSCS');
return gulp
.src(sources)
.pipe(plug.jscs('./.jscsrc'));
}
/**
* Start the tests using karma.
* @param {boolean} singleRun - True means run once and end (CI), or keep running (dev)
* @param {Function} done - Callback to fire when karma is done
* @return {undefined}
*/
function startTests(singleRun, done) {
karma.start({
configFile: __dirname + '/karma.conf.js',
singleRun: !!singleRun
}, karmaCompleted);
////////////////
function karmaCompleted() {
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 {Number} Formatted perentage
*/
function formatPercent(num, precision) {
return (num * 100).toFixed(precision);
}