-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gulpfile.js
412 lines (383 loc) · 15.4 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
/* -----------------------------------------------------------------------------
* Sphax Patch - Setup
* -----------------------------------------------------------------------------
* Edit the below section to customise the patch
* -------------------------------------------------------------------------- */
// Set patch name - will be used to name .zip files.
// Alternatively, when calling 'gulp makeZips' on the command line, pass in the
// argument '--patchname MyPatchName' to override
var patchName = 'GregTech';
// Initial size of source images - set this to the starting size of the patch
// E.g. if resizing a 128x patch, set to 128
var initialSize = 128;
// Set how many times the original patch should be downsized (inclusive)
// E.g. processing a 512x patch 5 times would produce: 512, 256, 128, 64, 32
var resizeLevels = 3;
// Prevent these files from being included in generated size packs:
var ignoreTheseFiles = [
// Design files
'**/*.{ai,psb,psd}',
// Win/OSX system files
'**/*.{DS_Store,db}',
];
// Resize these files:
var resizeables = [
'**/*.png',
// By default, don't resize GUIs (i.e. anything inside a gui/ or guis/ folder)
'!**/{gui,guis,font,fonts}/**/*.png',
// Add similar entries to the below to disable resizing for specific files, e.g.:
// '!**/blocks/someBlock.png',
// '!**/blocks/someOtherBlock.png',
// '!**/items/someItem.png',
// etc...
];
// Apply threshold filter to (remove transparent pixels from) these files:
var thresholdables = [
'**/*.png',
// Add similar entries to the below to disable thresholding for specific files, e.g.:
// '!**/blocks/someBlock.png',
// '!**/blocks/someOtherBlock.png',
// '!**/items/someItem.png',
// etc...
'!**/icons/**/*.png',
'!**/dustImpure_OVERLAY.png',
'!**/dust_impure_overlay.png',
'!**/dustPure_OVERLAY.png',
'!**/dust_pure_overlay.png',
'!**/ingotHot_OVERLAY.png',
'!**/ingot_hot_overlay.png',
'!**/fluids/*.png',
'!**/1.7.10_2/**/gt.metaitem.01/705.png', // Mainframes
'!**/1.7.10_2/**/gt.metaitem.01/706.png',
'!**/1.7.10_2/**/gt.metaitem.03/88.png',
'!**/1.7.10_2/**/gt.metaitem.03/91.png',
'!**/1.7.10_2/**/gt.metaitem.03/95.png',
];
// Force these files to grayscale:
var grayscaleables = [
'**/blocks/materialicons/**/*.png',
'**/blocks/material_sets/**/*.png',
'**/BUTCHERYKNIFE.png',
'**/CROWBAR.png',
'**/GRAFTER.png',
'**/JACKHAMMER.png',
'**/KNIFE.png',
'**/MORTAR.png',
'**/ROLLING_PIN.png',
'**/SCOOP.png',
'**/SICKLE.PNG',
'**/TURBINE*.png',
'**/WIRE_CUTTER.png',
'**/WRENCH.png',
'**/items/materialicons/**/*.png',
'**/items/material_sets/**/*.png',
'**/items/tools/*.png',
// Add similar entries to the below to disable grayscaling for specific files, e.g.:
// '!**/blocks/someBlock.png',
// '!**/blocks/someOtherBlock.png',
// '!**/items/someItem.png',
// etc...
'!**/*_OVERLAY.png',
'!**/*_overlay.png',
'!**/*_base.png',
'!**/handle*.png',
'!**/items/tools/power*.png',
];
// Optimise these files using imagemin:
var compressables = [
'**/*.png',
// Add similar entries to the below to disable lossless image compression for specific files, e.g.:
// '!**/blocks/someBlock.png',
// '!**/blocks/someOtherBlock.png',
// '!**/items/someItem.png',
// etc...
];
// Paths - set these to whatever, or simply leave as default
var paths = {
// Source images/designs folder - source designs should be placed here
src: 'source-designs/',
// Destination for generated size packs
dest: 'size-packs/'
};
/* -----------------------------------------------------------------------------
* Core logic below, only edit the below if you're brave/bored/interested
* -------------------------------------------------------------------------- */
// custom plugin settings
// -----------------------------------------------------------------------------
var settings = {
imagemin: {
// Default is 2 (8 trials)
optimizationLevel: 3,
keepBitDepth: false,
keepColorType: true,
keepPalette: false,
keepIDAT: false,
}
},
// watched filename patterns
// -----------------------------------------------------------------------------
watchedPatterns = {
img: paths.src + '**/*.{png,mcmeta,txt}'
},
// Suffix to add onto created size pack directories
// -----------------------------------------------------------------------------
suff = 'x',
// Pre-populate a list of sizes
// -----------------------------------------------------------------------------
sizes = (function () {
var a = [];
for (var l = 0; l < resizeLevels; l++) {
var size = initialSize / Math.pow(2, l);
a.push(size);
} // /for ... resizeLevels
return a;
})();
/* -----------------------------------------------------------------------------
* GULP PLUGINS
* -------------------------------------------------------------------------- */
// Gulp + plugins
var gulp = require('gulp'),
$ = require('gulp-load-plugins')({
pattern: '*',
camelize: true
}),
// Node core modules
path = require('path'),
fs = require('fs'),
// Get command line args
args = $.minimist(process.argv.slice(2));
/* -----------------------------------------------------------------------------
* GLOBAL FUNCTIONS
* -------------------------------------------------------------------------- */
// getDirs - returns an array of first-level directory names in a baseDir
function getDirs(baseDir, callback) {
// Store dirs in new empty array
var dirs = [];
// Read the contents of baseDir
fs.readdir(baseDir, function (err, files) {
function checkDir(count, file, filePath) {
// ignore hidden/system files
if (file[0] !== '.') {
// Get the file's stats
fs.stat(filePath, function(err, stats) {
// If this file is a dir, push to dirs
if (stats.isDirectory()) {
dirs.push(file);
} // /if (stats.isDirectory())
// Execute callback when finished looping through files
if (count === (files.length - 1)) {
return callback(dirs);
} // /if (count === 0)
}); // /fs.stat(filePath...
} // /ignore hidden/system files
} // /function checkDir
// Exit if errors
if (err) {
console.error(err);
return cb([]);
}
// Otherwise...
else {
// For every file in files
for (var f = 0; f < files.length; f++) {
// Get the filename and filepath
var file = files[f],
filePath = path.join(baseDir, file);
// Check whether the file as a dir
checkDir(f, file, filePath);
} // / For every file in files
}
}); // /fs.readdir(baseDir...
} // /function getDirs
function resizeStream(dirname, size) {
var pctScale = size / initialSize * 100 + '%', // 50%
packName = size + suff, // 256x
customDirname = path.join(dirname, packName), // 1.7.10/256x
// Set up PNG-only file filter:
filterPNG = $.filter('**/*.png', { restore: true });
// Optimise these files using OptiPNG:
filterCompressables = $.filter(compressables, { restore: true });
// Resize these files:
filterResizeables = $.filter(resizeables, { restore: true });
// Grayscale these files:
filterGrayscaleables = $.filter(grayscaleables, { restore: true});
// Apply threshold filter to (remove transparent pixels from) these files:
filterThresholdables = $.filter(thresholdables, { restore: true });
return gulp.src(path.join(paths.src, dirname, '**'),
{ base: path.join(paths.src, dirname) }
) // source everything
// Only pass through files newer than dest files
.pipe($.newer(path.join(paths.dest, dirname, packName)))
// Filter out crap
.pipe($.ignore(ignoreTheseFiles))
// Do the following steps to PNGs only( i.e. no .txt, .mcmeta files)
.pipe(filterPNG)
// Do the following to ONLY resizeables
.pipe(filterResizeables)
// Use gulp-gm to resize images
.pipe($.gm(function (imageFile) {
return imageFile
// Ensure 8-bit RGB
.bitdepth(8)
// Bilinear
// .filter('Triangle')
// 'Bicubic' (Catmull-Rom)
.filter('Catrom')
// Resize to % scale
.resize(pctScale, '%');
}))
.pipe(filterResizeables.restore)
// Use gulp-gm to apply threshold to (remove partial transparency from) images
// BUT - Apply only to images we want to remove transparency from
.pipe(filterThresholdables)
.pipe($.gm(function (imageFile) {
return imageFile
// Ensure no transparent edges on all PNGs
.operator('Opacity', 'Threshold', 50, '%');
}))
.pipe(filterThresholdables.restore)
.pipe(filterGrayscaleables)
.pipe($.gm(function (imageFile) {
return imageFile
.colorspace('GRAY');
}))
.pipe(filterGrayscaleables.restore)
// pass images registered in compressables through imagemin
.pipe(filterCompressables)
// Measure file-by-file byte difference
.pipe($.bytediff.start())
.pipe($.imageminOptipng(settings.imagemin)())
.pipe($.bytediff.stop())
.pipe(filterCompressables.restore)
// Restore non-PNG files to stream
.pipe(filterPNG.restore)
.pipe($.rename(function (thisPath) {
thisPath.dirname = path.join(customDirname, thisPath.dirname);
}))
.pipe(gulp.dest(paths.dest))
// Log when basic pack has been written
.on('end', function () {
$.util.log($.util.colors.magenta('Finished creating pack:'), $.util.colors.cyan(customDirname));
});
} // /function resizeStream
function zipStream(dirname, size) {
// Intended zip name:
// [dirname] [size] Sphax Patch - patchName.zip
// i.e.: [1.6.4] [32x] Sphax Patch - NoPatchName.zip
// -------------------------------------------------------------------------
var packName = size + suff,
zipName = '[' + dirname + '] [' + size + 'x] Sphax Patch - ' + (args.patchname || patchName) + '.zip',
targetDir = path.join(paths.dest, dirname, packName);
return gulp.src(targetDir + '/**', { base: targetDir })
// zip everything up
.pipe($.zip(zipName))
.pipe(gulp.dest(paths.dest))
// Log when zip has been written
.on('end', function () {
$.util.log($.util.colors.magenta('Created zip:'), $.util.colors.green(zipName));
});
} // /function zipStream
/* -----------------------------------------------------------------------------
* TASKS
* -------------------------------------------------------------------------- */
/* -----------------------------------------------------------------------------
* Task - makeZips
* -------------------------------------------------------------------------- */
gulp.task('makeZips', ['optimise'], function () {
// Set up a new merge-stream instance
var mergedStream = new $.mergeStream(),
dirs = [];
// Get the first-level dirs inside paths.dest
getDirs(paths.dest, function(destDirs) {
dirs = destDirs;
// For each dir (assume these are versions)
dirs.forEach(function(dir) {
// And for every resize level
sizes.forEach(function(size) {
// Push a new resizeStream to mergedStream
mergedStream.add(
new zipStream(dir, size)
);
}); // /sizes.forEach
}); // /dirs.forEach
}); // /getDirs(paths.src...
return mergedStream
.on('end', function () {
$.nodeNotifier.notify({
title: 'Sphax Patch - ' + (args.patchname || patchName),
message: 'Finished making zips!',
// icon: path.join(paths.src, dirs[0], 'pack.png')
});
});
}); // /gulp.task('makeZips'...
/* -----------------------------------------------------------------------------
* Task - optimise
* -------------------------------------------------------------------------- */
gulp.task('optimise', function () {
// Set up a new merge-stream instance
var mergedStream = new $.mergeStream();
// Get the first-level dirs inside paths.src
getDirs(paths.src, function(dirs) {
// For each dir (assume these are versions)
dirs.forEach(function(dir) {
// And for every resize level
sizes.forEach(function(size) {
// Push a new resizeStream to mergedStream
mergedStream.add(
new resizeStream(dir, size)
);
}); // /sizes.forEach
}); // /dirs.forEach
}); // /getDirs(paths.src...
// Return mergedStream, allows task to resolve once all async-added streams
// have collectively resolved
return mergedStream
.on('end', function () {
$.nodeNotifier.notify({
title: 'Sphax Patch - ' + (args.patchname || patchName),
message: 'Finished generating size packs!',
// icon: path.join(paths.src, dirs[0], 'pack.png')
});
});
});
// Watch task
// -----------------------------------------------------------------------------
gulp.task('watch', function () {
function watchFiles () {
$.util.log(
'\n\n',
$.util.colors.cyan('Watching for changes:\n'),
'\t' + $.util.colors.green('Source files:'), watchedPatterns.img,
'\n\n',
'Size packs will be regenerated when any of these filetypes change within',
$.util.colors.yellow('\'' + paths.src + '\''),
'.',
'\n'
);
// Watch Images
// ---------------------------------------------------------------------
gulp.watch(watchedPatterns.img, ['optimise']);
} // /function watchFiles
// for each dir inside paths.src
fs.readdir(paths.src, function (err, files) {
if (files.length) {
watchFiles();
}
else {
$.util.log(
'\n\n',
'\t' + 'No source files found to process.',
'\n',
'\t' + 'Make sure you\'ve placed the folders you want to process inside the',
$.util.colors.yellow('\'' + paths.src + '\''),
'directory.',
'\n',
'\t' + 'Alternatively, edit the',
$.util.colors.yellow('\'paths\''),
'variable in',
$.util.colors.yellow('\'Gulpfile.js\''),
'to customise the source/destination directories.'
);
}
}); // /fs.readdir(paths.src...
}); // /watch