├── .gitignore ├── History.md ├── LICENSE ├── Readme.md ├── index.js ├── package.json └── test └── gulp-insert-test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | -------------------------------------------------------------------------------- /History.md: -------------------------------------------------------------------------------- 1 | 2 | 0.5.0 / 2015-08-05 3 | ================== 4 | 5 | * added file object as second parameter to transform hook 6 | 7 | 0.4.0 / 2014-06-28 8 | ================== 9 | 10 | * Add function of file as arguments of appent/prepend/wrap 11 | 12 | 0.3.0 / 2014-04-06 13 | ================== 14 | 15 | * Add support for streams (@nfroidure) 16 | * Add support for passing null files through 17 | 18 | 0.2.0 / 2014-02-02 19 | ================== 20 | 21 | * Added wrap and transform methods 22 | 23 | 0.1.0 / 2014-01-17 24 | ================== 25 | 26 | * Initial Release 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2020 Ryan Schmukler 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # gulp-insert 2 | 3 | String manipulation library for gulp 4 | 5 | ## Usage 6 | 7 | ``` 8 | npm install gulp-insert 9 | ``` 10 | 11 | ```js 12 | var insert = require('gulp-insert'); 13 | ``` 14 | 15 | ## Append 16 | 17 | Appends a string onto the contents. 18 | 19 | ```js 20 | .pipe(insert.append('world')); // Appends 'world' to the contents of every file 21 | ``` 22 | 23 | ## Prepend 24 | 25 | Prepends a string onto the contents. 26 | 27 | ```js 28 | .pipe(insert.prepend('Hello')); // Prepends 'Hello' to the contents of every file 29 | ``` 30 | ## Wrap 31 | 32 | Wraps the contents with two strings. 33 | 34 | ```js 35 | .pipe(insert.wrap('Hello', 'World')); // prepends 'hello' and appends 'world' to the contents 36 | ``` 37 | 38 | ## Transform 39 | 40 | Calls a function with the contents of the file. 41 | 42 | ```js 43 | .pipe(insert.transform(function(contents, file) { 44 | return contents.toUpperCase(); 45 | })); 46 | ``` 47 | 48 | Transform has access to the underlying vinyl file. The following code adds a '//' comment with the full file name before the actual content. 49 | 50 | ```js 51 | .pipe(insert.transform(function(contents, file) { 52 | 53 | var comment = '// local file: ' + file.path + '\n'; 54 | return comment + contents; 55 | })); 56 | ``` 57 | 58 | See https://github.com/wearefractal/vinyl for docmentation on the 'file' parameter. 59 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var Stream = require('readable-stream'); 2 | var StreamQueue = require('streamqueue'); 3 | 4 | // Helper 5 | function getStreamFromBuffer(string) { 6 | var stream = new Stream.Readable(); 7 | stream._read = function() { 8 | stream.push(new Buffer(string)); 9 | stream._read = stream.push.bind(stream, null); 10 | }; 11 | return stream; 12 | } 13 | 14 | // Get string to insert 15 | function getInsertString(arg, file) { 16 | if(typeof arg === 'function') { 17 | return arg(file); 18 | } else { 19 | return arg; 20 | } 21 | } 22 | 23 | exports.prepend = function(prepend) { 24 | var stream = new Stream.Transform({objectMode: true}); 25 | 26 | stream._transform = function(file, unused, cb) { 27 | if(file.isNull()) { 28 | return cb(null, file); 29 | } 30 | var prependedBuffer = new Buffer(getInsertString(prepend, file)); 31 | if(file.isStream()) { 32 | file.contents = new StreamQueue( 33 | getStreamFromBuffer(prependedBuffer), 34 | file.contents 35 | ); 36 | return cb(null, file); 37 | } 38 | file.contents = Buffer.concat([prependedBuffer, file.contents], 39 | prependedBuffer.length + file.contents.length); 40 | cb(null, file); 41 | }; 42 | 43 | return stream; 44 | }; 45 | 46 | exports.append = function(append) { 47 | var stream = new Stream.Transform({objectMode: true}); 48 | 49 | stream._transform = function(file, unused, cb) { 50 | if(file.isNull()) { 51 | return cb(null, file); 52 | } 53 | var appendedBuffer = new Buffer(getInsertString(append, file)); 54 | if(file.isStream()) { 55 | file.contents = new StreamQueue( 56 | file.contents, 57 | getStreamFromBuffer(appendedBuffer) 58 | ); 59 | return cb(null, file); 60 | } 61 | file.contents = Buffer.concat([file.contents, appendedBuffer], 62 | appendedBuffer.length + file.contents.length); 63 | cb(null, file); 64 | }; 65 | 66 | return stream; 67 | }; 68 | 69 | exports.wrap = function(begin, end) { 70 | var stream = new Stream.Transform({objectMode: true}); 71 | 72 | stream._transform = function(file, unused, cb) { 73 | if(file.isNull()) { 74 | return cb(null, file); 75 | } 76 | var prependedBuffer = new Buffer(getInsertString(begin, file)); 77 | var appendedBuffer = new Buffer(getInsertString(end, file)); 78 | if(file.isStream()) { 79 | file.contents = new StreamQueue( 80 | getStreamFromBuffer(prependedBuffer), 81 | file.contents, 82 | getStreamFromBuffer(appendedBuffer) 83 | ); 84 | return cb(null, file); 85 | } 86 | file.contents = Buffer.concat([prependedBuffer, file.contents, appendedBuffer], 87 | appendedBuffer.length + file.contents.length + prependedBuffer.length); 88 | cb(null, file); 89 | }; 90 | 91 | return stream; 92 | }; 93 | 94 | exports.transform = function(fn) { 95 | var stream = new Stream.Transform({objectMode: true}); 96 | 97 | stream._transform = function(file, unused, cb) { 98 | if(file.isNull()) { 99 | return cb(null, file); 100 | } 101 | if(file.isStream()) { 102 | file.contents = file.contents.pipe(new Stream.Transform()); 103 | file.contents._transform = function(chunk, encoding, cb) { 104 | cb(null, new Buffer(fn(chunk.toString(), file))) 105 | }; 106 | return cb(null, file); 107 | } 108 | file.contents = new Buffer(fn(file.contents.toString(), file)); 109 | cb(null, file); 110 | }; 111 | 112 | return stream; 113 | }; 114 | 115 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gulp-insert", 3 | "version": "0.5.0", 4 | "description": "Append or Prepend a string with gulp", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "mocha test/*.js" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git://github.com/rschmukler/gulp-insert" 12 | }, 13 | "keywords": [ 14 | "gulp", 15 | "gulpplugin", 16 | "append", 17 | "insert", 18 | "prepend" 19 | ], 20 | "author": "Ryan Schmukler (http://slingingcode.com/)", 21 | "license": "MIT", 22 | "homepage": "https://github.com/rschmukler/gulp-insert/", 23 | "bugs": { 24 | "url": "https://github.com/rschmukler/gulp-insert/issues" 25 | }, 26 | "devDependencies": { 27 | "mocha": "~1.17.0", 28 | "gulp-util": "~2.2.12", 29 | "chai": "^1.9.1" 30 | }, 31 | "dependencies": { 32 | "readable-stream": "^1.0.26-4", 33 | "streamqueue": "0.0.6" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /test/gulp-insert-test.js: -------------------------------------------------------------------------------- 1 | var insert = require('../'), 2 | expect = require('chai').expect, 3 | File = require('gulp-util').File 4 | Stream = require('readable-stream'); 5 | 6 | // Helper 7 | function getStreamFromBuffer(string) { 8 | var stream = new Stream.Readable(); 9 | stream._read = function() { 10 | stream.push(new Buffer(string)); 11 | stream._read = stream.push.bind(stream, null); 12 | }; 13 | return stream; 14 | } 15 | 16 | describe('Append', function() { 17 | it('let pass null files through', function(done) { 18 | var stream = insert.append(' world'); 19 | 20 | var fakeFile = new File({ 21 | cwd: __dirname, 22 | base: __dirname + 'test', 23 | path: __dirname + 'test/file.js', 24 | contents: null 25 | }); 26 | 27 | stream.on('data', function(file) { 28 | expect(file.contents).to.be.equal(null); 29 | done(); 30 | }); 31 | 32 | stream.write(fakeFile); 33 | stream.end(); 34 | }); 35 | 36 | it('appends the string onto the file in buffer mode', function(done) { 37 | var stream = insert.append(' world'); 38 | 39 | var fakeFile = new File({ 40 | cwd: __dirname, 41 | base: __dirname + 'test', 42 | path: __dirname + 'test/file.js', 43 | contents: new Buffer('Hello') 44 | }); 45 | 46 | stream.on('data', function(file) { 47 | expect(file.contents.toString()).to.be.equal('Hello world'); 48 | done(); 49 | }); 50 | 51 | stream.write(fakeFile); 52 | stream.end(); 53 | }); 54 | 55 | it('appends the file.path onto the file in buffer mode', function(done) { 56 | var stream = insert.append(function(file) { 57 | return file.path; 58 | }); 59 | 60 | var fakeFile = new File({ 61 | cwd: __dirname, 62 | base: __dirname + 'test', 63 | path: __dirname + 'test/file.js', 64 | contents: new Buffer('Hello') 65 | }); 66 | 67 | stream.on('data', function(file) { 68 | expect(file.contents.toString()).to.be.equal('Hello' + file.path); 69 | done(); 70 | }); 71 | 72 | stream.write(fakeFile); 73 | stream.end(); 74 | }); 75 | 76 | it('appends the string onto the file in stream mode', function(done) { 77 | var stream = insert.append(' world'); 78 | 79 | var fakeFile = new File({ 80 | cwd: __dirname, 81 | base: __dirname + 'test', 82 | path: __dirname + 'test/file.js', 83 | contents: getStreamFromBuffer(new Buffer('Hello')) 84 | }); 85 | 86 | stream.on('data', function(file) { 87 | var buffers = []; 88 | file.contents.on('data', function(buf) { 89 | buffers.push(buf); 90 | }); 91 | file.contents.on('end', function() { 92 | expect(Buffer.concat(buffers).toString()).to.be.equal('Hello world'); 93 | done(); 94 | }); 95 | }); 96 | 97 | stream.write(fakeFile); 98 | stream.end(); 99 | }); 100 | 101 | }); 102 | 103 | describe('Prepend', function() { 104 | it('let pass null files through', function(done) { 105 | var stream = insert.prepend('Hello'); 106 | 107 | var fakeFile = new File({ 108 | cwd: __dirname, 109 | base: __dirname + 'test', 110 | path: __dirname + 'test/file.js', 111 | contents: null 112 | }); 113 | 114 | stream.on('data', function(file) { 115 | expect(file.contents).to.be.equal(null); 116 | done(); 117 | }); 118 | 119 | stream.write(fakeFile); 120 | stream.end(); 121 | }); 122 | 123 | it('prepends the string onto the file in buffer mode', function(done) { 124 | var stream = insert.prepend('Hello'); 125 | 126 | var fakeFile = new File({ 127 | cwd: __dirname, 128 | base: __dirname + 'test', 129 | path: __dirname + 'test/file.js', 130 | contents: new Buffer(' world') 131 | }); 132 | 133 | stream.on('data', function(file) { 134 | expect(file.contents.toString()).to.be.equal('Hello world'); 135 | done(); 136 | }); 137 | 138 | stream.write(fakeFile); 139 | stream.end(); 140 | }); 141 | 142 | it('prepends the file.path onto the file in buffer mode', function(done) { 143 | var stream = insert.prepend(function(file) { 144 | return file.path; 145 | }); 146 | 147 | var fakeFile = new File({ 148 | cwd: __dirname, 149 | base: __dirname + 'test', 150 | path: __dirname + 'test/file.js', 151 | contents: new Buffer(' world') 152 | }); 153 | 154 | stream.on('data', function(file) { 155 | expect(file.contents.toString()).to.be.equal(file.path + ' world'); 156 | done(); 157 | }); 158 | 159 | stream.write(fakeFile); 160 | stream.end(); 161 | }); 162 | 163 | it('prepends the string onto the file in stream mode', function(done) { 164 | var stream = insert.prepend('Hello'); 165 | 166 | var fakeFile = new File({ 167 | cwd: __dirname, 168 | base: __dirname + 'test', 169 | path: __dirname + 'test/file.js', 170 | contents: getStreamFromBuffer(new Buffer(' world')) 171 | }); 172 | 173 | stream.on('data', function(file) { 174 | var buffers = []; 175 | file.contents.on('data', function(buf) { 176 | buffers.push(buf); 177 | }); 178 | file.contents.on('end', function() { 179 | expect(Buffer.concat(buffers).toString()).to.be.equal('Hello world'); 180 | done(); 181 | }); 182 | }); 183 | 184 | stream.write(fakeFile); 185 | stream.end(); 186 | }); 187 | }); 188 | 189 | describe('Wrap', function() { 190 | it('let pass null files through', function(done) { 191 | var stream = insert.wrap('Hello ', '!'); 192 | 193 | var fakeFile = new File({ 194 | cwd: __dirname, 195 | base: __dirname + 'test', 196 | path: __dirname + 'test/file.js', 197 | contents: null 198 | }); 199 | 200 | stream.on('data', function(file) { 201 | expect(file.contents).to.be.equal(null); 202 | done(); 203 | }); 204 | 205 | stream.write(fakeFile); 206 | stream.end(); 207 | }); 208 | 209 | it('prepends the first argument and appends the second argument in buffer mode', function(done) { 210 | var stream = insert.wrap('Hello ', '!'); 211 | 212 | var fakeFile = new File({ 213 | cwd: __dirname, 214 | base: __dirname + 'test', 215 | path: __dirname + 'test/file.js', 216 | contents: new Buffer('world') 217 | }); 218 | 219 | stream.on('data', function(file) { 220 | expect(file.contents.toString()).to.be.equal('Hello world!'); 221 | done(); 222 | }); 223 | 224 | stream.write(fakeFile); 225 | stream.end(); 226 | }); 227 | 228 | it('prepends file.base and file.path in buffer mode', function(done) { 229 | var stream = insert.wrap(function(file) { 230 | return file.base; 231 | }, function(file) { 232 | return file.path; 233 | }); 234 | 235 | var fakeFile = new File({ 236 | cwd: __dirname, 237 | base: __dirname + 'test', 238 | path: __dirname + 'test/file.js', 239 | contents: new Buffer('world') 240 | }); 241 | 242 | stream.on('data', function(file) { 243 | expect(file.contents.toString()).to.be.equal(file.base + 'world' + file.path); 244 | done(); 245 | }); 246 | 247 | stream.write(fakeFile); 248 | stream.end(); 249 | }); 250 | 251 | it('prepends the first argument and appends the second argument in stream mode', function(done) { 252 | var stream = insert.wrap('Hello ', '!'); 253 | 254 | var fakeFile = new File({ 255 | cwd: __dirname, 256 | base: __dirname + 'test', 257 | path: __dirname + 'test/file.js', 258 | contents: getStreamFromBuffer(new Buffer('world')) 259 | }); 260 | 261 | stream.on('data', function(file) { 262 | var buffers = []; 263 | file.contents.on('data', function(buf) { 264 | buffers.push(buf); 265 | }); 266 | file.contents.on('end', function() { 267 | expect(Buffer.concat(buffers).toString()).to.be.equal('Hello world!'); 268 | done(); 269 | }); 270 | }); 271 | 272 | stream.write(fakeFile); 273 | stream.end(); 274 | }); 275 | }); 276 | 277 | describe('Transform', function() { 278 | it('let pass null files through', function(done) { 279 | var stream = insert.transform(function(data) { 280 | return data.toUpperCase(); 281 | }); 282 | 283 | var fakeFile = new File({ 284 | cwd: __dirname, 285 | base: __dirname + 'test', 286 | path: __dirname + 'test/file.js', 287 | contents: null 288 | }); 289 | 290 | stream.on('data', function(file) { 291 | expect(file.contents).to.be.equal(null); 292 | done(); 293 | }); 294 | 295 | stream.write(fakeFile); 296 | stream.end(); 297 | 298 | }); 299 | 300 | it('applys the function to the string in buffer mode', function(done) { 301 | var stream = insert.transform(function(data) { 302 | return data.toUpperCase(); 303 | }); 304 | 305 | var fakeFile = new File({ 306 | cwd: __dirname, 307 | base: __dirname + 'test', 308 | path: __dirname + 'test/file.js', 309 | contents: new Buffer('hello world') 310 | }); 311 | 312 | stream.on('data', function(file) { 313 | expect(file.contents.toString()).to.be.equal('HELLO WORLD'); 314 | done(); 315 | }); 316 | 317 | stream.write(fakeFile); 318 | stream.end(); 319 | 320 | }); 321 | 322 | it('applys the function to the string in stream mode', function(done) { 323 | var stream = insert.transform(function(data) { 324 | return data.toUpperCase(); 325 | }); 326 | 327 | var fakeFile = new File({ 328 | cwd: __dirname, 329 | base: __dirname + 'test', 330 | path: __dirname + 'test/file.js', 331 | contents: getStreamFromBuffer(new Buffer('hello world')) 332 | }); 333 | 334 | stream.on('data', function(file) { 335 | var buffers = []; 336 | file.contents.on('data', function(buf) { 337 | buffers.push(buf); 338 | }); 339 | file.contents.on('end', function() { 340 | expect(Buffer.concat(buffers).toString()).to.be.equal('HELLO WORLD'); 341 | done(); 342 | }); 343 | }); 344 | 345 | stream.write(fakeFile); 346 | stream.end(); 347 | }); 348 | 349 | it('passes the vinyl file object as second parameter in buffer mode', function(done) { 350 | 351 | var stream = insert.transform(function(data, file) { 352 | var output = []; 353 | output.push(file.cwd); 354 | output.push(file.base); 355 | output.push(file.path); 356 | output.push(data); 357 | 358 | return output.join('\n'); 359 | }); 360 | 361 | var fakeFile = new File({ 362 | cwd: __dirname, 363 | base: __dirname + 'test', 364 | path: __dirname + 'test/file.js', 365 | contents: new Buffer('hello world') 366 | }); 367 | 368 | stream.on('data', function(file) { 369 | 370 | var contents = file.contents.toString().split('\n'); 371 | 372 | expect(contents.length).to.be.equal(4); 373 | expect(contents[0]).to.be.equal(__dirname); 374 | expect(contents[1]).to.be.equal(__dirname + 'test'); 375 | expect(contents[2]).to.be.equal(__dirname + 'test/file.js'); 376 | expect(contents[3]).to.be.equal('hello world'); 377 | done(); 378 | }); 379 | 380 | stream.write(fakeFile); 381 | stream.end(); 382 | }); 383 | 384 | it('passes the vinyl file object as second parameter in stream mode', function(done) { 385 | 386 | var stream = insert.transform(function(data, file) { 387 | var output = []; 388 | output.push(file.cwd); 389 | output.push(file.base); 390 | output.push(file.path); 391 | output.push(data); 392 | 393 | return output.join('\n'); 394 | }); 395 | 396 | var fakeFile = new File({ 397 | cwd: __dirname, 398 | base: __dirname + 'test', 399 | path: __dirname + 'test/file.js', 400 | contents: getStreamFromBuffer(new Buffer('hello world')) 401 | }); 402 | 403 | stream.on('data', function(file) { 404 | var buffers = []; 405 | file.contents.on('data', function(buf) { 406 | buffers.push(buf); 407 | }); 408 | file.contents.on('end', function() { 409 | 410 | var contents = Buffer.concat(buffers).toString().split('\n'); 411 | 412 | expect(contents.length).to.be.equal(4); 413 | expect(contents[0]).to.be.equal(__dirname); 414 | expect(contents[1]).to.be.equal(__dirname + 'test'); 415 | expect(contents[2]).to.be.equal(__dirname + 'test/file.js'); 416 | expect(contents[3]).to.be.equal('hello world'); 417 | done(); 418 | }); 419 | }); 420 | 421 | stream.write(fakeFile); 422 | stream.end(); 423 | }); 424 | }); 425 | --------------------------------------------------------------------------------