Skip to content

Commit a88e73d

Browse files
teviostevemartin
authored andcommitted
Addresses #375
* glob data files in data folder, excluding listitems * merge data as global data * tests inform code * use lodash for merge
1 parent c35e4ba commit a88e73d

File tree

5 files changed

+34
-6
lines changed

5 files changed

+34
-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+
_ = require('lodash'),
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

test/files/_data/data.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{ "data" : "test" }
2+

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: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
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.data, "test");
11+
test.equals(dataResult.foo, "bar");
12+
test.equals(dataResult.test_list_item, undefined);
13+
test.done();
14+
}
15+
};

0 commit comments

Comments
 (0)