Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addresses #375 allow foo.json #400

Merged
merged 1 commit into from
Jul 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion core/lib/patternlab.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,24 @@
"use strict";

var diveSync = require('diveSync'),
glob = require('glob'),
_ = require('lodash'),
path = require('path');

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

function buildPatternData(dataFilesPath, fs) {
var dataFilesPath = dataFilesPath;
var dataFiles = glob.sync(dataFilesPath + '*.json', {"ignore" : [dataFilesPath + 'listitems.json']});
var mergeObject = {}
dataFiles.forEach(function (filePath) {
var jsonData = fs.readJSONSync(path.resolve(filePath), 'utf8')
mergeObject = _.merge(mergeObject, jsonData)
})
return mergeObject;
}

function processAllPatternsIterative(pattern_assembler, patterns_dir, patternlab) {
diveSync(
patterns_dir,
Expand Down Expand Up @@ -183,7 +196,7 @@ var patternlab_engine = function (config) {

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

Expand Down
2 changes: 2 additions & 0 deletions test/files/_data/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{ "data" : "test" }

1 change: 1 addition & 0 deletions test/files/_data/foo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "foo" : "bar" }
4 changes: 4 additions & 0 deletions test/files/_data/listitems.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"test_list_item" : "izzn thizzle"
}

17 changes: 12 additions & 5 deletions test/patternlab_tests.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
'use strict';

exports['test nodeunit'] = {
'hello world' : function(test){
test.equals(1,1);
test.done();
}
};
'buildPatternData - should merge all JSON files in the data folder except listitems' : function(test){
var fs = require('fs-extra');
var plMain = require('../core/lib/patternlab');
var data_dir = './test/files/_data/';

var dataResult = plMain.build_pattern_data(data_dir, fs);
test.equals(dataResult.data, "test");
test.equals(dataResult.foo, "bar");
test.equals(dataResult.test_list_item, undefined);
test.done();
}
};