Skip to content
This repository has been archived by the owner on Apr 20, 2018. It is now read-only.

Commit

Permalink
changed custom pattern behavior to merge isntead of overwrite
Browse files Browse the repository at this point in the history
  • Loading branch information
timhettler committed Nov 25, 2014
1 parent 3382f00 commit 9897662
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 55 deletions.
22 changes: 11 additions & 11 deletions lib/fileprocessor.js
Expand Up @@ -110,19 +110,19 @@ var defaultBlockReplacements = {
}
};

var FileProcessor = module.exports = function (patterns, finder, logcb, blockReplacements) {
if (!patterns) {
throw new Error('No pattern given');
var FileProcessor = module.exports = function (type, patterns, finder, logcb, blockReplacements) {
if (!type) {
throw new Error('No type given');
}

if (_.isString(patterns)) {
if (!_.contains(_.keys(_defaultPatterns), patterns)) {
throw new Error('Unsupported pattern: ' + patterns);
}
this.patterns = _defaultPatterns[patterns];
} else {
// FIXME: check the pattern format
this.patterns = patterns;
if (!_.isArray(patterns)) {
throw new Error('Patterns must be an array');
}

this.patterns = _defaultPatterns[type] || [];

if (patterns.length) {
this.patterns = this.patterns.concat(patterns);
}

this.log = logcb || function () {};
Expand Down
10 changes: 4 additions & 6 deletions tasks/usemin.js
Expand Up @@ -111,21 +111,19 @@ module.exports = function (grunt) {
var blockReplacements = options.blockReplacements || {};

debug('Looking at %s target', this.target);
var patterns;
var patterns = [];
var type = this.target;

// Check if we have a user defined pattern
if (options.patterns && options.patterns[this.target]) {
debug('Using user defined pattern for %s', this.target);
debug('Adding user defined patterns for %s', this.target);
patterns = options.patterns[this.target];
} else {
debug('Using predefined pattern for %s', this.target);
patterns = options.type;
}

// var locator = options.revmap ? grunt.file.readJSON(options.revmap) : function (p) { return grunt.file.expand({filter: 'isFile'}, p); };
var locator = getLocator(grunt, options);
var revvedfinder = new RevvedFinder(locator);
var handler = new FileProcessor(patterns, revvedfinder, function (msg) {
var handler = new FileProcessor(type, patterns, revvedfinder, function (msg) {
grunt.verbose.writeln(msg);
}, blockReplacements);

Expand Down
72 changes: 35 additions & 37 deletions test/test-fileprocessor.js
Expand Up @@ -5,42 +5,40 @@ var FileProcessor = require('../lib/fileprocessor.js');

describe('FileProcessor', function () {
describe('constructor', function () {
it('should fail if no pattern is furnished', function () {
it('should fail if no type is furnished', function () {
assert.throws(function () {
new FileProcessor();
}, /No pattern given/);
}, /No type given/);
});

it('should accept a pattern name', function () {
var fp = new FileProcessor('html', {});
var fp = new FileProcessor('html', [], {});
assert.ok(fp);
});

it('should access a pattern object', function () {
var foo = {
foo: 'bar'
};
var fp = new FileProcessor(foo, {});
assert.ok(fp);
assert.deepEqual(fp.patterns, foo);
it('should fail if pattern is not an array', function () {
assert.throws(function () {
new FileProcessor('html', {});
}, /Patterns must be an array/);
});

it('should fail if pattern name is not known', function () {
assert.throws(function () {
new FileProcessor('foo');
}, /Unsupported pattern: foo/);
it('should accept a custom pattern', function () {
var foo = ['bar'];
var fp = new FileProcessor('html', foo, {});
assert.ok(fp);
assert.notEqual(fp.patterns.indexOf(foo[0]), -1);
});

it('should check all needed arguments are furnished', function () {
it('should fail if no finder is furnished', function () {
assert.throws(function () {
new FileProcessor('html');
new FileProcessor('html', []);
}, /Missing parameter: finder/);
});
});

describe('replaceBlocks', function () {
it('should replace block with the right expression', function () {
var fp = new FileProcessor('html', {});
var fp = new FileProcessor('html', [], {});
fp.replaceWith = function () {
return 'foo';
};
Expand All @@ -57,7 +55,7 @@ describe('FileProcessor', function () {

describe('replaceWith', function () {
it('should replace css blocks with a link to a stylesheet', function () {
var fp = new FileProcessor('html', {});
var fp = new FileProcessor('html', [], {});
var block = {
dest: 'foo.css',
type: 'css',
Expand All @@ -70,7 +68,7 @@ describe('FileProcessor', function () {
});

it('should remove css blocks which have no stylesheets linked in them', function () {
var fp = new FileProcessor('html', {});
var fp = new FileProcessor('html', [], {});
var block = {
dest: 'foo.css',
type: 'css',
Expand All @@ -83,7 +81,7 @@ describe('FileProcessor', function () {
});

it('should replace js blocks with a link to a javascript file', function () {
var fp = new FileProcessor('html', {});
var fp = new FileProcessor('html', [], {});
var block = {
dest: 'foo.js',
type: 'js',
Expand All @@ -96,7 +94,7 @@ describe('FileProcessor', function () {
});

it('should remove js blocks which have no javascripts linked in the block', function () {
var fp = new FileProcessor('html', {});
var fp = new FileProcessor('html', [], {});
var block = {
dest: 'foo.js',
type: 'js',
Expand All @@ -114,7 +112,7 @@ describe('FileProcessor', function () {
return 'custom replacement for ' + block.dest;
}
};
var fp = new FileProcessor('html', {}, function () {}, blockReplacements);
var fp = new FileProcessor('html', [], {}, function () {}, blockReplacements);
var block = {
dest: 'foo.css',
type: 'less',
Expand All @@ -127,7 +125,7 @@ describe('FileProcessor', function () {
});

it('should preserve defer attribute (JS)', function () {
var fp = new FileProcessor('html', {});
var fp = new FileProcessor('html', [], {});
var block = {
dest: 'foo.js',
type: 'js',
Expand All @@ -141,7 +139,7 @@ describe('FileProcessor', function () {
});

it('should preserve async attribute (JS)', function () {
var fp = new FileProcessor('html', {});
var fp = new FileProcessor('html', [], {});
var block = {
dest: 'foo.js',
type: 'js',
Expand All @@ -155,7 +153,7 @@ describe('FileProcessor', function () {
});

it('should preserve media attribute', function () {
var fp = new FileProcessor('html', {});
var fp = new FileProcessor('html', [], {});
var block = {
dest: 'foo.css',
type: 'css',
Expand All @@ -169,7 +167,7 @@ describe('FileProcessor', function () {
});

it('should preserve IE conditionals for js blocks', function () {
var fp = new FileProcessor('html', {});
var fp = new FileProcessor('html', [], {});
var block = {
dest: 'foo.js',
type: 'js',
Expand All @@ -184,7 +182,7 @@ describe('FileProcessor', function () {
});

it('should preserve IE conditionals for css blocks', function () {
var fp = new FileProcessor('html', {});
var fp = new FileProcessor('html', [], {});
var block = {
dest: 'foo.css',
type: 'css',
Expand All @@ -210,7 +208,7 @@ describe('FileProcessor', function () {
return 'toto';
}
};
var fp = new FileProcessor(pattern, finder);
var fp = new FileProcessor('html', pattern, finder);
var content = 'bar\nfoo12345\nfoo8979\nbaz\n';
var result = fp.replaceWithRevved(content, ['']);

Expand Down Expand Up @@ -242,7 +240,7 @@ describe('FileProcessor', function () {
var revvedfinder = helpers.makeFinder(filemapping);

beforeEach(function () {
fp = new FileProcessor('html', revvedfinder);
fp = new FileProcessor('html', [], revvedfinder);

});

Expand Down Expand Up @@ -276,7 +274,7 @@ describe('FileProcessor', function () {
var fp;

beforeEach(function () {
fp = new FileProcessor('html', revvedfinder);
fp = new FileProcessor('html', [], revvedfinder);
});

it('should replace file referenced from root', function () {
Expand Down Expand Up @@ -311,7 +309,7 @@ describe('FileProcessor', function () {
var fp;

beforeEach(function () {
fp = new FileProcessor('html', revvedfinder);
fp = new FileProcessor('html', [], revvedfinder);
});

it('should replace script source with revved version', function () {
Expand Down Expand Up @@ -481,7 +479,7 @@ describe('FileProcessor', function () {
var revvedfinder = helpers.makeFinder(filemapping);

beforeEach(function () {
cp = new FileProcessor('css', revvedfinder);
cp = new FileProcessor('css', [], revvedfinder);
});

it('should replace with revved files when found', function () {
Expand Down Expand Up @@ -524,7 +522,7 @@ describe('FileProcessor', function () {
var revvedfinder = helpers.makeFinder(filemapping);

beforeEach(function () {
cp = new FileProcessor('css', revvedfinder);
cp = new FileProcessor('css', [], revvedfinder);
});

it('should replace with revved files when found', function () {
Expand Down Expand Up @@ -557,7 +555,7 @@ describe('FileProcessor', function () {
var revvedfinder = helpers.makeFinder(filemapping);

beforeEach(function () {
cp = new FileProcessor('css', revvedfinder);
cp = new FileProcessor('css', [], revvedfinder);
});

it('should replace but ignore querystrings on revved files when found', function () {
Expand All @@ -583,7 +581,7 @@ describe('FileProcessor', function () {
var revvedfinder = helpers.makeFinder(filemapping);

beforeEach(function () {
cp = new FileProcessor('css', revvedfinder);
cp = new FileProcessor('css', [], revvedfinder);
});

it('should replace but ignore querystrings on revved files when found', function () {
Expand Down Expand Up @@ -618,7 +616,7 @@ describe('FileProcessor', function () {
var revvedfinder = helpers.makeFinder(filemapping);

beforeEach(function () {
cp = new FileProcessor('json', revvedfinder);
cp = new FileProcessor('json', [], revvedfinder);
});

it('should replace with revved files when found', function () {
Expand Down Expand Up @@ -662,7 +660,7 @@ describe('FileProcessor', function () {
var revvedfinder = helpers.makeFinder(filemapping);

beforeEach(function () {
cp = new FileProcessor('json', revvedfinder);
cp = new FileProcessor('json', [], revvedfinder);
});

it('should replace with revved files when found', function () {
Expand Down
1 change: 0 additions & 1 deletion test/test-usemin.js
Expand Up @@ -277,7 +277,6 @@ describe('usemin', function () {
grunt.file.copy(path.join(__dirname, 'fixtures/misc.js'), 'misc.js');
grunt.task.run('usemin');
grunt.task.start();

var changed = grunt.file.read('misc.js');

// Check replace has performed its duty
Expand Down

0 comments on commit 9897662

Please sign in to comment.