├── .gitignore ├── LICENSE.md ├── README.md ├── index.js ├── package.json └── test └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | build/ 3 | node_modules -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 James Newell 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gulp-rev-delete-original 2 | 3 | Delete the original file rewritten by 4 | [gulp-rev](https://www.npmjs.com/package/gulp-rev) or 5 | [gulp-rev-all](https://www.npmjs.com/package/gulp-rev-all). 6 | 7 | ## Installation 8 | 9 | npm install --save-dev gulp-rev-delete-original 10 | 11 | ## Usage 12 | 13 | ```js 14 | var gulp = require('gulp'); 15 | var rev = require('gulp-rev'); 16 | var revcss = require('gulp-rev-css-url'); 17 | var revdel = require('gulp-rev-delete-original'); 18 | 19 | gulp.task('rev', function () { 20 | return gulp.src('./app/**/*') 21 | .pipe(rev()) 22 | .pipe(revcss()) 23 | .pipe(revdel()) 24 | .pipe(gulp.dest('./build/')) 25 | ; 26 | }); 27 | ``` 28 | 29 | ## Options 30 | 31 | #### exclude 32 | 33 | A filter `RegExp` or `function` that allows you to exclude certain files from being deleted. 34 | 35 | ##### Example 36 | 37 | RegExp: 38 | 39 | ```js 40 | revdel({ 41 | exclude: /build\.css$/ 42 | }); 43 | ``` 44 | 45 | Function: 46 | 47 | ```js 48 | revdel({ 49 | exclude: function(file) { 50 | if (/build\.css$/.test(file.name)) { 51 | return true; //if you want to exclude the file from being deleted 52 | } 53 | } 54 | }); 55 | ``` 56 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var through = require('through2'); 2 | var rimraf = require('rimraf'); 3 | 4 | /** 5 | * Return a stream for deleting the original file 6 | * @param {object} [options] 7 | * @param {RegExp|function} [options.exclude] 8 | * @param {function} [options.remove] 9 | * @returns {function} 10 | */ 11 | module.exports = function(options) { 12 | 13 | var exclude = options && options.exclude || false; 14 | var remove = options && options.remove || rimraf; 15 | 16 | options = options || {}; 17 | return through.obj(function(file, enc, cb) { 18 | 19 | //delete the original file 20 | var del = function() { 21 | if(file.revOrigPath) { 22 | rimraf(file.revOrigPath, function(err) { 23 | if (err) return cb(err); 24 | cb(null, file); 25 | }); 26 | } else { 27 | cb(null); 28 | } 29 | }; 30 | 31 | //don't delete files that haven't been rewritten 32 | if (file.revOrigPath === file.path) { 33 | return cb(null, file); 34 | } 35 | 36 | //exclude files from being deleted 37 | if (exclude) { 38 | 39 | var 40 | excluded, 41 | filter = exclude 42 | ; 43 | 44 | if (typeof filter === 'function') { 45 | excluded = filter(file); 46 | } else if(filter instanceof RegExp) { 47 | excluded = filter.test(file.path); 48 | } 49 | 50 | if (excluded) { 51 | return cb(null, file); 52 | } else { 53 | 54 | //delete the original file 55 | return del(); 56 | 57 | } 58 | 59 | } else { 60 | 61 | //delete the original file 62 | return del(); 63 | 64 | } 65 | 66 | }); 67 | }; 68 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gulp-rev-delete-original", 3 | "version": "0.2.3", 4 | "description": "Delete the original file rewritten by gulp-rev.", 5 | "keywords": [ 6 | "gulpplugin", 7 | "rev", 8 | "rev-del", 9 | "gulp-rev", 10 | "gulp-rev-all", 11 | "gulp-rev-napkin" 12 | ], 13 | "repository": { 14 | "type": "git", 15 | "url": "git@github.com:nib-health-funds/gulp-rev-delete-original.git" 16 | }, 17 | "dependencies": { 18 | "rimraf": "^2.3.4", 19 | "through2": "^0.6.5" 20 | }, 21 | "devDependencies": { 22 | "mocha": "^2.4.5", 23 | "vinyl": "^1.1.1" 24 | }, 25 | "scripts": { 26 | "test": "mocha" 27 | }, 28 | "license": "MIT" 29 | } 30 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var File = require('vinyl'); 3 | var revDel = require('..'); 4 | 5 | //TODO: exclude: function vs regex 6 | 7 | describe('gulp-rev-delete-original', function() { 8 | 9 | it('should not remove the original file when it has not been rewritten', function(done) { 10 | var removeWasCalled = false; 11 | 12 | var file = new File({ 13 | cwd: '/', 14 | base: '/test/', 15 | path: '/dist/index.js', 16 | contents: new Buffer('') 17 | }); 18 | file.revOrigPath = '/dist/index.js'; 19 | 20 | var stream = revDel({remove: function(path, callback) { 21 | assert(false); 22 | }}); 23 | 24 | stream.on('data', function() { 25 | done(); 26 | }); 27 | 28 | stream.write(file); 29 | 30 | }); 31 | 32 | it('should remove the original original file when it has been rewritten', function(done) { 33 | var removeWasCalled = false; 34 | 35 | var file = new File({ 36 | cwd: '/', 37 | base: '/test/', 38 | path: '/dist/index.abcd.js', 39 | contents: new Buffer('') 40 | }); 41 | file.revOrigPath = '/dist/index.js'; 42 | 43 | var stream = revDel({remove: function(path, callback) { 44 | removeWasCalled = true; 45 | 46 | //make sure we're removing the ORIGINAL file 47 | assert.equal(path, file.revOrigPath); 48 | 49 | callback(null); 50 | }}); 51 | 52 | stream.on('data', function() { 53 | 54 | //make sure we removed a file 55 | assert(removeWasCalled); 56 | 57 | done(); 58 | }); 59 | 60 | stream.write(file); 61 | 62 | }); 63 | 64 | it('should remove the original file when it has been rewritten and has not been excluded', function(done) { 65 | var removeWasCalled = false; 66 | 67 | var file = new File({ 68 | cwd: '/', 69 | base: '/test/', 70 | path: '/dist/index.abcd.js', 71 | contents: new Buffer('') 72 | }); 73 | file.revOrigPath = '/dist/index.js'; 74 | 75 | var stream = revDel({ 76 | 77 | exclude: function() { 78 | return false 79 | }, 80 | 81 | remove: function(path, callback) { 82 | removeWasCalled = true; 83 | 84 | //make sure we're removing the ORIGINAL file 85 | assert.equal(path, file.revOrigPath); 86 | 87 | callback(null); 88 | } 89 | }); 90 | 91 | stream.on('data', function() { 92 | 93 | //make sure we removed a file 94 | assert(removeWasCalled); 95 | 96 | done(); 97 | }); 98 | 99 | stream.write(file); 100 | 101 | }); 102 | 103 | it('should not remove the original file when it has been rewritten and has been excluded', function(done) { 104 | var removeWasCalled = false; 105 | 106 | var file = new File({ 107 | cwd: '/', 108 | base: '/test/', 109 | path: '/dist/index.abcd.js', 110 | contents: new Buffer('') 111 | }); 112 | file.revOrigPath = '/dist/index.js'; 113 | 114 | var stream = revDel({ 115 | 116 | exclude: function() { 117 | return true 118 | }, 119 | 120 | remove: function(path, callback) { 121 | removeWasCalled = true; 122 | 123 | //make sure we're removing the ORIGINAL file 124 | assert.equal(path, file.revOrigPath); 125 | 126 | callback(null); 127 | } 128 | }); 129 | 130 | stream.on('data', function() { 131 | 132 | //make sure we removed a file 133 | assert(!removeWasCalled); 134 | 135 | done(); 136 | }); 137 | 138 | stream.write(file); 139 | 140 | }); 141 | 142 | }); --------------------------------------------------------------------------------