Code coverage report for gulp-version-filename/index.js

Statements: 95.83% (23 / 24)      Branches: 90% (9 / 10)      Functions: 100% (2 / 2)      Lines: 95.65% (22 / 23)      Ignored: none     

All files » gulp-version-filename/ » index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51    1 1   1   4   4   4 4 4 4 4     4       4   2   1       2 2   2       2 2     2       2     4    
'use strict';
 
var Stream = require('stream');
var Path = require('path');
 
module.exports = function(options) {
 
  var stream = new Stream.Transform({ objectMode: true });
 
  options = options || {};
 
  stream._transform = function (file, filetype, callback) {
    var contents = file.contents.toString();
    var version;
    var versionRegEx;
    var components;
 
    // Load up option parameters
    var key = options.key || 'version';
 
    // indexOf is faster than running regex. As we are dealing with files
    // of an unknown size, check if the key exists first
    if (contents.indexOf('@' + key) < 0) {
      // If user wants to ignore files without the key, don't make a fuss
      if (options.silent) return callback(null, file);
      // Otherwise, raise hell
      return stream.emit('error', new Error('Missing version key @' + key + ' in ' + file.relative));
    }
 
    // The first result is the entire match, the second is only the version string
    versionRegEx = new RegExp('@' + key + ' ([^ ]*)', 'i')
    version = contents.match(versionRegEx)[1];
    // Break up the file name to splice in our version
    components = file.relative.split('.');
 
    // File extension should be the final component, add the version to the
    // segment before it
    components[components.length - 2] += '-' + version;
    file.path = Path.join(file.base, components.join('.'));
 
    // Rename sourcemap if present
    Iif (file.sourceMap) {
      file.sourceMap.file = file.relative;
    }
 
    callback(null, file);
  };
 
  return stream;
}