├── .gitignore ├── buffer.js ├── package.json ├── index.js ├── README.md ├── test.js └── .eslintrc /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/* 2 | *.DS_Store -------------------------------------------------------------------------------- /buffer.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var through = require('through2'); 4 | 5 | module.exports = function(func) { 6 | var buffer = []; 7 | var end = function(callback) { 8 | this.push(buffer); 9 | callback(); 10 | return func && func(null, buffer); 11 | }; 12 | var push = function(chunk, enc, callback) { 13 | buffer.push(chunk); 14 | callback(); 15 | }; 16 | return through.obj(push, end); 17 | }; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "stream-rename", 3 | "version": "1.0.1", 4 | "description": "A transform to rename files/folders in a stream", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "tape test.js" 8 | }, 9 | "keywords": [ 10 | "rename", 11 | "transform", 12 | "stream", 13 | "gulpfriendly", 14 | "through2" 15 | ], 16 | "repository": { 17 | "type": "git", 18 | "url": "https://github.com/davidchase/stream-rename.git" 19 | }, 20 | "bugs": { 21 | "url": "https://github.com/davidchase/stream-rename/issues" 22 | }, 23 | "author": "David Chase ", 24 | "license": "MIT", 25 | "dependencies": { 26 | "through2": "^0.6.3" 27 | }, 28 | "devDependencies": { 29 | "glob-stream": "^3.1.18", 30 | "tape": "^3.0.3" 31 | } 32 | } -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var through = require('through2'); 4 | var path = require('path'); 5 | 6 | // build string to rename file/directory 7 | // based on user options 8 | var _stringBuilder = function(options, oldBase) { 9 | var prefix = options.prefix || ''; 10 | var suffix = options.suffix || ''; 11 | var getExtname = path.extname(oldBase); 12 | var extname = options.extname || getExtname; 13 | var basename = options.basename || oldBase; 14 | basename = getExtname ? basename.replace(/\.[a-z]*/, '') : basename; 15 | return prefix + basename + suffix + extname; 16 | }; 17 | 18 | var streamRename = function(options) { 19 | options = options || {}; 20 | return through.obj(function(chunk, enc, callback) { 21 | var oldBase; 22 | var arr = chunk.path.split('/'); 23 | var root = options.root || chunk.base; 24 | root = arr.indexOf(path.basename(root)); 25 | oldBase = arr[root + 1]; 26 | arr[root + 1] = _stringBuilder(options, oldBase); 27 | chunk.path = arr.join('/'); 28 | this.push(chunk); 29 | callback(); 30 | }); 31 | }; 32 | 33 | module.exports = streamRename; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Stream Rename 2 | ============= 3 | 4 | > A simple stream transform that renames files/directories. 5 | 6 | This module makes no assumptions how you read or write streams. 7 | 8 | 9 | Usage 10 | ===== 11 | ```js 12 | // create a stream 13 | var globSteam = require('glob-stream'); 14 | var streamRename = require('stream-rename'); 15 | globSteam 16 | .create('./app.js') 17 | .pipe(streamRename({ 18 | basename: 'compressed', 19 | extname: '.min.js' 20 | })) 21 | .pipe(process.stdout); // path will contain compressed.min.js 22 | ``` 23 | 24 | Gulp Usage 25 | ========== 26 | ```js 27 | // create a stream 28 | var gulp = require('gulp'); 29 | var streamRename = require('stream-rename'); 30 | gulp.src('./app.js') 31 | .pipe(streamRename({ 32 | basename: 'compressed', 33 | extname: '.min.js' 34 | })) 35 | .pipe(gulp.dest('./dest')); // path will contain compressed.min.js 36 | ``` 37 | 38 | API 39 | === 40 | 41 | ```js 42 | var streamRename = require('stream-rename'); 43 | ``` 44 | 45 | var sr = streamRename(opts={}) 46 | ------ 47 | 48 | `opts.prefix` a prefix to the new or original basename. ie: `pre-` 49 | 50 | `opts.basename` is the new name of the file or directory you want to change. 51 | 52 | `opts.extname` is a new extension you want to add to the basename. ie: `.js` 53 | 54 | `opts.suffix` a suffix to the new or original basename. ie: `-ing` 55 | 56 | returns a `through` stream to `pipe` to a write stream. 57 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var globSteam = require('glob-stream'); 4 | var path = require('path'); 5 | var test = require('tape'); 6 | var streamRename = require('./index'); 7 | var buffer = require('./buffer'); 8 | 9 | test('simple rename keeps extension', function(t) { 10 | t.plan(1); 11 | globSteam 12 | .create('./package.json') 13 | .pipe(streamRename({ 14 | basename: 'what' 15 | })) 16 | .pipe(buffer(function(err, files) { 17 | if (err) { 18 | throw new Error(err); 19 | } 20 | t.equal(path.basename(files[0].path), 'what.json'); 21 | })); 22 | }); 23 | 24 | test('simple prefix', function(t) { 25 | t.plan(1); 26 | globSteam 27 | .create('./package.json') 28 | .pipe(streamRename({ 29 | prefix: 'hello-' 30 | })) 31 | .pipe(buffer(function(err, files) { 32 | if (err) { 33 | throw new Error(err); 34 | } 35 | t.equal(path.basename(files[0].path), 'hello-package.json'); 36 | })); 37 | 38 | }); 39 | 40 | test('simple suffix', function(t) { 41 | t.plan(1); 42 | globSteam 43 | .create('./package.json') 44 | .pipe(streamRename({ 45 | suffix: '-ing' 46 | })) 47 | .pipe(buffer(function(err, files) { 48 | if (err) { 49 | throw new Error(err); 50 | } 51 | t.equal(path.basename(files[0].path), 'package-ing.json'); 52 | })); 53 | 54 | }); 55 | 56 | test('simple extension', function(t) { 57 | t.plan(1); 58 | globSteam 59 | .create('./package.json') 60 | .pipe(streamRename({ 61 | extname: '.js' 62 | })) 63 | .pipe(buffer(function(err, files) { 64 | if (err) { 65 | throw new Error(err); 66 | } 67 | t.equal(path.basename(files[0].path), 'package.js'); 68 | })); 69 | 70 | }); -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | 2 | { 3 | "env": { 4 | "node": true, 5 | }, 6 | "rules": { 7 | "block-scoped-var": 2, 8 | "complexity": [1, 6], 9 | "consistent-return": 2, 10 | "curly": 2, 11 | "default-case": 2, 12 | "dot-notation": 2, 13 | "eqeqeq": 2, 14 | "guard-for-in": 2, 15 | "no-alert": 2, 16 | "no-caller": 2, 17 | "no-comma-dangle": 2, 18 | "no-div-regex": 2, 19 | "no-dupe-keys": 2, 20 | "no-else-return": 2, 21 | "no-empty-label": 2, 22 | "no-eq-null": 2, 23 | "no-eval": 2, 24 | "no-extend-native": 2, 25 | "no-extra-bind": 2, 26 | "no-extra-boolean-cast": 2, 27 | "no-fallthrough": 2, 28 | "no-floating-decimal": 2, 29 | "no-implied-eval": 2, 30 | "no-labels": 2, 31 | "no-iterator": 2, 32 | "no-lone-blocks": 2, 33 | "no-loop-func": 2, 34 | "no-multi-str": 2, 35 | "no-native-reassign": 2, 36 | "no-new": 2, 37 | "no-new-func": 2, 38 | "no-new-wrappers": 2, 39 | "no-octal": 2, 40 | "no-octal-escape": 2, 41 | "no-proto": 2, 42 | "no-redeclare": 2, 43 | "no-return-assign": 2, 44 | "no-script-url": 2, 45 | "no-self-compare": 2, 46 | "no-sequences": 2, 47 | "no-unused-expressions": 2, 48 | "no-unused-vars": 1, 49 | "no-unreachable": 2, 50 | "no-void": 2, 51 | "no-warning-comments": 2, 52 | "no-with": 2, 53 | "radix": 2, 54 | "vars-on-top": 2, 55 | "wrap-iife": [2, "inside"], 56 | "yoda": 2, 57 | "quotes": 0, 58 | "eol-last": 0, 59 | "no-extra-strict": 2, 60 | "camelcase": 0, 61 | "consistent-this": [2, "_this"], 62 | "new-cap": 2, 63 | "new-parens": 2, 64 | "func-style": [2, "expression"], 65 | "no-lonely-if": 2, 66 | "no-new-object": 2, 67 | "no-space-before-semi": 2, 68 | "brace-style": [2, "1tbs"], 69 | "no-wrap-func": 2, 70 | "space-after-keywords": [2, "always"], 71 | "use-isnan": 2, 72 | "max-nested-callbacks": [2, 3], 73 | "semi": [2, "always"], 74 | "no-underscore-dangle": 0 75 | } 76 | } --------------------------------------------------------------------------------