Skip to content

Commit a62a089

Browse files
committed
Merge branch 'fillerwriter-feature/349-yaml-support-pr' into dev
2 parents a52a346 + e3f28d7 commit a62a089

File tree

8 files changed

+112
-31
lines changed

8 files changed

+112
-31
lines changed

core/lib/data_loader.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"use strict";
2+
3+
const glob = require('glob'),
4+
_ = require('lodash'),
5+
path = require('path'),
6+
yaml = require('js-yaml');
7+
8+
/**
9+
* Loads a single config file, in yaml/json format.
10+
*
11+
* @param dataFilesPath - leave off the file extension.
12+
* @param fsDep
13+
* @returns {*}
14+
*/
15+
function loadFile(dataFilesPath, fsDep) {
16+
const dataFilesFullPath = dataFilesPath + '*.{json,yml,yaml}';
17+
18+
if (dataFilesPath) {
19+
const dataFiles = glob.sync(dataFilesFullPath),
20+
dataFile = _.head(dataFiles);
21+
22+
if (dataFile && fsDep.existsSync(path.resolve(dataFile))) {
23+
return yaml.safeLoad(fsDep.readFileSync(path.resolve(dataFile), 'utf8'));
24+
}
25+
}
26+
27+
return null;
28+
}
29+
30+
/**
31+
* Loads a set of config files from a folder, in yaml/json format.
32+
*
33+
* @param dataFilesPath - leave off the file extension
34+
* @param excludeFileNames - leave off the file extension
35+
* @param fsDep
36+
* @returns Object, with merged data files, empty object if no files.
37+
*/
38+
function loadDataFromFolder(dataFilesPath, excludeFileNames, fsDep) {
39+
const dataFilesFullPath = dataFilesPath + '*.{json,yml,yaml}',
40+
excludeFullPath = dataFilesPath + excludeFileNames + '.{json,yml,yaml}';
41+
42+
let globOptions = {};
43+
if (excludeFileNames) {
44+
globOptions.ignore = [excludeFullPath];
45+
}
46+
47+
const dataFiles = glob.sync(dataFilesFullPath, globOptions);
48+
let mergeObject = {};
49+
50+
dataFiles.forEach(function (filePath) {
51+
let jsonData = yaml.safeLoad(fsDep.readFileSync(path.resolve(filePath), 'utf8'));
52+
mergeObject = _.merge(mergeObject, jsonData);
53+
});
54+
55+
return mergeObject;
56+
}
57+
58+
module.exports = function configFileLoader() {
59+
return {
60+
loadDataFromFile: loadFile,
61+
loadDataFromFolder: loadDataFromFolder
62+
};
63+
};

core/lib/pattern_assembler.js

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var path = require('path'),
88
pph = require('./pseudopattern_hunter'),
99
mp = require('./markdown_parser'),
1010
plutils = require('./utilities'),
11+
dataLoader = require('./data_loader')(),
1112
patternEngines = require('./pattern_engines'),
1213
lh = require('./lineage_hunter'),
1314
lih = require('./list_item_hunter'),
@@ -324,16 +325,13 @@ var pattern_assembler = function () {
324325

325326
//look for a json file for this template
326327
try {
327-
var jsonFilename = path.resolve(patternsPath, currentPattern.subdir, currentPattern.fileName + ".json");
328-
try {
329-
var jsonFilenameStats = fs.statSync(jsonFilename);
330-
} catch (err) {
331-
//not a file
332-
}
333-
if (jsonFilenameStats && jsonFilenameStats.isFile()) {
334-
currentPattern.jsonFileData = fs.readJSONSync(jsonFilename);
328+
var jsonFilename = path.resolve(patternsPath, currentPattern.subdir, currentPattern.fileName);
329+
let configData = dataLoader.loadDataFromFile(jsonFilename, fs);
330+
331+
if (configData) {
332+
currentPattern.jsonFileData = configData;
335333
if (patternlab.config.debug) {
336-
console.log('processPatternIterative: found pattern-specific data.json for ' + currentPattern.patternPartial);
334+
console.log('processPatternIterative: found pattern-specific config data for ' + currentPattern.patternPartial);
337335
}
338336
}
339337
}
@@ -344,17 +342,14 @@ var pattern_assembler = function () {
344342

345343
//look for a listitems.json file for this template
346344
try {
347-
var listJsonFileName = path.resolve(patternsPath, currentPattern.subdir, currentPattern.fileName + ".listitems.json");
348-
try {
349-
var listJsonFileStats = fs.statSync(listJsonFileName);
350-
} catch (err) {
351-
//not a file
352-
}
353-
if (listJsonFileStats && listJsonFileStats.isFile()) {
354-
currentPattern.listitems = fs.readJSONSync(listJsonFileName);
345+
var listJsonFileName = path.resolve(patternsPath, currentPattern.subdir, currentPattern.fileName + ".listitems");
346+
let listItemsConfig = dataLoader.loadDataFromFile(listJsonFileName, fs);
347+
348+
if (listItemsConfig) {
349+
currentPattern.listitems = listItemsConfig;
355350
buildListItems(currentPattern);
356351
if (patternlab.config.debug) {
357-
console.log('found pattern-specific listitems.json for ' + currentPattern.patternPartial);
352+
console.log('found pattern-specific listitems config for ' + currentPattern.patternPartial);
358353
}
359354
}
360355
}

core/lib/patternlab.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"use strict";
1212

1313
var diveSync = require('diveSync'),
14-
glob = require('glob'),
1514
_ = require('lodash'),
1615
path = require('path'),
1716
chalk = require('chalk'),
@@ -20,6 +19,7 @@ var diveSync = require('diveSync'),
2019
pm = require('./plugin_manager'),
2120
fs = require('fs-extra'),
2221
packageInfo = require('../../package.json'),
22+
dataLoader = require('./data_loader')(),
2323
plutils = require('./utilities'),
2424
jsonCopy = require('./json_copy'),
2525
ui = require('./ui_builder'),
@@ -43,14 +43,14 @@ console.log(
4343
var patternEngines = require('./pattern_engines');
4444
var EventEmitter = require('events').EventEmitter;
4545

46+
/**
47+
* Given a path, load info from the folder to compile into a single config object.
48+
* @param dataFilesPath
49+
* @param fsDep
50+
* @returns {{}}
51+
*/
4652
function buildPatternData(dataFilesPath, fsDep) {
47-
var dataFiles = glob.sync(dataFilesPath + '*.json', {"ignore" : [dataFilesPath + 'listitems.json']});
48-
var mergeObject = {};
49-
dataFiles.forEach(function (filePath) {
50-
var jsonData = fsDep.readJSONSync(path.resolve(filePath), 'utf8');
51-
mergeObject = _.merge(mergeObject, jsonData);
52-
});
53-
return mergeObject;
53+
return dataLoader.loadDataFromFolder(dataFilesPath, 'listitems', fsDep);
5454
}
5555

5656
// GTP: these two diveSync pattern processors factored out so they can be reused
@@ -504,9 +504,9 @@ var patternlab_engine = function (config) {
504504
patternlab.data = {};
505505
}
506506
try {
507-
patternlab.listitems = fs.readJSONSync(path.resolve(paths.source.data, 'listitems.json'));
507+
patternlab.listitems = dataLoader.loadDataFromFile(path.resolve(paths.source.data, 'listitems.{json,yml,yaml}'));
508508
} catch (ex) {
509-
plutils.warning('WARNING: missing or malformed ' + paths.source.data + 'listitems.json file. Pattern Lab may not work without this file.');
509+
plutils.warning('WARNING: missing or malformed ' + paths.source.data + 'listitems file. Pattern Lab may not work without this file.');
510510
patternlab.listitems = {};
511511
}
512512
try {

test/data_loader_tests.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
const tap = require('tap');
4+
5+
tap.test('loadDataFromFile - Load ', function(test){
6+
const fs = require('fs-extra'),
7+
dataLoader = require('../core/lib/data_loader')(),
8+
data_dir = './test/files/_data/';
9+
10+
let data = dataLoader.loadDataFromFile(data_dir + 'foo', fs);
11+
test.equals(data.foo, 'bar');
12+
test.end();
13+
});

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: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ tap.test('buildPatterns - should replace data link even when pattern parameter p
4949
only on the order of events within build.
5050
*/
5151
export_patterns: function (patternlab) {
52-
var pattern = _.find(patternlab.patterns, (pattern) => {
53-
return pattern.patternPartial === 'test-paramParent';
52+
var pattern = _.find(patternlab.patterns, (p) => {
53+
return p.patternPartial === 'test-paramParent';
5454
});
5555
//assert
5656
test.equals(pattern.patternPartialCode.indexOf('00-test-00-foo.rendered.html') > -1, true, 'data link should be replaced properly');
@@ -68,6 +68,14 @@ tap.test('buildPatterns - should replace data link even when pattern parameter p
6868
pl.build(function() {
6969
test.end();
7070
}, true);
71+
});
7172

73+
tap.test('buildPatternData - can load json, yaml, and yml files', function(test) {
74+
const data_dir = './test/files/_data/';
7275

76+
let dataResult = plEngineModule.build_pattern_data(data_dir, fs);
77+
test.equals(dataResult.from_yml, "from_yml");
78+
test.equals(dataResult.from_yaml, "from_yaml");
79+
test.equals(dataResult.from_json, "from_json");
80+
test.end();
7381
});

0 commit comments

Comments
 (0)