forked from shonny-ua/gulp-rev-outdated
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
40 lines (36 loc) · 1.2 KB
/
index.js
File metadata and controls
40 lines (36 loc) · 1.2 KB
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
'use strict';
var gutil = require('gulp-util');
var PluginError = gutil.PluginError;
var through = require('through2');
var path = require('path');
var PLUGIN_NAME = 'gulp-rev-outdated';
function plugin(keepQuantity) {
keepQuantity = parseInt(keepQuantity) || 2;
var lists = {};
return through.obj(function (file, enc, cb) {
var regex = new RegExp('^(.*)-[0-9a-f]{8,10}(?:\\.min)?\\' + path.extname(file.path) + '$');
if (regex.test(file.path)) {
var identifier = regex.exec(file.path)[1] + path.extname(file.path);
if (lists[identifier] === undefined) {
lists[identifier] = [];
}
lists[identifier].push({
file: file,
time: file.stat.ctime.getTime()
});
}
cb();
}, function (cb) {
Object.keys(lists).forEach(function (identifier) {
lists[identifier].sort(function (a, b) {
return b.time - a.time;
})
.slice(keepQuantity)
.forEach(function (f) {
this.push(f.file);
}, this);
}, this);
cb();
});
}
module.exports = plugin;