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

Commit

Permalink
Merge pull request #382 from boushley/skip-duplicates-289
Browse files Browse the repository at this point in the history
Skip duplicate configurations.
  • Loading branch information
sindresorhus committed Jun 27, 2014
2 parents 2a6b574 + 65b28d5 commit 708ffca
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 1 deletion.
25 changes: 24 additions & 1 deletion lib/configwriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ var ConfigWriter = module.exports = function (flow, dirs) {
this.staging = dirs.staging;
this.steps = {};
this.postprocessors = [];
this.destinations = {};

// We need to create all the needed config writers, given them their output directory
// E.g, if we do have the flow concat | uglifyjs, the output dir will be .tmp/concat and dist
Expand Down Expand Up @@ -158,6 +159,9 @@ ConfigWriter.prototype.process = function (file, config) {
}

self.forEachStep(block.type, function (writer, last) {
var blockConfig;
var fileSet;
var dest;

// If this is the last writer of the pipe, we need to output
// in the destination directory
Expand All @@ -167,7 +171,26 @@ ConfigWriter.prototype.process = function (file, config) {
config[writer.name].generated = config[writer.name].generated || {};
context.options = config[writer.name];
// config[writer.name].generated = _.extend(config[writer.name].generated, writer.createConfig(context, block));
config[writer.name].generated = deepMerge(config[writer.name].generated, writer.createConfig(context, block));
blockConfig = writer.createConfig(context, block);
if (blockConfig.files) {
fileSet = blockConfig.files;
blockConfig.files = [];
fileSet.forEach(function (filesInfo) {
dest = filesInfo.dest;
if (!self.destinations[dest]) {
self.destinations[dest] = filesInfo;
blockConfig.files.push(filesInfo);
} else if (!_.isEqual(self.destinations[dest], filesInfo)) {
throw new Error('Different sources attempting to write to the same destination:\n ' + JSON.stringify(self.destinations[dest], null, ' ') + '\n ' + JSON.stringify(blockConfig, null, ' '));
}
});

if (blockConfig.files.length) {
config[writer.name].generated = deepMerge(config[writer.name].generated, blockConfig);
}
} else {
config[writer.name].generated = deepMerge(config[writer.name].generated, blockConfig);
}
context.inDir = context.outDir;
context.inFiles = context.outFiles;
context.outFiles = [];
Expand Down
116 changes: 116 additions & 0 deletions test/test-config-writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,122 @@ describe('ConfigWriter', function () {
it('should allow for an empty flow');
it('should allow for a filename as input');

it('should deduplicate blocks', function () {
var flow = new Flow({
steps: {
js: ['concat', 'uglifyjs']
}
});
var doubleBlocks = [blocks[0], blocks[0]];
var file = helpers.createFile('foo', 'app', doubleBlocks);
var c = new ConfigWriter(flow, {
input: 'app',
dest: 'dist',
staging: '.tmp'
});
var config = c.process(file);
var expected = helpers.normalize({
concat: {
generated: {
files: [{
dest: '.tmp/concat/scripts/site.js',
src: ['app/foo.js', 'app/bar.js', 'app/baz.js']
}]
}
},
uglify: {
generated: {
files: [{
dest: 'dist/scripts/site.js',
src: ['.tmp/concat/scripts/site.js']
}]
}
}
});

assert.deepEqual(config, expected);
});
it('should deduplicate blocks across files', function () {
var flow = new Flow({
steps: {
js: ['concat', 'uglifyjs']
}
});
var file = helpers.createFile('foo', 'app', blocks);
var c = new ConfigWriter(flow, {
input: 'app',
dest: 'dist',
staging: '.tmp'
});
var firstConfig = c.process(file);
var repeatConfig = c.process(file);
var expectedFirst = helpers.normalize({
concat: {
generated: {
files: [{
dest: '.tmp/concat/scripts/site.js',
src: ['app/foo.js', 'app/bar.js', 'app/baz.js']
}]
}
},
uglify: {
generated: {
files: [{
dest: 'dist/scripts/site.js',
src: ['.tmp/concat/scripts/site.js']
}]
}
}
});
var expectedRepeat = helpers.normalize({
concat: {
generated: {}
},
uglify: {
generated: {}
}
});

assert.deepEqual(firstConfig, expectedFirst);
assert.deepEqual(repeatConfig, expectedRepeat);
});
it('should throw with conflicting blocks', function () {
var flow = new Flow({
steps: {
js: ['concat', 'uglifyjs']
}
});
var conflictBlock = {
type: 'js',
dest: 'scripts/site.js',
searchPath: [],
indent: ' ',
src: [
'foo.js',
'bar.js',
'baz.js',
'fail.js'
],
raw: [
' <!-- build:js scripts/site.js -->',
' <script src="foo.js"></script>',
' <script src="bar.js"></script>',
' <script src="baz.js"></script>',
' <script src="fail.js"></script>',
' <!-- endbuild -->'
]
};
var file = helpers.createFile('foo', 'app', [blocks[0], conflictBlock]);
var c = new ConfigWriter(flow, {
input: 'app',
dest: 'dist',
staging: '.tmp'
});
assert.throws(function () {
c.process(file);
});
});

describe('stepWriters', function () {
it('should return all writers if called without block type', function () {
var flow = new Flow({
Expand Down

0 comments on commit 708ffca

Please sign in to comment.