├── .editorconfig ├── .gitattributes ├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── LICENSE ├── README.md ├── index.js ├── package.json └── test ├── fixtures ├── hello.min.txt └── hello.txt ├── path-parsing.spec.js ├── rename-sourcemap.spec.js ├── rename.spec.js └── spec-helper.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig helps developers define and maintain consistent 2 | # coding styles between different editors and IDEs 3 | # editorconfig.org 4 | 5 | root = true 6 | 7 | [*] 8 | 9 | # Change these settings to your own preference 10 | indent_style = space 11 | indent_size = 2 12 | 13 | # We recommend you to keep these unchanged 14 | end_of_line = lf 15 | charset = utf-8 16 | trim_trailing_whitespace = true 17 | insert_final_newline = true 18 | 19 | [*.md] 20 | trim_trailing_whitespace = false 21 | 22 | [test/fixtures/*] 23 | insert_final_newline = false 24 | trim_trailing_whitespace = false 25 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules/ 3 | temp/ 4 | package-lock.json 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '12' 4 | - '11' 5 | - '10' 6 | - '9' 7 | - '8' 8 | - '6' 9 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # v2.0.0 2 | 3 | #### Breaking 4 | 5 | - Add the ability to use the function argument as an immutable map function (This may be breaking for users who were relying on the return value of the function being ignored) 6 | 7 | #### Chores 8 | 9 | - Update deps 10 | - Switch from JSCS/JSHint to Prettier 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2013 Hector Guillermo Parra Alvarez 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gulp-rename 2 | 3 | gulp-rename is a [gulp](https://github.com/wearefractal/gulp) plugin to rename files easily. 4 | 5 | [![NPM](https://nodei.co/npm/gulp-rename.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/gulp-rename/) 6 | 7 | [![build status](https://secure.travis-ci.org/hparra/gulp-rename.svg)](http://travis-ci.org/hparra/gulp-rename) 8 | [![devDependency Status](https://david-dm.org/hparra/gulp-rename/dev-status.svg)](https://david-dm.org/hparra/gulp-rename#info=devDependencies) 9 | 10 | ## Usage 11 | 12 | gulp-rename provides simple file renaming methods. 13 | 14 | ```javascript 15 | var rename = require("gulp-rename"); 16 | 17 | // rename to a fixed value 18 | gulp.src("./src/main/text/hello.txt") 19 | .pipe(rename("main/text/ciao/goodbye.md")) 20 | .pipe(gulp.dest("./dist")); // ./dist/main/text/ciao/goodbye.md 21 | 22 | // rename via mutating function 23 | gulp.src("./src/**/hello.txt") 24 | .pipe(rename(function (path) { 25 | // Updates the object in-place 26 | path.dirname += "/ciao"; 27 | path.basename += "-goodbye"; 28 | path.extname = ".md"; 29 | })) 30 | .pipe(gulp.dest("./dist")); // ./dist/main/text/ciao/hello-goodbye.md 31 | 32 | // rename via a map function 33 | gulp.src("./src/**/hello.txt") 34 | .pipe(rename(function (path) { 35 | // Returns a completely new object, make sure you return all keys needed! 36 | return { 37 | dirname: path.dirname + "/ciao", 38 | basename: path.basename + "-goodbye", 39 | extname: ".md" 40 | }; 41 | })) 42 | .pipe(gulp.dest("./dist")); // ./dist/main/text/ciao/hello-goodbye.md 43 | 44 | // rename via a fixed object 45 | gulp.src("./src/main/text/hello.txt", { base: process.cwd() }) 46 | .pipe(rename({ 47 | dirname: "main/text/ciao", 48 | basename: "aloha", 49 | prefix: "bonjour-", 50 | suffix: "-hola", 51 | extname: ".md" 52 | })) 53 | .pipe(gulp.dest("./dist")); // ./dist/main/text/ciao/bonjour-aloha-hola.md 54 | ``` 55 | 56 | **See [test/rename.spec.js](test/rename.spec.js) for more examples and [test/path-parsing.spec.js](test/path-parsing.spec.js) for hairy details.** 57 | 58 | ## Notes 59 | 60 | * `dirname` is the relative path from the base directory set by `gulp.src` to the filename. 61 | * `gulp.src()` uses glob-stream which sets the base to the parent of the first directory glob (`*`, `**`, [], or extglob). `dirname` is the remaining directories or `./` if none. glob-stream versions >= 3.1.0 (used by gulp >= 3.2.2) accept a `base` option, which can be used to explicitly set the base. 62 | * `gulp.dest()` renames the directories between `process.cwd()` and `dirname` (i.e. the base relative to CWD). Use `dirname` to rename the directories matched by the glob or descendents of the base of option. 63 | * `basename` is the filename without the extension like `path.basename(filename, path.extname(filename))`. 64 | * `extname` is the file extension including the `.` like `path.extname(filename)`. 65 | * when using a function, a second `file` argument is provided with the whole context and original file value. 66 | * when using a function, if no `Object` is returned then the passed parameter object (along with any modifications) is re-used. 67 | 68 | ## License 69 | 70 | [MIT License](http://en.wikipedia.org/wiki/MIT_License) 71 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var Stream = require('stream'); 4 | var Path = require('path'); 5 | 6 | function gulpRename(obj, options) { 7 | options = options || {}; 8 | 9 | var stream = new Stream.Transform({ objectMode: true }); 10 | 11 | function parsePath(path) { 12 | var extname = options.multiExt 13 | ? Path.basename(path).slice(Path.basename(path).indexOf('.')) 14 | : Path.extname(path); 15 | return { 16 | dirname: Path.dirname(path), 17 | basename: Path.basename(path, extname), 18 | extname: extname 19 | }; 20 | } 21 | 22 | stream._transform = function(originalFile, unused, callback) { 23 | var file = originalFile.clone({ contents: false }); 24 | var parsedPath = parsePath(file.relative); 25 | var path; 26 | 27 | var type = typeof obj; 28 | 29 | if (type === 'string' && obj !== '') { 30 | path = obj; 31 | } else if (type === 'function') { 32 | let newParsedPath = obj(parsedPath, file); 33 | if (typeof newParsedPath === 'object' && newParsedPath !== null) { 34 | parsedPath = newParsedPath; 35 | } 36 | 37 | path = Path.join( 38 | parsedPath.dirname, 39 | parsedPath.basename + parsedPath.extname 40 | ); 41 | } else if (type === 'object' && obj !== undefined && obj !== null) { 42 | var dirname = 'dirname' in obj ? obj.dirname : parsedPath.dirname, 43 | prefix = obj.prefix || '', 44 | suffix = obj.suffix || '', 45 | basename = 'basename' in obj ? obj.basename : parsedPath.basename, 46 | extname = 'extname' in obj ? obj.extname : parsedPath.extname; 47 | 48 | path = Path.join(dirname, prefix + basename + suffix + extname); 49 | } else { 50 | callback( 51 | new Error('Unsupported renaming parameter type supplied'), 52 | undefined 53 | ); 54 | return; 55 | } 56 | 57 | file.path = Path.join(file.base, path); 58 | 59 | // Rename sourcemap if present 60 | if (file.sourceMap) { 61 | file.sourceMap.file = file.relative; 62 | } 63 | 64 | callback(null, file); 65 | }; 66 | 67 | return stream; 68 | } 69 | 70 | module.exports = gulpRename; 71 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gulp-rename", 3 | "version": "2.0.0", 4 | "description": "Rename files", 5 | "keywords": [ 6 | "gulpplugin" 7 | ], 8 | "homepage": "https://github.com/hparra/gulp-rename", 9 | "bugs": "https://github.com/hparra/gulp-rename/issues", 10 | "author": { 11 | "name": "Hector Guillermo Parra Alvarez", 12 | "email": "hector@hectorparra.com", 13 | "url": "https://github.com/hparra" 14 | }, 15 | "main": "./index.js", 16 | "files": [ 17 | "index.js" 18 | ], 19 | "repository": { 20 | "type": "git", 21 | "url": "git://github.com/hparra/gulp-rename.git" 22 | }, 23 | "scripts": { 24 | "pretest": "prettier --single-quote --write index.js test/**/*.js", 25 | "test": "mocha" 26 | }, 27 | "devDependencies": { 28 | "gulp": "^4.0.2", 29 | "gulp-sourcemaps": "^2.6.5", 30 | "map-stream": "^0.0.7", 31 | "mocha": "^6.0.0", 32 | "prettier": "^1.19.1", 33 | "should": "^13.2.3", 34 | "vinyl": "^2.0.0" 35 | }, 36 | "engines": { 37 | "node": ">=4" 38 | }, 39 | "license": "MIT" 40 | } 41 | -------------------------------------------------------------------------------- /test/fixtures/hello.min.txt: -------------------------------------------------------------------------------- 1 | Hello -------------------------------------------------------------------------------- /test/fixtures/hello.txt: -------------------------------------------------------------------------------- 1 | Hello -------------------------------------------------------------------------------- /test/path-parsing.spec.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | /* global helper */ 3 | 4 | require('./spec-helper'); 5 | var Path = require('path'); 6 | 7 | describe('gulp-rename path parsing', function() { 8 | describe('dirname', function() { 9 | context('when src pattern contains no globs', function() { 10 | it("dirname is '.'", function(done) { 11 | var srcPattern = 'test/fixtures/hello.txt'; 12 | var obj = function(path) { 13 | path.dirname.should.equal('.'); 14 | }; 15 | helper(srcPattern, obj, null, done); 16 | }); 17 | }); 18 | 19 | context('when src pattern contains filename glob', function() { 20 | it("dirname is '.'", function(done) { 21 | var srcPattern = 'test/fixtures/*.min.txt'; 22 | var obj = function(path) { 23 | path.dirname.should.equal('.'); 24 | }; 25 | helper(srcPattern, obj, null, done); 26 | }); 27 | }); 28 | 29 | var dirnameHelper = function(srcPattern) { 30 | it('dirname is path from directory glob to file', function(done) { 31 | var obj = function(path) { 32 | path.dirname.should.match(/^fixtures[0-9]?$/); 33 | }; 34 | helper(srcPattern, obj, null, done); 35 | }); 36 | }; 37 | 38 | context('when src pattern matches a directory with *', function() { 39 | dirnameHelper('test/*/*.min.txt'); 40 | }); 41 | 42 | context('when src pattern matches a directory with **', function() { 43 | dirnameHelper('test/**/*.min.txt'); 44 | }); 45 | 46 | context('when src pattern matches a directory with [...]', function() { 47 | dirnameHelper('test/fixt[a-z]res/*.min.txt'); 48 | }); 49 | 50 | context('when src pattern matches a directory with {...,...}', function() { 51 | dirnameHelper('test/f{ri,ixtur}es/*.min.txt'); 52 | }); 53 | 54 | /* SKIP: glob2base does not handle brace expansion as expected. See wearefractal/glob2base#1 */ 55 | context.skip( 56 | 'when src pattern matches a directory with {#..#}', 57 | function() { 58 | dirnameHelper('test/fixtures{0..9}/*.min.txt'); 59 | } 60 | ); 61 | 62 | context('when src pattern matches a directory with an extglob', function() { 63 | dirnameHelper('test/f+(ri|ixtur)es/*.min.txt'); 64 | }); 65 | 66 | context('when src pattern includes `base` option', function() { 67 | it('dirname is path from given directory to file', function(done) { 68 | var srcPattern = 'test/**/*.min.txt'; 69 | var srcOptions = { base: process.cwd() }; 70 | var obj = function(path) { 71 | path.dirname.should.equal(Path.join('test', 'fixtures')); 72 | }; 73 | helper({ pattern: srcPattern, options: srcOptions }, obj, null, done); 74 | }); 75 | }); 76 | }); 77 | 78 | describe('basename', function() { 79 | it('strips extension like Path.basename(path, ext)', function(done) { 80 | var srcPattern = 'test/fixtures/hello.min.txt'; 81 | var obj = function(path) { 82 | path.basename.should.equal('hello.min'); 83 | path.basename.should.equal( 84 | Path.basename(srcPattern, Path.extname(srcPattern)) 85 | ); 86 | }; 87 | helper(srcPattern, obj, null, done); 88 | }); 89 | }); 90 | 91 | describe('extname', function() { 92 | it("includes '.' like Path.extname", function(done) { 93 | var srcPattern = 'test/fixtures/hello.txt'; 94 | var obj = function(path) { 95 | path.extname.should.equal('.txt'); 96 | path.extname.should.equal(Path.extname(srcPattern)); 97 | }; 98 | helper(srcPattern, obj, null, done); 99 | }); 100 | 101 | it('excludes multiple extensions like Path.extname', function(done) { 102 | var srcPattern = 'test/fixtures/hello.min.txt'; 103 | var obj = function(path) { 104 | path.extname.should.equal('.txt'); 105 | path.extname.should.equal(Path.extname(srcPattern)); 106 | }; 107 | helper(srcPattern, obj, null, done); 108 | }); 109 | }); 110 | 111 | describe('multiExt option', function() { 112 | it('includes multiple extensions in extname', function(done) { 113 | var srcPattern = 'test/fixtures/hello.min.txt'; 114 | var obj = function(path) { 115 | path.extname.should.equal('.min.txt'); 116 | path.basename.should.equal('hello'); 117 | }; 118 | helper(srcPattern, obj, null, done, { multiExt: true }); 119 | }); 120 | }); 121 | 122 | describe('original file context', function() { 123 | it('passed to plugin as second argument', function(done) { 124 | var srcPattern = 'test/fixtures/hello.min.txt'; 125 | var obj = function(path, file) { 126 | file.should.be.instanceof(Object); 127 | file.should.be.ok(); 128 | }; 129 | helper(srcPattern, obj, null, done, { multiExt: true }); 130 | }); 131 | 132 | it('has expected properties', function(done) { 133 | var srcPattern = 'test/fixtures/hello.min.txt'; 134 | var obj = function(path, file) { 135 | file.path.should.equal(Path.resolve(srcPattern)); 136 | file.base.should.equal(Path.dirname(Path.resolve(srcPattern))); 137 | file.basename.should.equal(Path.basename(srcPattern)); 138 | file.relative.should.equal(Path.basename(srcPattern)); 139 | file.extname.should.equal(Path.extname(srcPattern)); 140 | }; 141 | helper(srcPattern, obj, null, done, { multiExt: true }); 142 | }); 143 | }); 144 | }); 145 | -------------------------------------------------------------------------------- /test/rename-sourcemap.spec.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var rename = require('../index'); 4 | var Vinyl = require('vinyl'); 5 | var sourceMaps = require('gulp-sourcemaps'); 6 | require('should'); 7 | 8 | describe('gulp-rename', function() { 9 | context('when file has source map', function() { 10 | it('updates source map file to match relative path of renamed file', function(done) { 11 | var init = sourceMaps.init(); 12 | var stream = init 13 | .pipe(rename({ prefix: 'test-' })) 14 | .pipe(rename({ prefix: 'test-' })); 15 | 16 | stream.on('data', function(file) { 17 | file.sourceMap.file.should.equal('test-test-fixture.css'); 18 | file.sourceMap.file.should.equal(file.relative); 19 | done(); 20 | }); 21 | 22 | init.write( 23 | new Vinyl({ 24 | base: 'fixtures', 25 | path: 'fixtures/fixture.css', 26 | contents: new Buffer('') 27 | }) 28 | ); 29 | 30 | init.end(); 31 | }); 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /test/rename.spec.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | /* global helper, helperError */ 3 | 4 | require('./spec-helper'); 5 | var rename = require('../'); 6 | var gulp = require('gulp'); 7 | var Path = require('path'); 8 | 9 | describe('gulp-rename', function() { 10 | context('with string parameter', function() { 11 | context('when src pattern does not contain directory glob', function() { 12 | it('sets filename to value', function(done) { 13 | var srcPattern = 'test/fixtures/hello.txt'; 14 | var obj = 'hola.md'; 15 | var expectedPath = 'test/fixtures/hola.md'; 16 | helper(srcPattern, obj, expectedPath, done); 17 | }); 18 | }); 19 | context('when src pattern contains directory glob', function() { 20 | it('sets relative path to value', function(done) { 21 | var srcPattern = 'test/**/hello.txt'; 22 | var obj = 'fixtures/hola.md'; 23 | var expectedPath = 'test/fixtures/hola.md'; 24 | helper(srcPattern, obj, expectedPath, done); 25 | }); 26 | }); 27 | }); 28 | 29 | context('with object parameter', function() { 30 | var srcPattern; 31 | beforeEach(function() { 32 | srcPattern = 'test/**/hello.txt'; 33 | }); 34 | 35 | context('with empty object', function() { 36 | it('has no effect', function(done) { 37 | var obj = {}; 38 | var expectedPath = 'test/fixtures/hello.txt'; 39 | helper(srcPattern, obj, expectedPath, done); 40 | }); 41 | }); 42 | 43 | context('with dirname value', function() { 44 | it('replaces dirname with value', function(done) { 45 | var obj = { 46 | dirname: 'elsewhere' 47 | }; 48 | var expectedPath = 'test/elsewhere/hello.txt'; 49 | helper(srcPattern, obj, expectedPath, done); 50 | }); 51 | it("removes dirname with './'", function(done) { 52 | var obj = { 53 | dirname: './' 54 | }; 55 | var expectedPath = 'test/hello.txt'; 56 | helper(srcPattern, obj, expectedPath, done); 57 | }); 58 | it('removes dirname with empty string', function(done) { 59 | var obj = { 60 | dirname: '' 61 | }; 62 | var expectedPath = 'test/hello.txt'; 63 | helper(srcPattern, obj, expectedPath, done); 64 | }); 65 | }); 66 | 67 | context('with prefix value', function() { 68 | it('prepends value to basename', function(done) { 69 | var obj = { 70 | prefix: 'bonjour-' 71 | }; 72 | var expectedPath = 'test/fixtures/bonjour-hello.txt'; 73 | helper(srcPattern, obj, expectedPath, done); 74 | }); 75 | }); 76 | 77 | context('with basename value', function() { 78 | it('replaces basename with value', function(done) { 79 | var obj = { 80 | basename: 'aloha' 81 | }; 82 | var expectedPath = 'test/fixtures/aloha.txt'; 83 | helper(srcPattern, obj, expectedPath, done); 84 | }); 85 | it('removes basename with empty string (for consistency)', function(done) { 86 | var obj = { 87 | prefix: 'aloha', 88 | basename: '' 89 | }; 90 | var expectedPath = 'test/fixtures/aloha.txt'; 91 | helper(srcPattern, obj, expectedPath, done); 92 | }); 93 | }); 94 | 95 | context('with suffix value', function() { 96 | it('appends value to basename', function(done) { 97 | var obj = { 98 | suffix: '-hola' 99 | }; 100 | var expectedPath = 'test/fixtures/hello-hola.txt'; 101 | helper(srcPattern, obj, expectedPath, done); 102 | }); 103 | }); 104 | 105 | context('with extname value', function() { 106 | it('replaces extname with value', function(done) { 107 | var obj = { 108 | extname: '.md' 109 | }; 110 | var expectedPath = 'test/fixtures/hello.md'; 111 | helper(srcPattern, obj, expectedPath, done); 112 | }); 113 | it('removes extname with empty string', function(done) { 114 | var obj = { 115 | extname: '' 116 | }; 117 | var expectedPath = 'test/fixtures/hello'; 118 | helper(srcPattern, obj, expectedPath, done); 119 | }); 120 | }); 121 | }); 122 | 123 | context('with function parameter', function() { 124 | var srcPattern; 125 | beforeEach(function() { 126 | srcPattern = 'test/**/hello.txt'; 127 | }); 128 | 129 | it('receives object with dirname', function(done) { 130 | var obj = function(path) { 131 | path.dirname.should.equal('fixtures'); 132 | path.dirname = 'elsewhere'; 133 | }; 134 | var expectedPath = 'test/elsewhere/hello.txt'; 135 | helper(srcPattern, obj, expectedPath, done); 136 | }); 137 | 138 | it('receives object with basename', function(done) { 139 | var obj = function(path) { 140 | path.basename.should.equal('hello'); 141 | path.basename = 'aloha'; 142 | }; 143 | var expectedPath = 'test/fixtures/aloha.txt'; 144 | helper(srcPattern, obj, expectedPath, done); 145 | }); 146 | 147 | it('receives object with extname', function(done) { 148 | var obj = function(path) { 149 | path.extname.should.equal('.txt'); 150 | path.extname = '.md'; 151 | }; 152 | var expectedPath = 'test/fixtures/hello.md'; 153 | helper(srcPattern, obj, expectedPath, done); 154 | }); 155 | 156 | it('receives object from return value', function(done) { 157 | var obj = function(path) { 158 | return { 159 | dirname: path.dirname, 160 | basename: path.basename, 161 | extname: '.md' 162 | }; 163 | }; 164 | var expectedPath = 'test/fixtures/hello.md'; 165 | helper(srcPattern, obj, expectedPath, done); 166 | }); 167 | 168 | it('ignores null return value but uses passed object', function(done) { 169 | var obj = function(path) { 170 | path.extname.should.equal('.txt'); 171 | path.extname = '.md'; 172 | return null; 173 | }; 174 | var expectedPath = 'test/fixtures/hello.md'; 175 | helper(srcPattern, obj, expectedPath, done); 176 | }); 177 | 178 | it('receives object with extname even if a different value is returned', function(done) { 179 | var obj = function(path) { 180 | path.extname.should.equal('.txt'); 181 | path.extname = '.md'; 182 | }; 183 | var expectedPath = 'test/fixtures/hello.md'; 184 | helper(srcPattern, obj, expectedPath, done); 185 | }); 186 | }); 187 | 188 | context('in parallel streams', function() { 189 | it('only changes the file in the current stream', function(done) { 190 | var files = gulp.src('test/fixtures/hello.txt'); 191 | 192 | var pipe1 = files.pipe(rename({ suffix: '-1' })); 193 | var pipe2 = files.pipe(rename({ suffix: '-2' })); 194 | var end1 = false; 195 | var end2 = false; 196 | var file1; 197 | var file2; 198 | 199 | function check() { 200 | file1.path.should.equal(Path.resolve('test/fixtures/hello-1.txt')); 201 | file2.path.should.equal(Path.resolve('test/fixtures/hello-2.txt')); 202 | 203 | return done(); 204 | } 205 | 206 | pipe1 207 | .on('data', function(file) { 208 | file1 = file; 209 | }) 210 | .on('end', function() { 211 | end1 = true; 212 | 213 | if (end2) { 214 | return check(); 215 | } 216 | }); 217 | 218 | pipe2 219 | .on('data', function(file) { 220 | file2 = file; 221 | }) 222 | .on('end', function() { 223 | end2 = true; 224 | 225 | if (end1) { 226 | return check(); 227 | } 228 | }); 229 | }); 230 | }); 231 | 232 | context('throws unsupported parameter type', function() { 233 | var srcPattern; 234 | beforeEach(function() { 235 | srcPattern = 'test/**/hello.txt'; 236 | }); 237 | 238 | var UNSUPPORTED_PARAMATER = 'Unsupported renaming parameter type supplied'; 239 | it('with undefined object', function(done) { 240 | var obj; 241 | var expectedError = UNSUPPORTED_PARAMATER; 242 | helperError(srcPattern, obj, expectedError, done); 243 | }); 244 | 245 | it('with null object', function(done) { 246 | var obj = null; 247 | var expectedError = UNSUPPORTED_PARAMATER; 248 | helperError(srcPattern, obj, expectedError, done); 249 | }); 250 | 251 | it('with empty string', function(done) { 252 | var obj = ''; 253 | var expectedError = UNSUPPORTED_PARAMATER; 254 | helperError(srcPattern, obj, expectedError, done); 255 | }); 256 | 257 | it('with boolean value', function(done) { 258 | var obj = true; 259 | var expectedError = UNSUPPORTED_PARAMATER; 260 | helperError(srcPattern, obj, expectedError, done); 261 | }); 262 | 263 | it('with numeric value', function(done) { 264 | var obj = 1; 265 | var expectedError = UNSUPPORTED_PARAMATER; 266 | helperError(srcPattern, obj, expectedError, done); 267 | }); 268 | }); 269 | }); 270 | -------------------------------------------------------------------------------- /test/spec-helper.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | require('should'); 4 | require('mocha'); 5 | 6 | var Path = require('path'), 7 | gulp = require('gulp'), 8 | rename = require('../'); 9 | 10 | global.helper = function(srcArgs, obj, expectedPath, done, options) { 11 | var srcPattern = srcArgs.pattern || srcArgs; 12 | var srcOptions = srcArgs.options || {}; 13 | var stream = gulp.src(srcPattern, srcOptions).pipe(rename(obj, options)); 14 | var count = 0; 15 | stream.on('error', done); 16 | stream.on('data', function() { 17 | count++; 18 | }); 19 | if (expectedPath) { 20 | stream.on('data', function(file) { 21 | var resolvedExpectedPath = Path.resolve(expectedPath); 22 | var resolvedActualPath = Path.join(file.base, file.relative); 23 | resolvedActualPath.should.equal(resolvedExpectedPath); 24 | }); 25 | } 26 | stream.on('end', function() { 27 | count.should.be.greaterThan(0); 28 | done.apply(this, arguments); 29 | }); 30 | }; 31 | 32 | global.helperError = function(srcPattern, obj, expectedError, done) { 33 | var stream = gulp.src(srcPattern).pipe(rename(obj)); 34 | stream.on('error', function(err) { 35 | err.message.should.equal(expectedError); 36 | done(); 37 | }); 38 | stream.on('data', function() {}); 39 | stream.on('end', done); 40 | }; 41 | --------------------------------------------------------------------------------