Skip to content

Commit 45aecc9

Browse files
teviostevemartin
authored andcommitted
Addresses #375
* glob data files in data folder, excluding listitems * merge data as global data
1 parent c35e4ba commit 45aecc9

File tree

6 files changed

+108
-6
lines changed

6 files changed

+108
-6
lines changed

core/lib/patternlab.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,24 @@
1111
"use strict";
1212

1313
var diveSync = require('diveSync'),
14+
glob = require('glob'),
15+
merge = require('merge'),
1416
path = require('path');
1517

1618
// GTP: these two diveSync pattern processors factored out so they can be reused
1719
// from unit tests to reduce code dupe!
1820

21+
function buildPatternData(dataFilesPath, fs) {
22+
var dataFilesPath = dataFilesPath;
23+
var dataFiles = glob.sync(dataFilesPath + '*.json', {"ignore" : [dataFilesPath + 'listitems.json']});
24+
var mergeObject = {}
25+
dataFiles.forEach(function (filePath) {
26+
var jsonData = fs.readJSONSync(path.resolve(filePath), 'utf8')
27+
mergeObject = merge(mergeObject, jsonData)
28+
})
29+
return mergeObject;
30+
}
31+
1932
function processAllPatternsIterative(pattern_assembler, patterns_dir, patternlab) {
2033
diveSync(
2134
patterns_dir,
@@ -183,7 +196,7 @@ var patternlab_engine = function (config) {
183196

184197
function buildPatterns(deletePatternDir) {
185198
try {
186-
patternlab.data = fs.readJSONSync(path.resolve(paths.source.data, 'data.json'));
199+
patternlab.data = buildPatternData(paths.source.data, fs);
187200
} catch (ex) {
188201
plutils.logRed('missing or malformed' + paths.source.data + 'data.json Pattern Lab may not work without this file.');
189202
patternlab.data = {};
@@ -392,6 +405,7 @@ var patternlab_engine = function (config) {
392405
// export these free functions so they're available without calling the exported
393406
// function, for use in reducing code dupe in unit tests. At least, until we
394407
// have a better way to do this
408+
patternlab_engine.build_pattern_data = buildPatternData;
395409
patternlab_engine.process_all_patterns_iterative = processAllPatternsIterative;
396410
patternlab_engine.process_all_patterns_recursive = processAllPatternsRecursive;
397411

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"json5": "^0.5.0",
1111
"lodash": "~4.13.1",
1212
"markdown-it": "^6.0.1",
13+
"merge": "^1.2.0",
1314
"patternengine-node-mustache": "^1.0.0"
1415
},
1516
"devDependencies": {

test/files/_data/data.json

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
{
2+
"key": "value",
3+
"key2": "use this for variables you want to load globally",
4+
"title": "Nullizzle shizznit velizzle, hizzle, suscipit own yo', gravida vizzle, arcu.",
5+
"img": {
6+
"avatar": {
7+
"src": "http://placeimg.com/100/100/people",
8+
"alt": "Avatar"
9+
},
10+
"square": {
11+
"src": "http://placeimg.com/300/300/nature",
12+
"alt": "Square"
13+
},
14+
"rectangle": {
15+
"src": "http://placeimg.com/400/300/tech",
16+
"alt": "Rectangle"
17+
},
18+
"landscape-4x3": {
19+
"src": "http://placeimg.com/400/300/tech",
20+
"alt": "4x3 Image"
21+
},
22+
"landscape-16x9": {
23+
"src": "http://placeimg.com/640/360/tech",
24+
"alt": "16x9 Image"
25+
}
26+
},
27+
"headline": {
28+
"short": "Lorizzle pimpin' dolizzle sit amet I",
29+
"medium": "Rizzle adipiscing elizzle. Nullam sapien velizzle, shit volutpizzle, my"
30+
},
31+
"excerpt": {
32+
"short": "Shizz fo shizzle mah nizzle fo rizzle, mah home g-dizzle, gravida vizzle, arcu. Pellentesque crunk tortizzle. Sed erizzle. Black izzle sheezy telliv.",
33+
"medium": "Izzle crazy tempizzle sizzle. We gonna chung gangsta get down get down fo shizzle turpizzle. Away break it down black. Pellentesque bling bling rhoncus fo shizzle. In hac the bizzle platea dictumst. Black dapibizzle. Crackalackin.",
34+
"long": "Curabitizzle fo shizzle diam quizzle nisi nizzle mollizzle. Suspendisse boofron. Morbi odio. Sure pizzle. Crazy orci. Shut the shizzle up maurizzle get down get down, check out this a, go to hizzle sit amizzle, malesuada izzle, pede. Pellentesque gravida. Vestibulizzle check it out mi, volutpat izzle, shiz sed, shiznit sempizzle, da bomb. Funky fresh in ipsum. Da bomb volutpat felis vizzle daahng dawg. Crizzle quis dope izzle fo shizzle my ni."
35+
},
36+
"description": "Fizzle crazy tortor. Sed rizzle. Ass pimpin' dolor dapibizzle turpis tempizzle fo shizzle my nizzle. Maurizzle pellentesque its fo rizzle izzle turpis. Get down get down we gonna chung nizzle. Shizzlin dizzle eleifend rhoncizzle break it down. In yo ghetto platea dictumst. Bling bling dapibizzle. Curabitur break yo neck, yall fo, pretizzle eu, go to hizzle dope, own yo' vitae, nunc. Bizzle suscipizzle. Ass semper velit sizzle fo.",
37+
"url": "http://lorizzle.nl/",
38+
"name": {
39+
"first": "Junius",
40+
"firsti": "J",
41+
"middle": "Marius",
42+
"middlei": "M",
43+
"last": "Koolen",
44+
"lasti": "K"
45+
},
46+
"year": {
47+
"long": "2013",
48+
"short": "13"
49+
},
50+
"month": {
51+
"long": "January",
52+
"short": "Jan",
53+
"digit": "01"
54+
},
55+
"dayofweek": {
56+
"long": "Sunday",
57+
"short": "Sun"
58+
},
59+
"day": {
60+
"long": "01",
61+
"short": "1",
62+
"ordinal": "st"
63+
},
64+
"hour": {
65+
"long": "06",
66+
"short": "6",
67+
"military": "06",
68+
"ampm": "am"
69+
},
70+
"minute": {
71+
"long": "20",
72+
"short": "20"
73+
},
74+
"seconds": "31"
75+
}
76+

test/files/_data/foo.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "foo" : "bar" }

test/files/_data/listitems.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"test_list_item" : "izzn thizzle"
3+
}
4+

test/patternlab_tests.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
'use strict';
22

33
exports['test nodeunit'] = {
4-
'hello world' : function(test){
5-
test.equals(1,1);
6-
test.done();
7-
}
8-
};
4+
'buildPatternData - should merge all JSON files in the data folder except listitems' : function(test){
5+
var fs = require('fs-extra');
6+
var plMain = require('../core/lib/patternlab');
7+
var data_dir = './test/files/_data/';
8+
9+
var dataResult = plMain.build_pattern_data(data_dir, fs);
10+
test.equals(dataResult.foo, "bar");
11+
test.equals(dataResult.test_list_item, undefined);
12+
test.done();
13+
}
14+
};

0 commit comments

Comments
 (0)