-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathgulpfile.js
186 lines (169 loc) · 4.77 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
var gulp = require('gulp');
var args = require('yargs').argv;
var config = require('./gulp.config')().getConfig();
var del = require('del');
var glob = require('glob');
var _ = require('lodash');
var path = require('path');
var $ = require('gulp-load-plugins')({lazy: true});
var colors = $.util.colors;
var env = $.util.env;
/**
* List the available gulp tasks
*/
gulp.task('help', $.taskListing);
gulp.task('default', ['help']);
/**
* Lint the code, create coverage report, and a visualizer
* @return {Stream}
*/
gulp.task('vet', function() {
log('Analyzing source with JSHint and JSCS');
return gulp
.src(config.js)
.pipe($.if(env.verbose, $.print()))
.pipe($.jshint.reporter('jshint-stylish'))
.pipe($.jscs());
});
/**
* Bump the version
* --type=pre will bump the prerelease version *.*.*-x
* --type=patch or no flag will bump the patch version *.*.x
* --type=minor will bump the minor version *.x.*
* --type=major will bump the major version x.*.*
* --version=1.2.3 will bump to a specific version and ignore other flags
*/
gulp.task('bump', function() {
var msg = 'Bumping versions';
var type = args.type;
var version = args.ver;
var options = {};
if (version) {
options.version = version;
msg += ' to ' + version;
} else {
options.type = type;
msg += ' for a ' + type;
}
log(msg);
return gulp
.src(config.packages)
.pipe($.print())
.pipe($.bump(options))
.pipe(gulp.dest(config.root));
});
/**
* Build everything
*/
gulp.task('build', ['clean'], function(done) {
log('Optimizing the js, css, and html');
var stream = gulp
.src(config.js)
.pipe(getHeader())
.pipe(gulp.dest(config.build))
.pipe($.uglify())
.pipe(getHeader())
.pipe($.rename('bard.min.js'))
.pipe(gulp.dest(config.build))
.on('end', success)
.on('error', error);
function error(err) {
log(err);
done(err);
}
function success() {
var msg = {
title: 'gulp build',
message: 'Deployed to the dist folder'
};
log(msg);
notify(msg);
done();
}
});
/**
* Remove all files from the build, temp, and reports folders
* @param {Function} done - callback when complete
*/
gulp.task('clean', function(done) {
var delconfig = [].concat(config.build, config.report);
log('Cleaning: ' + $.util.colors.blue(delconfig));
del(delconfig, done);
});
/**
* 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);
}
/**
* 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);
}
/**
* Format and return the header for files
* @return {String} Formatted file header
*/
function getHeader() {
var pkg = require('./package.json');
var template = ['/**',
' * <%= pkg.name %> - <%= pkg.description %>',
' * @authors <%= pkg.authors %>',
' * @version v<%= pkg.version %>',
' * @link <%= pkg.homepage %>',
' * @license <%= pkg.license %>',
' */',
''
].join('\n');
return $.header(template, {
pkg: pkg
});
}
/**
* 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));
}
}
/**
* Show OS level notification using node-notifier
*/
function notify(options) {
var notifier = require('node-notifier');
var notifyOptions = {
sound: 'Bottle',
contentImage: path.join(__dirname, 'gulp.png'),
icon: path.join(__dirname, 'gulp.png')
};
_.assign(notifyOptions, options);
notifier.notify(notifyOptions);
}