-
Notifications
You must be signed in to change notification settings - Fork 10
/
gulpfile.js
189 lines (153 loc) · 5.43 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
/*jshint node:true */
'use strict';
var config = require('./gulpconfig.js');
var gulp = require('gulp');
var nopt = require('nopt'); // handle CLI arguments
var fs = require('fs');
var plugins = require('gulp-load-plugins')();
// helpers
/**
* Get the version number
* @param {Array.<string>} packageFiles
* @throws {Error}
* @return {string}
*/
var getVersionNumberFromFile = function(packageFiles) {
if (packageFiles.length === 0) {
throw new Error(
'Where are your package files (package.json, bower.json)?'
);
}
var packageFile = packageFiles[0];
var fileContent = fs.readFileSync(
__dirname + '/' + packageFile, { encoding: 'utf-8' }
);
var pkg = JSON.parse(fileContent);
if (!pkg.hasOwnProperty('version')) {
throw new Error(
'Your package file (' + packageFile +
') does not contain any version number!'
);
}
return pkg.version;
};
// parse CLI arguments with nopt
nopt.invalidHandler = function(key) {
var msg = 'Invalid "' + key + '" parameter!';
throw new Error(msg);
};
nopt.typeDefs.release = {
type: 'release',
validate: function(data, key, val) {
val = (val + '').toLowerCase();
// https://github.com/mojombo/semver/issues/110#issuecomment-19433829
var semver = new RegExp(
'^' +
'(0|[1-9]\\d*)' + // major
'\\.(0|[1-9]\\d*)' + // minor
'\\.(0|[1-9]\\d*)' + // patch
'(?:-' + // start prerelease
'(' + // capture
'(?:' + // first identifier
'0|' + // 0, or
'[1-9]\\d*|' + // numeric identifier, or
'\\d*[a-z-][a-z0-9-]*' +
')' + // end first identifier
'(?:\\.' + // dot-separated
'(?:0|[1-9]\\d*|\\d*[a-z-][a-z0-9-]*)' +
')*' + // zero or more of those
')' + // end prerelease capture
')?' + // prerelease is optional
'(?:' + // build tag (non-capturing)
'\\+[0-9a-z-]+(?:\\.[0-9a-z-]+)*' +
')?' + // build tag is optional
'$'
);
// major: 1.0.0
// minor: 0.1.0
// patch: 0.0.2
var shortHands = [ 'major', 'minor', 'patch' ];
if (shortHands.indexOf(val) === -1 && !semver.test(val)) {
return false;
}
data[key] = val;
}
};
var argv = nopt({ release: 'release' }, { r: '--release' }, process.argv, 1);
// set the default values
argv.release = argv.release || 'patch';
// Task for bumping the version number
// Usage: `gulp bump [--release <version>]`
gulp.task('bump', [ 'dist', 'bump-version-number' ], function() {
return gulp.start('bump-commit-and-tag');
});
gulp.task('bump-version-number', function() {
var options = {};
var key = ([ 'major', 'minor', 'patch' ].indexOf(argv.release) !== -1) ?
'type' : 'version';
options[key] = argv.release;
var packageFiles = config.plugins.bump.packageFiles;
return gulp.src(packageFiles).
pipe(plugins.bump(options)).
pipe(gulp.dest('./'));
});
gulp.task('bump-commit-and-tag', [ 'bump-commit' ], function() {
return gulp.start('bump-tag');
});
gulp.task('bump-commit', function() {
var packageFiles = config.plugins.bump.packageFiles;
var version = 'v' + getVersionNumberFromFile(packageFiles);
var message = 'Release ' + version;
var filesToCommit = [].concat(packageFiles, config.path.dist + '/**');
return gulp.src(filesToCommit).pipe(plugins.git.commit(message));
});
gulp.task('bump-tag', function(cb) {
var packageFiles = config.plugins.bump.packageFiles;
var version = 'v' + getVersionNumberFromFile(packageFiles);
var message = 'Release ' + version;
plugins.git.tag(version, message, null, cb);
});
// Task for testing and linting
// Usage: `gulp test` or if you want to run the testing and the linting tasks
// separately use: `gulp qunit` and `gulp lint`
gulp.task('test', [ 'qunit', 'lint' ]);
gulp.task('qunit', function() {
return gulp.src(config.path.test + '/index.html').pipe(plugins.qunit());
});
gulp.task('lint', [ 'jshint', 'jscs', 'jsonlint' ]);
gulp.task('jshint', function() {
var files = config.filesForAnalyze.js;
if (files.length === 0) { return; }
return gulp.src(files).
pipe(plugins.jshint()).
pipe(plugins.jshint.reporter());
});
gulp.task('jscs', function() {
var files = config.filesForAnalyze.js;
if (files.length === 0) { return; }
return gulp.src(files).pipe(plugins.jscs());
});
gulp.task('jsonlint', function() {
var files = config.filesForAnalyze.json;
if (files.length === 0) { return; }
return gulp.src(files).
pipe(plugins.jsonlint()).
pipe(plugins.jsonlint.reporter());
});
// Task for distributing
// Usage: `gulp dist` or `gulp`
gulp.task('default', [ 'dist' ]);
gulp.task('dist', [ 'dist-concat' ], function() {
return gulp.start('dist-minify');
});
gulp.task('dist-concat', function() {
return gulp.src(config.path.src + '/**/*.js').
pipe(plugins.concat('jquery.keep-ratio.js')).
pipe(gulp.dest(config.path.dist));
});
gulp.task('dist-minify', function() {
return gulp.src(config.path.dist + '/jquery.keep-ratio.js').
pipe(plugins.concat('jquery.keep-ratio.min.js')).
pipe(plugins.uglify()).
pipe(gulp.dest(config.path.dist));
});