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

Commit 65b28d5

Browse files
author
Aaron Boushley
committed
Skip duplicate configurations.
This should resolve #289. This also detects a bad configuration where multiple blocks attempt to write to the same destination with different sources.
1 parent 05dcb0c commit 65b28d5

File tree

2 files changed

+140
-1
lines changed

2 files changed

+140
-1
lines changed

lib/configwriter.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ var ConfigWriter = module.exports = function (flow, dirs) {
4949
this.staging = dirs.staging;
5050
this.steps = {};
5151
this.postprocessors = [];
52+
this.destinations = {};
5253

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

160161
self.forEachStep(block.type, function (writer, last) {
162+
var blockConfig;
163+
var fileSet;
164+
var dest;
161165

162166
// If this is the last writer of the pipe, we need to output
163167
// in the destination directory
@@ -167,7 +171,26 @@ ConfigWriter.prototype.process = function (file, config) {
167171
config[writer.name].generated = config[writer.name].generated || {};
168172
context.options = config[writer.name];
169173
// config[writer.name].generated = _.extend(config[writer.name].generated, writer.createConfig(context, block));
170-
config[writer.name].generated = deepMerge(config[writer.name].generated, writer.createConfig(context, block));
174+
blockConfig = writer.createConfig(context, block);
175+
if (blockConfig.files) {
176+
fileSet = blockConfig.files;
177+
blockConfig.files = [];
178+
fileSet.forEach(function (filesInfo) {
179+
dest = filesInfo.dest;
180+
if (!self.destinations[dest]) {
181+
self.destinations[dest] = filesInfo;
182+
blockConfig.files.push(filesInfo);
183+
} else if (!_.isEqual(self.destinations[dest], filesInfo)) {
184+
throw new Error('Different sources attempting to write to the same destination:\n ' + JSON.stringify(self.destinations[dest], null, ' ') + '\n ' + JSON.stringify(blockConfig, null, ' '));
185+
}
186+
});
187+
188+
if (blockConfig.files.length) {
189+
config[writer.name].generated = deepMerge(config[writer.name].generated, blockConfig);
190+
}
191+
} else {
192+
config[writer.name].generated = deepMerge(config[writer.name].generated, blockConfig);
193+
}
171194
context.inDir = context.outDir;
172195
context.inFiles = context.outFiles;
173196
context.outFiles = [];

test/test-config-writer.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,122 @@ describe('ConfigWriter', function () {
266266
it('should allow for an empty flow');
267267
it('should allow for a filename as input');
268268

269+
it('should deduplicate blocks', function () {
270+
var flow = new Flow({
271+
steps: {
272+
js: ['concat', 'uglifyjs']
273+
}
274+
});
275+
var doubleBlocks = [blocks[0], blocks[0]];
276+
var file = helpers.createFile('foo', 'app', doubleBlocks);
277+
var c = new ConfigWriter(flow, {
278+
input: 'app',
279+
dest: 'dist',
280+
staging: '.tmp'
281+
});
282+
var config = c.process(file);
283+
var expected = helpers.normalize({
284+
concat: {
285+
generated: {
286+
files: [{
287+
dest: '.tmp/concat/scripts/site.js',
288+
src: ['app/foo.js', 'app/bar.js', 'app/baz.js']
289+
}]
290+
}
291+
},
292+
uglify: {
293+
generated: {
294+
files: [{
295+
dest: 'dist/scripts/site.js',
296+
src: ['.tmp/concat/scripts/site.js']
297+
}]
298+
}
299+
}
300+
});
301+
302+
assert.deepEqual(config, expected);
303+
});
304+
it('should deduplicate blocks across files', function () {
305+
var flow = new Flow({
306+
steps: {
307+
js: ['concat', 'uglifyjs']
308+
}
309+
});
310+
var file = helpers.createFile('foo', 'app', blocks);
311+
var c = new ConfigWriter(flow, {
312+
input: 'app',
313+
dest: 'dist',
314+
staging: '.tmp'
315+
});
316+
var firstConfig = c.process(file);
317+
var repeatConfig = c.process(file);
318+
var expectedFirst = helpers.normalize({
319+
concat: {
320+
generated: {
321+
files: [{
322+
dest: '.tmp/concat/scripts/site.js',
323+
src: ['app/foo.js', 'app/bar.js', 'app/baz.js']
324+
}]
325+
}
326+
},
327+
uglify: {
328+
generated: {
329+
files: [{
330+
dest: 'dist/scripts/site.js',
331+
src: ['.tmp/concat/scripts/site.js']
332+
}]
333+
}
334+
}
335+
});
336+
var expectedRepeat = helpers.normalize({
337+
concat: {
338+
generated: {}
339+
},
340+
uglify: {
341+
generated: {}
342+
}
343+
});
344+
345+
assert.deepEqual(firstConfig, expectedFirst);
346+
assert.deepEqual(repeatConfig, expectedRepeat);
347+
});
348+
it('should throw with conflicting blocks', function () {
349+
var flow = new Flow({
350+
steps: {
351+
js: ['concat', 'uglifyjs']
352+
}
353+
});
354+
var conflictBlock = {
355+
type: 'js',
356+
dest: 'scripts/site.js',
357+
searchPath: [],
358+
indent: ' ',
359+
src: [
360+
'foo.js',
361+
'bar.js',
362+
'baz.js',
363+
'fail.js'
364+
],
365+
raw: [
366+
' <!-- build:js scripts/site.js -->',
367+
' <script src="foo.js"></script>',
368+
' <script src="bar.js"></script>',
369+
' <script src="baz.js"></script>',
370+
' <script src="fail.js"></script>',
371+
' <!-- endbuild -->'
372+
]
373+
};
374+
var file = helpers.createFile('foo', 'app', [blocks[0], conflictBlock]);
375+
var c = new ConfigWriter(flow, {
376+
input: 'app',
377+
dest: 'dist',
378+
staging: '.tmp'
379+
});
380+
assert.throws(function () {
381+
c.process(file);
382+
});
383+
});
384+
269385
describe('stepWriters', function () {
270386
it('should return all writers if called without block type', function () {
271387
var flow = new Flow({

0 commit comments

Comments
 (0)