Skip to content

Commit 65b5c5f

Browse files
author
Brandon Morrison
committed
#349: Use js-yaml to parse json, yaml, and yml config files
1 parent d9ee3ae commit 65b5c5f

File tree

5 files changed

+43
-14
lines changed

5 files changed

+43
-14
lines changed

core/lib/patternlab.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
"use strict";
1212

13-
var diveSync = require('diveSync'),
13+
const diveSync = require('diveSync'),
1414
glob = require('glob'),
1515
_ = require('lodash'),
1616
path = require('path'),
@@ -35,16 +35,25 @@ console.log(
3535
chalk.bold(']====\n')
3636
);
3737

38-
var patternEngines = require('./pattern_engines');
39-
var EventEmitter = require('events').EventEmitter;
38+
const patternEngines = require('./pattern_engines');
39+
const EventEmitter = require('events').EventEmitter;
4040

41+
const yaml = require('js-yaml');
42+
43+
/**
44+
* Given a path, load info from the folder to compile into a single config object.
45+
* @param dataFilesPath
46+
* @param fsDep
47+
* @returns {{}}
48+
*/
4149
function buildPatternData(dataFilesPath, fsDep) {
42-
var dataFiles = glob.sync(dataFilesPath + '*.json', {"ignore" : [dataFilesPath + 'listitems.json']});
43-
var mergeObject = {};
50+
let dataFiles = glob.sync(dataFilesPath + '*.{json,yml,yaml}', {"ignore" : [dataFilesPath + 'listitems.{json,yml,yaml}']});
51+
let mergeObject = {};
4452
dataFiles.forEach(function (filePath) {
45-
var jsonData = fsDep.readJSONSync(path.resolve(filePath), 'utf8');
53+
let jsonData = yaml.safeLoad(fsDep.readFileSync(path.resolve(filePath), 'utf8'));
4654
mergeObject = _.merge(mergeObject, jsonData);
4755
});
56+
4857
return mergeObject;
4958
}
5059

@@ -492,9 +501,15 @@ var patternlab_engine = function (config) {
492501
patternlab.data = {};
493502
}
494503
try {
495-
patternlab.listitems = fs.readJSONSync(path.resolve(paths.source.data, 'listitems.json'));
504+
let dataFiles = glob.sync(paths.source.data + 'listitems.{json,yml,yaml}');
505+
let mergeObject = {};
506+
dataFiles.forEach(function (filePath) {
507+
let jsonData = yaml.safeLoad(fs.readFileSync(path.resolve(filePath), 'utf8'));
508+
mergeObject = _.merge(mergeObject, jsonData);
509+
});
510+
patternlab.listitems = mergeObject;
496511
} catch (ex) {
497-
plutils.warning('WARNING: missing or malformed ' + paths.source.data + 'listitems.json file. Pattern Lab may not work without this file.');
512+
plutils.warning('WARNING: missing or malformed ' + paths.source.data + 'listitems file. Pattern Lab may not work without this file.');
498513
patternlab.listitems = {};
499514
}
500515
try {

test/files/_data/data.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
{ "data" : "test" }
1+
{ "data" : "test", "from_json" : "from_json" }
22

test/files/_data/data.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from_yaml: "from_yaml"

test/files/_data/data.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from_yml: "from_yml"

test/patternlab_tests.js

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

3-
var tap = require('tap');
3+
const tap = require('tap');
44

55
tap.test('buildPatternData - should merge all JSON files in the data folder except listitems', function(test){
6-
var fs = require('fs-extra');
7-
var plMain = require('../core/lib/patternlab');
8-
var data_dir = './test/files/_data/';
6+
const fs = require('fs-extra'),
7+
plMain = require('../core/lib/patternlab'),
8+
data_dir = './test/files/_data/';
99

10-
var dataResult = plMain.build_pattern_data(data_dir, fs);
10+
let dataResult = plMain.build_pattern_data(data_dir, fs);
1111
test.equals(dataResult.data, "test");
1212
test.equals(dataResult.foo, "bar");
1313
test.equals(dataResult.test_list_item, undefined);
1414
test.end();
1515
});
16+
17+
tap.test('buildPatternData - can load json, yaml, and yml files', function(test) {
18+
const fs = require('fs-extra'),
19+
plMain = require('../core/lib/patternlab'),
20+
data_dir = './test/files/_data/';
21+
22+
let dataResult = plMain.build_pattern_data(data_dir, fs);
23+
test.equals(dataResult.from_yml, "from_yml");
24+
test.equals(dataResult.from_yaml, "from_yaml");
25+
test.equals(dataResult.from_json, "from_json");
26+
test.end();
27+
});

0 commit comments

Comments
 (0)