Skip to content

Commit e802bae

Browse files
Merge pull request #556 from /issues/549-viewall
Issues/549 viewall
2 parents 0de8279 + 2136e38 commit e802bae

File tree

5 files changed

+78
-34
lines changed

5 files changed

+78
-34
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ before_install:
99
- phantomjs --version
1010

1111
before_script:
12-
- npm install -g grunt-cli
1312
- npm install patternengine-node-underscore
1413
- npm install patternengine-node-handlebars
1514
- npm install patternengine-node-twig

core/lib/patternlab.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* patternlab-node - v2.6.1 - 2016
2+
* patternlab-node - v2.6.2 - 2016
33
*
44
* Brian Muenzenmeyer, Geoff Pursell, and the web community.
55
* Licensed under the MIT license.

core/lib/ui_builder.js

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ var fs = require('fs-extra');
66
var ae = require('./annotation_exporter');
77
var of = require('./object_factory');
88
var Pattern = of.Pattern;
9-
var pa = require('./pattern_assembler');
10-
var pattern_assembler = new pa();
9+
var pattern_assembler = require('./pattern_assembler')();
1110
var plutils = require('./utilities');
1211
var eol = require('os').EOL;
1312
var _ = require('lodash');
@@ -54,20 +53,6 @@ var ui_builder = function () {
5453
}
5554
}
5655

57-
/**
58-
* Writes a file to disk, with an optional callback
59-
* @param filePath - the path to write to with filename
60-
* @param data - the file contents
61-
* @param callback - an optional callback
62-
*/
63-
function writeFile(filePath, data, callback) {
64-
if (callback) {
65-
fs.outputFileSync(filePath, data, callback);
66-
} else {
67-
fs.outputFileSync(filePath, data);
68-
}
69-
}
70-
7156
/**
7257
* Returns whether or not the pattern should be excluded from direct rendering or navigation on the front end
7358
* @param pattern - the pattern to test for inclusion/exclusion
@@ -468,7 +453,7 @@ var ui_builder = function () {
468453
//do not create a viewall page for flat patterns
469454
if (patternType === patternSubtype) {
470455
writeViewAllFile = false;
471-
return false;
456+
return;
472457
}
473458

474459
//render the footer needed for the viewall template
@@ -485,13 +470,12 @@ var ui_builder = function () {
485470
typePatterns = typePatterns.concat(subtypePatterns);
486471

487472
var viewAllHTML = buildViewAllHTML(patternlab, subtypePatterns, patternPartial);
488-
writeFile(paths.public.patterns + p.flatPatternPath + '/index.html', mainPageHeadHtml + viewAllHTML + footerHTML);
489-
return true; //stop yelling at us eslint we know we know
473+
fs.outputFileSync(paths.public.patterns + p.flatPatternPath + '/index.html', mainPageHeadHtml + viewAllHTML + footerHTML);
490474
});
491475

492476
//do not create a viewall page for flat patterns
493477
if (!writeViewAllFile || !p) {
494-
return false;
478+
return;
495479
}
496480

497481
//render the footer needed for the viewall template
@@ -507,7 +491,7 @@ var ui_builder = function () {
507491

508492
//render the viewall template for the type
509493
var viewAllHTML = buildViewAllHTML(patternlab, typePatterns, patternType);
510-
writeFile(paths.public.patterns + anyPatternOfType.patternType + '/index.html', mainPageHeadHtml + viewAllHTML + footerHTML);
494+
fs.outputFileSync(paths.public.patterns + anyPatternOfType.patternType + '/index.html', mainPageHeadHtml + viewAllHTML + footerHTML);
511495

512496
//determine if we should omit this patterntype completely from the viewall page
513497
var omitPatternType = styleGuideExcludes && styleGuideExcludes.length
@@ -521,8 +505,6 @@ var ui_builder = function () {
521505
} else {
522506
patterns = patterns.concat(typePatterns);
523507
}
524-
525-
return true; //stop yelling at us eslint we know we know
526508
});
527509
return patterns;
528510
}
@@ -561,12 +543,12 @@ var ui_builder = function () {
561543
output += 'var defaultPattern = "' + (patternlab.config.defaultPattern ? patternlab.config.defaultPattern : 'all') + '";' + eol;
562544

563545
//write all output to patternlab-data
564-
writeFile(path.resolve(paths.public.data, 'patternlab-data.js'), output);
546+
fs.outputFileSync(path.resolve(paths.public.data, 'patternlab-data.js'), output);
565547

566548
//annotations
567549
var annotationsJSON = annotation_exporter.gather();
568550
var annotations = 'var comments = { "comments" : ' + JSON.stringify(annotationsJSON) + '};';
569-
writeFile(path.resolve(paths.public.annotations, 'annotations.js'), annotations);
551+
fs.outputFileSync(path.resolve(paths.public.annotations, 'annotations.js'), annotations);
570552
}
571553

572554
/**
@@ -626,7 +608,7 @@ var ui_builder = function () {
626608
patternSection: patternlab.patternSection,
627609
patternSectionSubtype: patternlab.patternSectionSubType
628610
});
629-
writeFile(path.resolve(paths.public.styleguide, 'html/styleguide.html'), headerHTML + styleguideHtml + footerHTML);
611+
fs.outputFileSync(path.resolve(paths.public.styleguide, 'html/styleguide.html'), headerHTML + styleguideHtml + footerHTML);
630612

631613
//move the index file from its asset location into public root
632614
var patternlabSiteHtml;
@@ -637,7 +619,7 @@ var ui_builder = function () {
637619
console.log("\nERROR: Could not load one or more styleguidekit assets from", paths.source.styleguide, '\n');
638620
process.exit(1);
639621
}
640-
writeFile(path.resolve(paths.public.root, 'index.html'), patternlabSiteHtml);
622+
fs.outputFileSync(path.resolve(paths.public.root, 'index.html'), patternlabSiteHtml);
641623

642624
//write out patternlab.data object to be read by the client
643625
exportData(patternlab);
@@ -655,6 +637,9 @@ var ui_builder = function () {
655637
},
656638
resetUIBuilderState: function (patternlab) {
657639
resetUIBuilderState(patternlab);
640+
},
641+
buildViewAllPages: function (mainPageHeadHtml, patternlab, styleguidePatterns) {
642+
return buildViewAllPages(mainPageHeadHtml, patternlab, styleguidePatterns);
658643
}
659644
};
660645

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "patternlab-node",
33
"description": "Pattern Lab is a collection of tools to help you create atomic design systems. This is the node command line interface (CLI).",
4-
"version": "2.6.1",
4+
"version": "2.6.2",
55
"main": "./core/lib/patternlab.js",
66
"dependencies": {
77
"diveSync": "^0.3.0",
@@ -17,6 +17,7 @@
1717
},
1818
"devDependencies": {
1919
"eslint": "^3.5.0",
20+
"rewire": "^2.5.2",
2021
"tap": "^7.1.2"
2122
},
2223
"keywords": [

test/ui_builder_tests.js

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
11
"use strict";
22

33
var tap = require('tap');
4-
4+
var rewire = require("rewire");
55
var eol = require('os').EOL;
66
var Pattern = require('../core/lib/object_factory').Pattern;
77
var extend = require('util')._extend;
8+
var uiModule = rewire('../core/lib/ui_builder');
9+
10+
//set up a global mocks - we don't want to be writing/rendering any files right now
11+
var fsMock = {
12+
outputFileSync: function (path, data, cb) { }
13+
};
14+
15+
var patternAssemblerMock = {
16+
renderPattern: function (template, data, partials) { return ''; }
17+
};
18+
19+
//set our mocks in place of usual require()
20+
uiModule.__set__({
21+
'fs': fsMock,
22+
'pattern_assembler': patternAssemblerMock
23+
});
24+
25+
var ui = uiModule();
826

927
function createFakePatternLab(customProps) {
1028
var pl = {
@@ -24,13 +42,12 @@ function createFakePatternLab(customProps) {
2442
rawTemplate: '',
2543
markupOnly: '.markup-only'
2644
}
27-
}
45+
},
46+
data: {}
2847
};
2948
return extend(pl, customProps);
3049
}
3150

32-
var ui = require('../core/lib/ui_builder')();
33-
3451
tap.test('isPatternExcluded - returns true when pattern filename starts with underscore', function (test) {
3552
//arrange
3653
var patternlab = createFakePatternLab({});
@@ -246,3 +263,45 @@ tap.test('resetUIBuilderState - reset global objects', function (test) {
246263

247264
test.end();
248265
});
266+
267+
tap.test('buildViewAllPages - adds viewall page for each type and subtype', function (test) {
268+
//arrange
269+
let mainPageHeadHtml = '<head></head>';
270+
let patternlab = createFakePatternLab({
271+
patterns: [],
272+
patternGroups: {},
273+
subtypePatterns: {}
274+
});
275+
276+
patternlab.patterns.push(
277+
//this flat pattern is found and causes trouble for the rest of the crew
278+
new Pattern('00-test/foo.mustache'),
279+
new Pattern('patternType1/patternSubType1/blue.mustache'),
280+
new Pattern('patternType1/patternSubType1/red.mustache'),
281+
new Pattern('patternType1/patternSubType1/yellow.mustache'),
282+
new Pattern('patternType1/patternSubType2/black.mustache'),
283+
new Pattern('patternType1/patternSubType2/grey.mustache'),
284+
new Pattern('patternType1/patternSubType2/white.mustache')
285+
);
286+
ui.resetUIBuilderState(patternlab);
287+
288+
let styleguidePatterns = ui.groupPatterns(patternlab);
289+
290+
//act
291+
var patterns = ui.buildViewAllPages(mainPageHeadHtml, patternlab, styleguidePatterns);
292+
293+
//assert
294+
//this was a nuanced one. buildViewAllPages() had return false; statements
295+
//within _.forOwn(...) loops, causing premature termination of the entire loop
296+
//when what was intended was a continue
297+
//we expect 8 here because:
298+
// - foo.mustache is flat and therefore does not have a viewall page
299+
// - the colors.mustache files make 6
300+
// - patternSubType1 and patternSubType2 make 8
301+
//while most of that heavy lifting occurs inside groupPatterns and not buildViewAllPages,
302+
//it's important to ensure that this method does not get prematurely terminated
303+
//we choose to do that by checking it's return number of patterns
304+
test.equals(patterns.length, 8, '2 viewall pages should be added');
305+
306+
test.end();
307+
});

0 commit comments

Comments
 (0)