Skip to content

Commit

Permalink
feat(multi-md5): Allow objects in match options to allow multiple md5…
Browse files Browse the repository at this point in the history
…'s in <pattern>:<file> format - fixes #10
  • Loading branch information
shakyShane committed Aug 5, 2015
1 parent f59b03a commit 89be969
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 38 deletions.
23 changes: 11 additions & 12 deletions Gruntfile.js
Expand Up @@ -44,26 +44,27 @@ module.exports = function (grunt) {
cachebreaker: {
js: {
options: {
match: ['combined.min.js', 'app.js'],
match: ['script.js', 'app.js'],
replacement: function () {
return "123456";
}
},
files: {
'<%= js_dest %>': '<%= js_src %>'
'test/fixtures/query.out.html': ["test/fixtures/query.html"]
}
},
jsmd52: {
md5: {
options: {
match: ['dummy.*.js', 'combined.min.*.js'],
replacement: "md5",
position: "overwrite",
src: {
path: "./LICENSE-MIT"
}
match: [
{
'script.js': 'test/fixtures/js/script.js',
'app.js': 'test/fixtures/js/app.js'
}
]
},
files: {
'tmp/jsmd5': 'test/fixtures/jsmd5'
'test/fixtures/md5.out.html': ["test/fixtures/query.html"]
}
}
},
Expand All @@ -87,9 +88,7 @@ module.exports = function (grunt) {
// Whenever the "test" task is run, first clean the "tmp" dir, then run this
// plugin's task(s), then test the result.
grunt.registerTask('test', [
'clean',
'cachebreaker:js',
'cachebreaker:jsmd52',
'cachebreaker',
'nodeunit:tests'
]);

Expand Down
8 changes: 5 additions & 3 deletions package.json
Expand Up @@ -28,10 +28,11 @@
"test": "grunt"
},
"devDependencies": {
"grunt-contrib-jshint": "~0.1.1",
"grunt": "~0.4.1",
"grunt-contrib-clean": "~0.4.0",
"grunt-contrib-jshint": "~0.1.1",
"grunt-contrib-nodeunit": "~0.1.2",
"grunt": "~0.4.1"
"lodash": "^3.10.1"
},
"keywords": [
"gruntplugin",
Expand All @@ -41,6 +42,7 @@
"break"
],
"dependencies": {
"cache-breaker": "0.0.4"
"cache-breaker": "0.0.4",
"lodash.clonedeep": "^3.0.2"
}
}
55 changes: 36 additions & 19 deletions tasks/cache_breaker.js
@@ -1,4 +1,5 @@
"use strict";
var _ = require("lodash");

module.exports = function (grunt) {

Expand All @@ -18,25 +19,41 @@ module.exports = function (grunt) {
return grunt.fail.warn("No source files were found.");
}

f.src
.forEach(function (filepath) {

var dest = filepath;

if (f.dest && f.dest !== "src") {
dest = f.dest;
}

var input = grunt.file.read(filepath);
var broken = cb.breakCache(input, options.match, options);

if (broken.length) {
grunt.log.ok("Cache broken in: " + filepath.cyan);
grunt.file.write(dest, broken);
} else {
return grunt.fail.warn("No changes were made.");
}
});
f.src.forEach(function (filepath) {

var dest = filepath;

if (f.dest && f.dest !== "src") {
dest = f.dest;
}

var input = grunt.file.read(filepath);

if (options.replacement === "md5") {
options.match.forEach(function (item) {
if (typeof item === "string") {
input = cb.breakCache(input, item, options);
} else {
var clone = _.cloneDeep(options);
Object.keys(item).forEach(function (key) {
clone.src = {
path: item[key]
};
input = cb.breakCache(input, key, clone);
});
}
});
} else {
input = cb.breakCache(input, options.match, options);
}

if (input.length) {
grunt.log.ok("Cache broken in: " + filepath.cyan);
grunt.file.write(dest, input);
} else {
return grunt.fail.warn("No changes were made.");
}
});
});
});
};
10 changes: 6 additions & 4 deletions test/cache_breaker_test.js
Expand Up @@ -32,16 +32,18 @@ exports.cache_breaker = {
},
task: function (test) {

var types = ['js', 'jsmd5'];
var types = ['query', 'md5'];

test.expect(types.length);

types.forEach(function (item) {

var actual = grunt.file.read('tmp/' + item);
var expected = grunt.file.read('test/expected/' + item);
test.deepEqual(actual, expected);
var out = 'test/fixtures/' + item + '.out.html';
var actual = grunt.file.read(out);
var expected = grunt.file.read('test/fixtures/' + item + '.expected.html');

grunt.file.delete(out);
test.deepEqual(actual, expected);
});

test.done();
Expand Down

0 comments on commit 89be969

Please sign in to comment.