-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
93 lines (69 loc) · 2.1 KB
/
index.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
/* jshint unused:false */
'use strict';
var through2 = require('through2'),
mime = require('mime'),
gutil = require('gulp-util'),
_ = require('lodash'),
del = require('del'),
fs = require('fs');
var PLUGIN_NAME = 'gulp-delete-unused-images';
function deleteUnusedImages(options) {
options = options || {};
_.defaults(options, {delete: true, log: false});
var imageNames = [];
var usedImageNames = [];
var transform = through2.obj(function (chunk, enc, callback) {
var self = this;
if (chunk.isNull()) {
self.push(chunk);
return callback();
}
if (chunk.isStream()) {
return callback(new gutil.PluginError(PLUGIN_NAME, 'Streaming not supported'));
}
if (mime.lookup(chunk.path).match(/image\//)) {
imageNames.push(chunk.path);
return callback();
}
try {
var rl = require('readline').createInterface({
input: fs.createReadStream(String(chunk.path)),
terminal: false
}).on('line', function (line) {
var filename = (line.match(/((?:((?:[^\(\\\'\"\r\n\t\f\/\s\.])+)\.(?:(png|gif|jpe?g|pdf|xml|apng|svg|mng)\b)))/gmi) || []).pop();
if (filename) {
usedImageNames.push(filename);
}
}).on('close', function () {
self.push(chunk);
callback();
});
} catch (e) {
console.log(e);
callback();
}
});
transform.on('finish', function () {
_.mixin({
findUsedImages: function (imageNames, usedImageNames) {
return _.filter(imageNames, function (path) {
return _.includes(usedImageNames, _(path).split('/').last());
});
}
});
var usedImages = _.findUsedImages(imageNames, usedImageNames);
var unusedImages = _.difference(imageNames, usedImages);
if (unusedImages.length) {
if (options.delete) {
del(unusedImages).then(paths => {
console.log('Deleted images:\n', paths.join('\n'));
});
}
if (options.log) {
console.log('Unused images:\n', unusedImages.join('\n'));
}
}
});
return transform;
}
module.exports = deleteUnusedImages;