-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
318 lines (286 loc) · 8.12 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
// Require all dev dependencies.
var gulp = require('gulp'),
minify = require('gulp-minify'),
watch = require('gulp-watch'),
cleanCSS = require('gulp-clean-css'),
rename = require('gulp-rename'),
postcss = require('gulp-postcss'),
sass = require('gulp-sass'),
zip = require('gulp-zip'),
prompt = require('gulp-prompt'),
git = require('gulp-git'),
request = require('request'),
shell = require('shelljs'),
semver = require('semver'), // Versioning standard - http://semver.org/
fs = require('fs'),
asynclib = require('async'),
rmdir = require('rmdir'),
autoprefixer = require('autoprefixer'),
browserSync = require('browser-sync').create(),
sourcemaps = require('gulp-sourcemaps'),
spawn_shell = require('spawn-shell'),
imagemin = require('gulp-imagemin');
var exec = require('child_process').exec;
// me.js contains vars to your specific setup
var me = require('./gulpconf.js');
var environment = Object.assign({}, process.env, { PATH: process.env.PATH + ':/usr/local/bin' });
var WEBSITE = me.WEBSITE;
var CONTENT_TYPE = me.CONTENT_TYPE;
var BASE_NAME = __dirname.match(/([^\/]*)\/*$/)[1];
const BASE_FILE = 'style.css';
const TAG_REGEX = /\*\s?version:\s?(?:(\d+)\.)?(?:(\d+)\.)?(\*|\d+)/i;
// JS source, destination, and excludes.
var JS_EXCLD = '!assets/js/*.min.js',
JS_SRC = 'assets/js/*.js',
JS_DEST = 'assets/js/';
// CSS and SASS src, dest, and exclude.
var CSS_SRC = 'assets/css/*.css',
CSS_DEST = 'assets/css/',
CSS_EXCLD = '!assets/css/*.min.css',
SASS_WATCH = 'assets/scss/*.scss';
if( 'plugin' === CONTENT_TYPE){
SASS_SRC = 'assets/scss/*.scss';
}else{
SASS_SRC = ['assets/scss/*.scss', '!assets/scss/style.scss' ];
}
// Image src and dest.
var IMG_SRC = 'assets/images/*',
IMG_DEST = 'assets/images';
// Zip src and options.
var ZIP_SRC_ARR = [
'./**',
'!**/composer.*',
'!**/gulpfile.js',
'!**/gulpconf.js',
'!**/package.json',
'!**/README.md',
'!**/phpcs.xml',
'!**/phpcs.ruleset.xml',
'!**/phpdoc.dist.xml',
'!**/phpunit.xml.dist',
'!**/{node_modules,node_modules/**}',
'!**/{bin,bin/**}',
'!**/{dist,dist/**}',
'!**/{vendor,vendor/**}',
'!**/{docs,docs/**}',
'!**/{tests,tests/**}'
];
var ZIP_OPTS = { base: '..' };
// PHP Source.
var PHP_SRC = '**/*.php';
/*******************************************************************************
* Gulp Tasks
******************************************************************************/
/**
* Default gulp task. Initializes browserSync proxy server and watches src files
* for any changes.
*
* CMD: gulp
*/
gulp.task('default', function() {
browserSync.init({
proxy: WEBSITE
});
gulp.watch( SASS_SRC, ['build-sass']);
gulp.watch( JS_SRC , ['js-watch']);
gulp.watch( PHP_SRC, function(){
browserSync.reload();
});
});
/**
* JS Watch task. This is a dependency task for the default gulp task that
* builds the js files and reloads the browser in the correct order
*
* CMD: None. Not meant to be run as standalone command.
*/
gulp.task('js-watch', ['build-js'], function(){
browserSync.reload();
});
/**
* Compiles SCSS into regular CSS.
*
* CMD: gulp build-sass
*/
gulp.task('build-sass', function() {
gulp.src( SASS_SRC )
.pipe(sourcemaps.init())
.pipe(sass().on('error', sass.logError))
.pipe(postcss([
autoprefixer({browsers: ['> 5% in US']})
]))
.pipe(cleanCSS({compatibility: 'ie8'}))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(CSS_DEST));
gulp.src( 'assets/scss/style.scss' )
.pipe(sourcemaps.init())
.pipe(sass().on('error', sass.logError))
.pipe(postcss([
autoprefixer({browsers: ['> 5% in US']})
]))
.pipe(cleanCSS({compatibility: 'ie8'}))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('.'))
.pipe(browserSync.stream());
});
/**
* Minifies JS files.
*
* CMD: gulp build-js
*/
gulp.task('build-js', function(){
gulp.src( [ JS_SRC, JS_EXCLD ] )
.pipe(minify({
ext:{
src:'.js',
min:'.min.js'
},
noSource: true
}))
.pipe(gulp.dest( JS_DEST ));
});
gulp.task('build-img', function(){
gulp.src('assets/images/*')
.pipe(imagemin())
.pipe(gulp.dest('assets/images'));
});
/**
* Executes all of the build tasks in the correct sequence.
*
* CMD: gulp build
*/
gulp.task('build', ['build-sass','build-js', 'build-img']);
/**
* Creates a zip file of the current project without any of the config and dev
* files and saves it under the 'dist' folder.
*
* CMD: gulp zip
*/
gulp.task('zip', function(){
return gulp.src( ZIP_SRC_ARR, ZIP_OPTS )
.pipe( zip( BASE_NAME + '.zip' ) )
.pipe( gulp.dest('dist') );
});
/**
* Initializes dev dependencies.
*/
gulp.task('init',['wp-enforcer'], function(){
return request('https://gist.githubusercontent.com/sfgarza/32258b7332a715de4e3948892ba415d3/raw/8a499cfe32749f4cabe2f6fe0d95653ce98e14e2/pre-commit-gulp.bash').pipe(fs.createWriteStream('.git/hooks/pre-commit'));
});
/**
* Runs composer install.
*/
gulp.task('composer', function(cb) {
//Install composer packages
return shell_exec('composer install', cb );
});
/**
* Runs composer update
*/
gulp.task('composer-update', function(cb) {
//Install composer packages
return shell_exec('composer update', cb );
});
/**
* Installs wp-enforcer
*/
gulp.task('wp-enforcer', ['composer'], function(cb){
return shell_exec('./vendor/bin/wp-enforcer', cb );
});
gulp.task('clean', function(){
rmdir('./dist');
});
gulp.task('tag', function(){
get_current_version();
get_current_branch();
console.log(('Current version: ' + current_version).green );
gulp.src( BASE_FILE )
.pipe(prompt.prompt({
type: 'list',
name: 'bump',
message: 'What kind of release would you like to make?',
choices: ['patch', 'minor', 'major']
}, function(res){
asynclib.waterfall([
function(callback){
callback(null, res.bump);
},
git_bump,
git_push,
git_tag
//git_push
], function (err, result) {
if( null !== err ){
console.log('ERROR: %j', err);
}
});
}));
})
/*******************************************************************************
* Functions
******************************************************************************/
function get_current_version(){
head = shell.head( {'-n':30}, BASE_FILE );
found = head.match( TAG_REGEX )
console.log('%j', head);
current_version = found[1] + '.' + found[2] + '.' + found[3];
}
function get_current_branch(){
git.revParse({args:'--abbrev-ref HEAD'}, function (err, hash) {
if (err){
console.error( (err.message).red );
}
else{
current_branch = hash;
}
});
}
function git_bump(bump,callback){
new_version = semver.inc( current_version, bump )
shell.sed( '-i', TAG_REGEX, '* Version: ' + new_version, BASE_FILE );
console.log(('New version: ' + new_version).green );
gulp.src( '.' )
.pipe(git.add({args: '--all'}))
.pipe(git.commit('Bumping version number'));
return callback(null, current_branch);
}
function git_tag(callback){
git.tag(new_version, 'Release' + new_version, {quiet:false}, function (err) {
if (err){
console.error( (err.message).red );
console.error( 'Reverting changes...'.yellow );
shell.sed( '-i', TAG_REGEX, '* Version: ' + current_version, BASE_FILE );
return callback(err)
}
else{
return callback(null, '--tags')
}
});
}
function git_push( branch, callback ){
console.log(branch);
git.push('origin', branch, function (err) {
if (err){
console.error( (err.message).red );
return callback(err);
}
else{
return callback(null);
}
});
}
/**
* Execute Shell script within node.
*
* Not currently being used.
* @param {String} command : Command to execute.
* @param {Function} callback : Callback function.
* @return {Function} : Callback function.
*/
function shell_exec( command, callback ){
// Execute bash script.
// command = path.join( __dirname , '/scripts/gulpconf.sh');
shell = spawn_shell(command, { shell: '/bin/bash', env: environment });
shell.on('exit', function(data){
return callback();
});
}