Skip to content

Commit 9ca70a7

Browse files
committed
0.0.2: renaming from gulp-rev-cleaner to gulp-rev-outdated + Docs && test
1 parent 338b355 commit 9ca70a7

File tree

5 files changed

+172
-14
lines changed

5 files changed

+172
-14
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
language: node_js
2+
node_js:
3+
- '0.10'

README.md

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,50 @@
1-
gulp-rev-cleaner
2-
================
1+
# [gulp](https://github.com/wearefractal/gulp)-rev-outdated [![Build Status](https://travis-ci.org/shonny-ua/gulp-rev-outdated.svg?branch=master)](https://travis-ci.org/shonny-ua/gulp-rev-outdated)
2+
3+
> Old static asset revision files filter.
4+
5+
## Install
6+
7+
```sh
8+
$ npm install --save-dev gulp-rev-outdated
9+
```
10+
11+
## Usage
12+
13+
We can use [gulp-rev](https://github.com/sindresorhus/gulp-rev) to cache-bust several assets. Every modification of source files caused a new revisioned asset creation. In case of using separate http://static.exsample.com/ domain for distributing static assets we have some problem with a lot of accumulated revisioned asset files. If we have several different frontends (e.q. [www-1.exsample.com, www-2.exsample.cpm, ... www-12.exsample com]) worked with different software releases, We can't remove all revisioned asset files on static.exsample.com. We need to save number of recent revisioned assets. gulp-rev-outdated filter revisioned assets and exclude parametrised quantity of recent files for removing.
14+
15+
Takes one parameter [ keepQuantity ] - number of saved recent files.
16+
Default value == 2.
17+
18+
```js
19+
var gulp = require('gulp');
20+
var revOutdated = require('gulp-rev-outdated');
21+
var cleaner = require('gulp-rimraf');
22+
23+
gulp.task('clean', function() {
24+
gulp.src( ['dist/js/vendors*.js'], {read: false})
25+
.pipe( revOutdated(1) ) // leave 1 latest asset file
26+
.pipe( cleaner() );
27+
28+
gulp.src( ['dist/js/bundle*.js'], {read: false})
29+
.pipe( revOutdated(3) ) // leave 3 recent assets
30+
.pipe( cleaner() );
31+
32+
gulp.src( ['dist/css/*.css'], {read: false})
33+
.pipe( revOutdated() ) // leave 2 recent assets (default value)
34+
.pipe( cleaner() );
35+
36+
return;
37+
});
38+
```
39+
40+
gulp.src option read false prevents gulp to read the contents of the file and makes this task a lot faster. If you need the file and it's contents after cleaning in the same stream, do not set the read option to false.
41+
42+
### Works with gulp-rev-outdated
43+
44+
- [gulp-rev](https://github.com/sindresorhus/gulp-rev)
45+
- [gulp-rimraf](https://github.com/robrich/gulp-rimraf)
46+
- [gulp-clean](https://github.com/peter-vilja/gulp-clean)
47+
48+
## License
49+
50+
[MIT](http://opensource.org/licenses/MIT) © [Oleksandr Ovcharenko](mailto:shonny.ua@gmail.com)

index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
'use strict';
2+
var gutil = require('gulp-util');
3+
var PluginError = gutil.PluginError;
14
var through = require('through2');
25
var path = require('path');
36

7+
var PLUGIN_NAME = 'gulp-rev-outdated';
8+
49
function plugin(keepQuantity){
510
keepQuantity = parseInt(keepQuantity) || 2;
611
var list = [];
@@ -17,11 +22,10 @@ function plugin(keepQuantity){
1722
list.sort(function(a, b){
1823
return b.time - a.time;
1924
})
20-
.slice(keepQuantity)
25+
.slice( keepQuantity )
2126
.forEach(function(f){
2227
this.push(f.file);
2328
}, this);
24-
2529
cb();
2630
});
2731
}

package.json

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
{
2-
"name": "gulp-rev-cleaner",
3-
"version": "0.0.1",
4-
"description": "Old static asset revision files cleanup",
2+
"name": "gulp-rev-outdated",
3+
"version": "0.0.2",
4+
"description": "Old static asset revision files filter",
55
"license": "MIT",
66
"repository": {
77
"type": "git",
8-
"url": "https://github.com/shonny-ua/gulp-rev-cleaner"
8+
"url": "https://github.com/shonny-ua/gulp-rev-outdated"
9+
},
10+
"author": {
11+
"name": "Oleksandr Ovcharenko",
12+
"email": "shonny.ua@gmail.com"
913
},
10-
"author": "Oleksandr Ovcharenko <shonny.ua@gmail.com>",
1114
"main": "index.js",
1215
"files": [
1316
"index.js"
@@ -29,21 +32,25 @@
2932
"versioning",
3033
"cache",
3134
"expire",
35+
"old",
36+
"outdated",
3237
"static",
3338
"asset",
3439
"assets",
3540
"clean",
36-
"cleanup"
41+
"cleanup",
42+
"filtering",
43+
"filter"
3744
],
3845
"dependencies": {
39-
"through2": ">=0.5.1"
46+
"through2": ">=0.5.1",
47+
"gulp-util": "^2.2.17"
4048
},
4149
"devDependencies": {
4250
"mocha": "*"
4351
},
4452
"bugs": {
45-
"url": "https://github.com/shonny-ua/gulp-rev-cleaner/issues"
53+
"url": "https://github.com/shonny-ua/gulp-rev-outdated/issues"
4654
},
47-
"_from": "gulp-rev-cleaner@0.0.1",
48-
"homepage": "https://github.com/shonny-ua/gulp-rev-cleaner"
55+
"homepage": "https://github.com/shonny-ua/gulp-rev-outdated"
4956
}

test.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
'use strict';
2+
var assert = require('assert');
3+
var gutil = require('gulp-util');
4+
var revOutdated = require('./index');
5+
var path = require('path');
6+
7+
var assets = [
8+
{path: 'css/style.css', time: 0},
9+
{path: 'css/stylexxx.css', time: 0},
10+
{path: 'css/style-22222222.css', time: 1403184415416},
11+
{path: 'css/style-61e0be79.css', time: 1403184377571},
12+
{path: 'css/style-a42f5380.css', time: 1403184303451},
13+
{path: 'css/style-1d87bebe.css', time: 1222222222222},
14+
{path: 'css/style-11111111.css', time: 1111111111111},
15+
{path: 'css/style-00000000.css', time: 0},
16+
];
17+
18+
var keepQuantity;
19+
var filteredQantity;
20+
var fileCount;
21+
22+
it('should filter 5 files', function (cb) {
23+
keepQuantity = 1;
24+
filteredQantity = 6 - keepQuantity;
25+
fileCount = 0;
26+
27+
var stream = initStream( revOutdated(keepQuantity) );
28+
29+
stream.on('data', streamDataCheck);
30+
31+
stream.on('end', function() {
32+
assert.equal(fileCount, filteredQantity, 'Only ' + filteredQantity + ' files should pass through the stream');
33+
cb();
34+
});
35+
36+
stream.end();
37+
});
38+
39+
it('should filter 4 files using default keepQuantity option', function (cb) {
40+
keepQuantity = undefined;
41+
filteredQantity = 6 - 2;
42+
fileCount = 0;
43+
44+
var stream = initStream( revOutdated() );
45+
46+
stream.on('data', streamDataCheck);
47+
48+
stream.on('end', function() {
49+
assert.equal(fileCount, filteredQantity, 'Only ' + filteredQantity + ' files should pass through the stream');
50+
cb();
51+
});
52+
53+
stream.end();
54+
});
55+
56+
it('should filter correct files', function (cb) {
57+
keepQuantity = 3;
58+
filteredQantity = 6 - keepQuantity;
59+
fileCount = 0;
60+
61+
var stream = initStream( revOutdated(keepQuantity) );
62+
63+
stream.on('data', function(file){
64+
streamDataCheck(file);
65+
assert(
66+
/\/style-(1d87bebe|11111111|00000000)\.css/.test(file.path),
67+
'should filter correct files'
68+
);
69+
});
70+
71+
stream.on('end', function() {
72+
assert.equal(fileCount, filteredQantity, 'Only ' + filteredQantity + ' files should pass through the stream');
73+
cb();
74+
});
75+
76+
stream.end();
77+
});
78+
79+
function initStream(stream) {
80+
assets.forEach(function(asset){
81+
stream.write(new gutil.File({
82+
path: asset.path,
83+
stat: {ctime: new Date(asset.time)},
84+
contents: new Buffer(' ')
85+
}));
86+
});
87+
return stream;
88+
}
89+
90+
function streamDataCheck (file) {
91+
assert(
92+
/\/style-[0-9a-f]{8}\.css/.test(file.path),
93+
'should filter only revisioned files'
94+
);
95+
fileCount++;
96+
}

0 commit comments

Comments
 (0)