Skip to content

Commit fb1e031

Browse files
Merge pull request #680 from pattern-lab/dev
Pattern Lab Node Core 2.11.0
2 parents 52e77bf + c60c2be commit fb1e031

13 files changed

+133
-38
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/object_factory.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ var Pattern = function (relPath, data, patternlab) {
5959

6060
// Let's calculate the verbose name ahead of time! We don't use path.sep here
6161
// on purpose. This isn't a file name!
62-
this.verbosePartial = this.subdir + '/' + this.fileName;
62+
this.verbosePartial = this.subdir.split(path.sep).join('/') + '/' + this.fileName;
6363

6464
this.isPattern = true;
6565
this.isFlatPattern = this.patternGroup === this.patternSubGroup;

core/lib/parameter_hunter.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ var parameter_hunter = function () {
5050
* * Return paramStringWellFormed.
5151
*
5252
* @param {string} pString
53+
* @param {object} patternlab
5354
* @returns {string} paramStringWellFormed
5455
*/
55-
function paramToJson(pString) {
56+
function paramToJson(pString, patternlab) {
5657
var colonPos = -1;
5758
var keys = [];
5859
var paramString = pString; // to not reassign param
@@ -62,6 +63,17 @@ var parameter_hunter = function () {
6263
var values = [];
6364
var wrapper;
6465

66+
// attempt to parse the data in case it is already well formed JSON
67+
try {
68+
paramStringWellFormed = JSON.stringify(JSON.parse(pString));
69+
return paramStringWellFormed;
70+
} catch (err) {
71+
//todo this might be a good candidate for a different log level, should we implement that someday
72+
if (patternlab.config.debug) {
73+
console.log(`Not valid JSON found for passed pattern parameter ${pString} will attempt to parse manually...`);
74+
}
75+
}
76+
6577
//replace all escaped double-quotes with escaped unicode
6678
paramString = paramString.replace(/\\"/g, '\\u0022');
6779

@@ -253,7 +265,7 @@ var parameter_hunter = function () {
253265
var leftParen = pMatch.indexOf('(');
254266
var rightParen = pMatch.lastIndexOf(')');
255267
var paramString = '{' + pMatch.substring(leftParen + 1, rightParen) + '}';
256-
var paramStringWellFormed = paramToJson(paramString);
268+
var paramStringWellFormed = paramToJson(paramString, patternlab);
257269

258270
var paramData = {};
259271
var globalData = {};

core/lib/pattern_assembler.js

Lines changed: 14 additions & 19 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'),
@@ -34,7 +35,7 @@ var pattern_assembler = function () {
3435
for (var i = 0; i < patternlab.patterns.length; i++) {
3536
switch (partialName) {
3637
case patternlab.patterns[i].relPath:
37-
case patternlab.patterns[i].subdir + '/' + patternlab.patterns[i].fileName:
38+
case patternlab.patterns[i].verbosePartial:
3839
return patternlab.patterns[i];
3940
}
4041
}
@@ -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/pattern_registry.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ PatternRegistry.prototype = {
6262
for (let thisPattern of patterns) {
6363
switch (partialName) {
6464
case thisPattern.relPath:
65-
case thisPattern.subdir + '/' + thisPattern.fileName:
65+
case thisPattern.verbosePartial:
6666
return thisPattern;
6767
}
6868
}

core/lib/patternlab.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* patternlab-node - v2.10.0 - 2017
2+
* patternlab-node - v2.11.0 - 2017
33
*
44
* Brian Muenzenmeyer, Geoff Pursell, Raphael Okon, tburny and the web community.
55
* Licensed under the MIT license.
@@ -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 {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "patternlab-node",
33
"description": "Pattern Lab is a collection of tools to help you create atomic design systems. This is the node command line interface (CLI).",
4-
"version": "2.10.0",
4+
"version": "2.11.0",
55
"main": "./core/lib/patternlab.js",
66
"dependencies": {
77
"chalk": "^1.1.3",

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"

0 commit comments

Comments
 (0)