-
Notifications
You must be signed in to change notification settings - Fork 9
/
gulpfile.js
425 lines (379 loc) · 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
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
var $, _, gulp, merge, fs, path, semver, nodegit, CONFIG, FOLDERS, DOMAIN, PATHS, MATCH, SRC;
gulp = require( 'gulp' );
merge = require( 'merge-stream' );
fs = require( 'fs' );
path = require( 'path' );
semver = require( 'semver' );
_ = require( 'underscore' );
$ = require( 'gulp-load-plugins' )( {pattern: '*'} );
if( path.sep !== '/' ) {
var oldJoin = path.join;
path.join = function(){
var ret = oldJoin.apply(this, arguments);
return ret.replace( new RegExp('\\' + path.sep, 'g' ), '/' );
};
}
CONFIG = {
production: ! ! $.util.env.production,
watch: ! ! $.util.env.watch,
bs: ! ! $.util.env.bs,
noprefix: ! ! $.util.env.noprefix
};
// Here are defined relative paths for source files, dest paths and maps
// Change them if you know what you are doing or just stick to folder structure convention
PATHS = {
assets: '/assets',
source: '{self.assets}/source',
sass: '{self.assets}/source/sass',
jsSource: '{self.assets}/source/js',
css: '{self.assets}/css',
jsDest: '{self.assets}/js',
maps: '{self.assets}/source/_maps'
};
MATCH = {
php: '**/*.php',
sass: '**/*.scss',
css: '**/*.css',
js: '**/*.js'
};
SRC = {
sass: [],
js: []
};
/* Confing: Edit project-folders.json to set your folders and domain
========================================================= */
try {
FOLDERS = JSON.parse( fs.readFileSync( './project-folders.json' ) );
} catch ( e ) {
FOLDERS = [ '.' ];
}
Array.prototype.getFiltered = function(type){
if ( type ) {
return this.filter(
function( item ){
return item.name.substr( 0, type.length ) === type;
}
);
} else {
return this;
}
};
Array.prototype.getFlattened = function(prefix) {
if ( typeof prefix == 'undefined' ) {
prefix = '';
}
var paths = [];
this.forEach(
function(item, i){
item.files.forEach(
function(item){
paths.push( prefix + item.f );
}
);
}
);
return paths;
};
var newFolders = [];
_.each(
FOLDERS,
function(item, key){
if ( typeof item == 'string' ) {
item = { folder: item };
} else if ( typeof item == 'object' ) {
if ( typeof item.folder == 'string' ) {
// do nothing, we have a folder defined
} else if ( typeof key == 'string' ) {
// we're on an object based structure
item.folder = key;
} else {
// We don't have a folder defined, we are editing globals here
if ( ! _.isUndefined( item.PATHS ) ) {
PATHS = _.extend( {}, PATHS, item.PATHS );
}
if ( ! _.isUndefined( item.MATCH ) ) {
MATCH = _.extend( {}, MATCH, item.MATCH );
}
if ( ! _.isUndefined( item.SRC ) ) {
SRC = _.extend( {}, SRC, item.SRC );
}
return; // ignore because we have no folder defined
}
}
if ( _.isUndefined( item.PATHS ) ) {
item.PATHS = {};
}
item.PATHS = _.extend( {}, PATHS, item.PATHS );
_.each(
item.PATHS,
function(val, key, self){
var newPath = val.replace(
/\{self\.(.*?)\}/g,
function(match, g1){
return self[g1];
}
);
self[key] = newPath;
}
);
if ( _.isUndefined( item.MATCH ) ) {
item.MATCH = {};
}
item.MATCH = _.extend( {}, MATCH, item.MATCH );
if ( _.isUndefined( item.SRC ) ) {
item.SRC = {};
}
item.SRC = _.extend( {}, SRC, item.SRC );
if ( _.isUndefined( item.concat ) ) {
try {
item.concat = JSON.parse( fs.readFileSync( path.join( item.folder, item.PATHS.source, 'concat.json' ) ) );
} catch ( e ) {
item.concat = [];
}
}
item.concat.forEach(
function(concatDest){
concatDest.files = concatDest.files.map(
function(source){
if ( typeof source == 'string' ) {
source = { f: source };
}
var prefix = '';
if ( source.f.substr( 0, 1 ) == '!' ) {
prefix = '!';
source.f = source.f.substr( 1 );
}
source.f = prefix + path.join( item.folder, item.PATHS.source, source.f );
return source;
}
);
}
);
newFolders.push( item );
}
);
FOLDERS = newFolders;
gulp.task(
'zip',
function() {
var tasks = FOLDERS.map(
function( folderConfig ) {
var folder = folderConfig.folder;
var basename = path.basename( path.resolve( folder ) );
var filename = path.join( folder, basename + '.zip' );
try {
fs.unlinkSync( filename );
} catch ( e ) {
// do nothing
}
return gulp.src(
[
`${folder}/{assets,i18n,includes,src,vendor,views}/**/*`,
`${folder}/*.{php,txt,md}`,
'LICENSE'
],
{
base: path.join( folder, '..' ),
}
)
.pipe( $.vinylZip.dest( filename ) )
.pipe( $.size( {title: folder + ' zip'} ) );
}
);
return merge( tasks );
}
);
function map_destination( folderConfig, dest ) {
var assetsPath = path.join( folderConfig.folder, folderConfig.PATHS.assets );
var destPath = path.join( folderConfig.folder, dest );
var mapsPath = path.join( folderConfig.folder, folderConfig.PATHS.maps );
var destRelative = path.relative( assetsPath,destPath );
var suffix = '';
if ( destRelative.substr( 0,1 ) != '.' && destRelative.substr( 0,1 ) != path.sep ) {
suffix = path.sep + destRelative;
}
return {
dest: destPath,
maps: path.relative( destPath, mapsPath ) + suffix
};
}
gulp.task(
'sass',
function() {
var tasks = FOLDERS.map(
function( folderConfig ) {
var folder = folderConfig.folder;
var PATHS = folderConfig.PATHS;
var MATCH = folderConfig.MATCH;
var SRC = JSON.parse( JSON.stringify( folderConfig.SRC.sass ) );
var baseSRC = path.join( folder, PATHS.sass );
SRC.unshift( path.join( baseSRC, MATCH.sass ) );
var destination = map_destination( folderConfig, PATHS.css );
return gulp.src( SRC, { base: baseSRC } )
.pipe( $.plumber() )
.pipe( $.sourcemaps.init() )
.pipe( $.sass( { precision: 10 } ).on( 'error', $.sass.logError ) )
.pipe( ! CONFIG.noprefix ? $.autoprefixer() : $.util.noop() )
.pipe( ! CONFIG.production ? $.sourcemaps.write( destination.maps ) : $.util.noop() )
.pipe( $.cached( 'sass' ) )
.pipe( gulp.dest( destination.dest ) )
.pipe( $.filter( '**/*.css' ) )
.pipe( $.cleanCss() )
.pipe( $.rename( {suffix: '.min'} ) )
.pipe( ! CONFIG.production ? $.sourcemaps.write( destination.maps ) : $.util.noop() )
.pipe( $.cached( 'sass' ) )
.pipe( gulp.dest( destination.dest ) )
.pipe( $.size( {title: folder + ' css'} ) );
}
);
return merge( tasks );
}
);
gulp.task(
'js',
function() {
var tasks = FOLDERS.map(
function( folderConfig ) {
var folder = folderConfig.folder;
var PATHS = folderConfig.PATHS;
var MATCH = folderConfig.MATCH;
var SRC = JSON.parse( JSON.stringify( folderConfig.SRC.js ) );
var baseSRC = path.join( folder, PATHS.jsSource );
SRC.unshift( path.join( baseSRC, MATCH.js ) );
var destination = map_destination( folderConfig, PATHS.jsDest );
// get concat sources for this particular folder (from the source folder)
var concatFiles = folderConfig.concat.getFiltered( 'js' );
// use the concat sources as ignored paths in the "base" process
var includePaths = concatFiles.getFlattened( '!' );
SRC = _.union( SRC, includePaths );
var folderTasks = [];
// Do everything not included in the concat
var base = gulp.src( SRC, { base: baseSRC } )
.pipe( $.plumber() )
.pipe( $.sourcemaps.init() )
.pipe( ! CONFIG.production ? $.sourcemaps.write( destination.maps ) : $.util.noop() )
.pipe( $.cached( 'js' ) )
.pipe( gulp.dest( destination.dest ) )
.pipe( $.filter( [ '**/*.js', '!**/*.min.js' ] ) )
.pipe( $.uglify() )
.pipe( $.rename( {suffix: '.min'} ) )
.pipe( ! CONFIG.production ? $.sourcemaps.write( destination.maps ) : $.util.noop() )
.pipe( $.cached( 'js' ) )
.pipe( gulp.dest( destination.dest ) )
.pipe( $.size( {title: folder + ' js'} ) );
folderTasks.push( base );
// Process each concat dest as it's individual file
concatFiles.forEach(
function(dest) {
var thisDest;
if ( dest.passthrough ) {
var passThroughDest = destination;
if ( dest.files.length > 1 || dest.files[0].f.indexOf( '*' ) > -1 ) {
passThroughDest = map_destination( folderConfig, path.join( PATHS.jsDest, path.basename( dest.name ) ) );
}
thisDest = gulp.src( [ dest ].getFlattened() )
.pipe( $.plumber() )
.pipe( $.cached( 'js' ) )
.pipe( gulp.dest( passThroughDest.dest ) )
.pipe( $.size( {title: folder + ' passthrough ' + dest.name } ) );
} else {
var thisDestPath = path.join( folderConfig.PATHS.assets, path.dirname( dest.name ) );
var thisDestination = map_destination( folderConfig, thisDestPath );
var keepUnminified = _.pluck( _.filter( dest.files, 'keepUnminified' ), 'f' );
keepUnminified = keepUnminified.length ? $.filter( keepUnminified, { restore : true } ) : null;
thisDest = gulp.src( [ dest ].getFlattened() )
.pipe( $.plumber() )
.pipe( $.sourcemaps.init() )
.pipe( keepUnminified ? keepUnminified : $.util.noop() )
.pipe( keepUnminified && ! CONFIG.production ? $.sourcemaps.write( thisDestination.maps ) : $.util.noop() )
.pipe( keepUnminified ? $.cached( 'js' ) : $.util.noop() )
.pipe( keepUnminified ? gulp.dest( thisDestination.dest ) : $.util.noop() )
.pipe( keepUnminified ? keepUnminified.restore : $.util.noop() )
.pipe( $.filter( [ '**/*.js' ] ) )
.pipe( $.concat( path.basename( dest.name ) ) )
.pipe( ! dest.minifiedOnly && ! CONFIG.production ? $.sourcemaps.write( thisDestination.maps ) : $.util.noop() )
.pipe( ! dest.minifiedOnly ? $.cached( 'js' ) : $.util.noop() )
.pipe( ! dest.minifiedOnly ? gulp.dest( thisDestination.dest ) : $.util.noop() )
.pipe( $.filter( [ '**/*.js' ] ) )
.pipe( $.uglify() )
.pipe( $.rename( {suffix: '.min'} ) )
.pipe( ! CONFIG.production ? $.sourcemaps.write( thisDestination.maps ) : $.util.noop() )
.pipe( $.cached( 'js' ) )
.pipe( gulp.dest( thisDestination.dest ) )
.pipe( $.size( {title: folder + ' concat ' + dest.name } ) );
}
folderTasks.push( thisDest );
}
);
return merge( folderTasks );
}
);
return merge( tasks );
}
);
gulp.task(
'browser-sync',
function(done) {
var files = [];
FOLDERS.map(
function (folderConfig) {
files.push( path.join( folderConfig.folder, folderConfig.PATHS.css, folderConfig.MATCH.css ) );
files.push( path.join( folderConfig.folder, folderConfig.PATHS.jsDest, folderConfig.MATCH.js ) );
files.push( path.join( folderConfig.folder, folderConfig.MATCH.php ) );
}
);
try {
DOMAIN = JSON.parse( fs.readFileSync( './dev-domain.json' ) );
} catch ( e ) {
return done( e );
}
$.browserSync.init( files, {proxy: DOMAIN} );
return done();
}
);
var doWatch = function (done) {
FOLDERS.map(
function (folderConfig) {
gulp.watch( path.join( folderConfig.folder, folderConfig.PATHS.sass, folderConfig.MATCH.sass ), gulp.parallel( 'sass' ) );
gulp.watch( path.join( folderConfig.folder, folderConfig.PATHS.jsSource, folderConfig.MATCH.js ), gulp.parallel( 'js' ) );
}
);
};
var watch_task;
if ( CONFIG.bs ) {
watch_task = gulp.series( 'browser-sync', doWatch );
} else {
watch_task = doWatch;
}
gulp.task( 'watch', watch_task );
gulp.task( 'build', gulp.parallel( 'sass', 'js' ) );
var default_task;
if ( CONFIG.watch ) {
default_task = gulp.series( 'build', 'watch' );
} else {
default_task = gulp.series( 'build' );
}
async function getDiffFiles() {
nodegit = require( 'nodegit' );
var repository = await nodegit.Repository.open( '.' );
var currCommit = await repository.getHeadCommit();
var mastCommit = await repository.getMasterCommit();
var currTree = await currCommit.getTree();
var mastTree = await mastCommit.getTree();
var diff = await currTree.diff( mastTree );
diff = await diff.patches();
diff = diff.map(
function(diffFile) {
if ( diffFile.isDeleted() ) {
return false;
}
return '.' + path.sep + diffFile.newFile().path();
}
).filter(
function(cont){
return cont ? true : false;
}
);
return diff;
}
gulp.task( 'default', default_task );