-
Notifications
You must be signed in to change notification settings - Fork 5
/
gulpfile.js
438 lines (391 loc) · 9.55 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/**
* Import modules
*/
const packageJSON = require('./package.json');
const gulp = require('gulp');
const rename = require('gulp-rename');
const uglify = require('gulp-uglify');
const iife = require('gulp-iife');
const concat = require('gulp-concat');
const sass = require('gulp-sass');
const autoprefixer = require('gulp-autoprefixer');
const plumber = require('gulp-plumber');
const notify = require('gulp-notify');
const browserSync = require('browser-sync').create();
const argv = require('yargs').argv;
const gulpif = require('gulp-if');
const cleanCSS = require('gulp-clean-css');
const sourcemaps = require('gulp-sourcemaps');
const wpPot = require('gulp-wp-pot');
const phpcs = require('gulp-phpcs');
const bump = require('gulp-bump');
const jshint = require('gulp-jshint');
const jshintStylish = require('jshint-stylish');
/**
* Local variables
*/
const prefix = 'woogosend';
const project = 'WooGoSend';
const assets = [
{
type: 'scripts',
target: 'backend',
sources: [
'helpers.js',
'console-listener.js',
'map-picker.js',
'backend.js',
],
targetDir: 'assets/js/',
sourcesDir: 'assets/src/js/',
isPrefixed: true,
isIife: true,
},
{
type: 'scripts',
target: 'frontend',
sources: [
'frontend.js',
],
targetDir: 'assets/js/',
sourcesDir: 'assets/src/js/',
isPrefixed: true,
isIife: true,
},
{
type: 'styles',
target: 'backend',
sources: [
'backend.scss',
],
targetDir: 'assets/css/',
sourcesDir: 'assets/src/scss/',
isPrefixed: true,
},
{
type: 'php',
target: 'php',
sources: [
'*.php',
'**/*.php',
'!vendor/',
'!vendor/**',
'!dist/',
'!dist/**',
'!node_modules/',
'!node_modules/**',
'!index.php',
'!**/index.php',
],
},
];
/**
* Custom error handler
*/
const errorHandler = function () {
return plumber(function (err) {
notify.onError({
title: 'Gulp error in ' + err.plugin,
message: err.toString()
})(err);
});
};
/**
* Script taks handler
*/
const scriptsHandler = function (asset, isMinify) {
const srcParam = asset.sources.map(function (sources) {
const sourcesDir = asset.sourcesDir || '';
return sourcesDir + sources;
});
return gulp.src(srcParam)
.pipe(errorHandler())
.pipe(concat(asset.target + '.js'))
.pipe(gulpif(asset.isIife, iife({
useStrict: false,
trimCode: true,
prependSemicolon: false,
bindThis: false,
params: ['$'],
args: ['jQuery']
})))
.pipe(gulpif(asset.isPrefixed, rename({
prefix: prefix + '-',
})))
.pipe(gulp.dest(asset.targetDir))
.pipe(gulpif(isMinify, rename({
suffix: '.min',
})))
.pipe(gulpif(isMinify, sourcemaps.init()))
.pipe(gulpif(isMinify, uglify()))
.pipe(gulpif(isMinify, sourcemaps.write()))
.pipe(gulpif(isMinify, gulp.dest(asset.targetDir)));
}
/**
* Style taks handler
*/
const stylesHandler = function (asset, isMinify) {
const srcParam = asset.sources.map(function (sourcesFile) {
const sourcesDir = asset.sourcesDir || '';
return sourcesDir + sourcesFile;
});
return gulp.src(srcParam)
.pipe(errorHandler())
.pipe(gulpif(isMinify, sourcemaps.init()))
.pipe(sass().on('error', sass.logError))
.pipe(autoprefixer(
'last 2 version',
'> 1%',
'safari 5',
'ie 8',
'ie 9',
'opera 12.1',
'ios 6',
'android 4'))
.pipe(gulpif(asset.isPrefixed, rename({
prefix: prefix + '-',
})))
.pipe(gulp.dest(asset.targetDir))
.pipe(gulpif(isMinify, rename({
suffix: '.min',
})))
.pipe(gulpif(isMinify, cleanCSS({
compatibility: 'ie8',
})))
.pipe(gulpif(isMinify, sourcemaps.write()))
.pipe(gulpif(isMinify, gulp.dest(asset.targetDir)))
.pipe(gulpif(!isMinify, browserSync.stream()));
}
/**
* PHPCS taks handler
*/
const phpcsHandler = function (asset) {
const srcParam = asset.sources.map(function (sourcesFile) {
const sourcesDir = asset.sourcesDir || '';
return sourcesDir + sourcesFile;
});
const config = Object.assign({}, asset.config, {
bin: 'vendor/bin/phpcs',
standard: '.phpcs.xml',
warningSeverity: 0,
});
return gulp.src(srcParam)
.pipe(errorHandler())
.pipe(phpcs(config))
.pipe(phpcs.reporter('log'));
}
/**
* I18N taks handler
*/
const i18nHandler = function (asset) {
const srcParam = asset.sources.map(function (sourcesFile) {
const sourcesDir = asset.sourcesDir || '';
return sourcesDir + sourcesFile;
});
const config = Object.assign({}, asset.config, {
domain: prefix,
package: project,
});
return gulp.src(srcParam)
.pipe(wpPot(config))
.pipe(gulp.dest('languages/' + config.domain + '.pot'));
}
/**
* Build tasks list
*/
const tasksListBuild = [];
assets.forEach(function (asset) {
/**
* Minify Scripts Task
*/
if (asset.type === 'scripts') {
const taskName = asset.target + '-scripts-minify';
gulp.task(taskName, function () {
return scriptsHandler(asset, true);
});
tasksListBuild.push(taskName);
}
/**
* Minify Styles Task
*/
if (asset.type === 'styles') {
const taskName = asset.target + '-styles-minify';
gulp.task(taskName, function () {
return stylesHandler(asset, true);
});
tasksListBuild.push(taskName);
}
/**
* PHPCBF Task
*/
if (asset.type === 'php') {
const taskName = asset.target + '-i18n';
gulp.task(taskName, function () {
return i18nHandler(asset);
});
tasksListBuild.push(taskName);
}
});
/**
* Build task
*/
gulp.task('build', tasksListBuild);
/**
* Default tasks list
*/
const tasksListDefault = [];
assets.forEach(function (asset) {
/**
* Scripts Task
*/
if (asset.type === 'scripts') {
const taskName = asset.target + '-scripts';
gulp.task(taskName, function () {
return scriptsHandler(asset);
});
tasksListDefault.push(taskName);
}
/**
* Styles Task
*/
if (asset.type === 'styles') {
const taskName = asset.target + '-styles';
gulp.task(taskName, function () {
return stylesHandler(asset, false);
});
tasksListDefault.push(taskName);
}
/**
* PHPCS Task
*/
if (asset.type === 'php') {
const taskName = asset.target + '-phpcs';
gulp.task(taskName, function () {
return phpcsHandler(asset);
});
tasksListDefault.push(taskName);
}
});
/**
* Default task
*/
gulp.task('default', tasksListDefault, function () {
if (argv.hasOwnProperty('proxy')) {
browserSync.init({
proxy: argv.proxy
});
}
assets.forEach(function (asset) {
/**
* Watch styles sources files
*/
if (asset.type === 'styles') {
const watchStylesSrc = asset.sources.map(function (sourcesFile) {
const sourcesDir = asset.sourcesDir || '';
return sourcesDir + sourcesFile;
});
gulp.watch(watchStylesSrc, [asset.target + '-styles']);
}
/**
* Watch scripts sources files
*/
if (asset.type === 'scripts') {
const watchScriptsSrc = asset.sources.map(function (sourcesFile) {
const sourcesDir = asset.sourcesDir || '';
return sourcesDir + sourcesFile;
});
gulp.watch(watchScriptsSrc, [asset.target + '-scripts']).on('change', function () {
if (argv.hasOwnProperty('proxy')) {
browserSync.reload();
}
});
}
/**
* Watch php sources files
*/
if (asset.type === 'php') {
const watchSrc = asset.sources.map(function (sourcesFile) {
const sourcesDir = asset.sourcesDir || '';
return sourcesDir + sourcesFile;
});
gulp.watch(watchSrc, [asset.target + '-phpcs']).on('change', function () {
if (argv.hasOwnProperty('proxy')) {
browserSync.reload();
}
});
}
});
});
gulp.task('lintScripts', function () {
return gulp.src('./assets/src/js/*.js')
.pipe(jshint())
.pipe(jshint.reporter(jshintStylish));
})
gulp.task('bump', function () {
var sources = [
{
file: ['./README.txt'],
config: {
key: "Stable tag",
type: argv.hasOwnProperty('type') ? argv.type : 'patch',
},
},
{
file: ['./woogosend.php'],
config: {
key: "Version",
type: argv.hasOwnProperty('type') ? argv.type : 'patch',
},
},
{
file: ['./includes/constants.php'],
config: {
key: "WOOGOSEND_VERSION",
regex: new RegExp('([<|\'|"]?(WOOGOSEND_VERSION)[>|\'|"]?[ ]*[:=,]?[ ]*[\'|"]?[a-z]?)(\\d+.\\d+.\\d+)(-[0-9A-Za-z.-]+)?(\\+[0-9A-Za-z\\.-]+)?([\'|"|<]?)', 'i'),
type: argv.hasOwnProperty('type') ? argv.type : 'patch',
},
dest: './includes/',
},
{
file: ['./package.json'],
config: {
key: "version",
type: argv.hasOwnProperty('type') ? argv.type : 'patch',
},
},
];
sources.forEach(function (source) {
const dest = source.dest || './';
gulp.src(source.file)
.pipe(bump(source.config))
.pipe(gulp.dest(dest));
});
});
// Export task
gulp.task('dist', ['build'], function () {
gulp.src([
'./**',
'!tests/',
'!bin/',
'!vendor/',
'!dist/',
'!node_modules/',
'!assets/src/',
'!tests/**',
'!bin/**',
'!vendor/**',
'!dist/**',
'!node_modules/**',
'!assets/src/**',
'!gulpfile.js',
'!package-lock.json',
'!package.json',
'!composer.lock',
'!composer.json',
'!yarn.lock',
'!phpcs.xml',
'!phpunit.xml'
])
.pipe(gulp.dest('./dist/trunk'))
.pipe(gulp.dest('./dist/tags/' + packageJSON.version));
});