Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesknelson committed Apr 9, 2014
1 parent f89438b commit 6273819
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 24 deletions.
25 changes: 1 addition & 24 deletions .gitignore
@@ -1,25 +1,2 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# Deployed apps should consider commenting this line out:
# see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git
node_modules
npm-debug.log
3 changes: 3 additions & 0 deletions .travis.yml
@@ -0,0 +1,3 @@
language: node_js
node_js:
- '0.10'
60 changes: 60 additions & 0 deletions index.js
@@ -0,0 +1,60 @@
'use strict';
var crypto = require('crypto');
var path = require('path');
var gutil = require('gulp-util');
var through = require('through2');

function relPath(base, filePath) {
var newPath = filePath.replace(base, '');
if (filePath !== newPath && newPath[0] === path.sep) {
return newPath.substr(1);
} else {
return newPath;
}
}

var plugin = function () {
var renames = {};
var cache = [];

return through.obj(function (file, enc, cb) {
if (file.isNull()) {
this.push(file);
return cb();
}

if (file.isStream()) {
this.emit('error', new gutil.PluginError('gulp-rev-replace', 'Streaming not supported'));
return cb();
}

if (file.revOrigPath) {
renames[relPath(file.revOrigBase, file.revOrigPath)] = relPath(file.base, file.path);

// Assume renamed files don't need any replacing and pass them through
// (we don't want to cache anything huge)
this.push(file);
cb();
} else {
cache.push(file);
cb();
}
}, function(cb) {
// Once we have a full list of renames, search/replace in the non-renamed
// files and push them through.
var file;
for (var i=0, ii=cache.length; i!=ii; i++) {
file = cache[i];
for (path in renames) {
if (renames.hasOwnProperty(path)) {
file.contents = new Buffer(file.contents.toString().replace(path, renames[path]));
}
}
this.push(file);
}

cb();
});
};

module.exports = plugin;
36 changes: 36 additions & 0 deletions package.json
@@ -0,0 +1,36 @@
{
"name": "gulp-rev-replace",
"version": "0.0.1",
"description": "Rewrite occurences of filenames which have been renamed by gulp-rev",
"main": "index.js",
"repository": "jamesknelson/gulp-rev-rename",
"scripts": {
"test": "mocha"
},
"keywords": [
"gulpplugin",
"rev",
"revision",
"version",
"replace",
"asset"
],
"author": {
"name": "James K Nelson",
"email": "james@numbattech.com",
"url": "http://jamesknelson.com"
},
"engines": {
"node": ">=0.10.0"
},
"license": "MIT",
"dependencies": {
"through2": "~0.4.1",
"gulp-util": "~2.2.14"
},
"devDependencies": {
"mocha": "~1.18.2",
"gulp-rev": "~0.3.2",
"gulp-filter": "~0.4.0"
}
}
50 changes: 50 additions & 0 deletions test.js
@@ -0,0 +1,50 @@
'use strict';
var assert = require('assert');
var gutil = require('gulp-util');
var filter = require('gulp-filter');
var rev = require('gulp-rev');
var revReplace = require('./index');
var path = require('path');

it('should replace filenames of only reved files', function (cb) {
var cssFilter = filter("**/*.css");

var stream = cssFilter
.pipe(rev())
.pipe(cssFilter.restore())
.pipe(revReplace());

var fileCount = 0;
var unreplacedPattern = /style\.css/;
stream.on('data', function(file) {
var contents = file.contents.toString();

if (file.path.substr(-4) == 'html') {
assert(
!unreplacedPattern.test(contents),
"The renamed file's name should be replaced"
);
} else if (file.path.substr(-3) == 'css') {
assert(
unreplacedPattern.test(contents),
"The renamed file should not be modified"
);
}

fileCount++;
});
stream.on('end', function() {
assert.equal(fileCount, 2, "Only two files should pass through the stream");
cb();
});

cssFilter.write(new gutil.File({
path: 'css/style.css',
contents: new Buffer('/* filename: /css/style.css */ body { color: red; } ')
}));
cssFilter.write(new gutil.File({
path: 'index.html',
contents: new Buffer('<html><head><link rel="stylesheet" href="/css/style.css" /></head><body></body></html>')
}));
cssFilter.end();
});

0 comments on commit 6273819

Please sign in to comment.