Skip to content

Commit

Permalink
Merge pull request #680 from pattern-lab/dev
Browse files Browse the repository at this point in the history
Pattern Lab Node Core 2.11.0
  • Loading branch information
bmuenzenmeyer authored Sep 11, 2017
2 parents 52e77bf + c60c2be commit fb1e031
Show file tree
Hide file tree
Showing 13 changed files with 133 additions and 38 deletions.
63 changes: 63 additions & 0 deletions core/lib/data_loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"use strict";

const glob = require('glob'),
_ = require('lodash'),
path = require('path'),
yaml = require('js-yaml');

/**
* Loads a single config file, in yaml/json format.
*
* @param dataFilesPath - leave off the file extension.
* @param fsDep
* @returns {*}
*/
function loadFile(dataFilesPath, fsDep) {
const dataFilesFullPath = dataFilesPath + '*.{json,yml,yaml}';

if (dataFilesPath) {
const dataFiles = glob.sync(dataFilesFullPath),
dataFile = _.head(dataFiles);

if (dataFile && fsDep.existsSync(path.resolve(dataFile))) {
return yaml.safeLoad(fsDep.readFileSync(path.resolve(dataFile), 'utf8'));
}
}

return null;
}

/**
* Loads a set of config files from a folder, in yaml/json format.
*
* @param dataFilesPath - leave off the file extension
* @param excludeFileNames - leave off the file extension
* @param fsDep
* @returns Object, with merged data files, empty object if no files.
*/
function loadDataFromFolder(dataFilesPath, excludeFileNames, fsDep) {
const dataFilesFullPath = dataFilesPath + '*.{json,yml,yaml}',
excludeFullPath = dataFilesPath + excludeFileNames + '.{json,yml,yaml}';

let globOptions = {};
if (excludeFileNames) {
globOptions.ignore = [excludeFullPath];
}

const dataFiles = glob.sync(dataFilesFullPath, globOptions);
let mergeObject = {};

dataFiles.forEach(function (filePath) {
let jsonData = yaml.safeLoad(fsDep.readFileSync(path.resolve(filePath), 'utf8'));
mergeObject = _.merge(mergeObject, jsonData);
});

return mergeObject;
}

module.exports = function configFileLoader() {
return {
loadDataFromFile: loadFile,
loadDataFromFolder: loadDataFromFolder
};
};
2 changes: 1 addition & 1 deletion core/lib/object_factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ var Pattern = function (relPath, data, patternlab) {

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

this.isPattern = true;
this.isFlatPattern = this.patternGroup === this.patternSubGroup;
Expand Down
16 changes: 14 additions & 2 deletions core/lib/parameter_hunter.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ var parameter_hunter = function () {
* * Return paramStringWellFormed.
*
* @param {string} pString
* @param {object} patternlab
* @returns {string} paramStringWellFormed
*/
function paramToJson(pString) {
function paramToJson(pString, patternlab) {
var colonPos = -1;
var keys = [];
var paramString = pString; // to not reassign param
Expand All @@ -62,6 +63,17 @@ var parameter_hunter = function () {
var values = [];
var wrapper;

// attempt to parse the data in case it is already well formed JSON
try {
paramStringWellFormed = JSON.stringify(JSON.parse(pString));
return paramStringWellFormed;
} catch (err) {
//todo this might be a good candidate for a different log level, should we implement that someday
if (patternlab.config.debug) {
console.log(`Not valid JSON found for passed pattern parameter ${pString} will attempt to parse manually...`);
}
}

//replace all escaped double-quotes with escaped unicode
paramString = paramString.replace(/\\"/g, '\\u0022');

Expand Down Expand Up @@ -253,7 +265,7 @@ var parameter_hunter = function () {
var leftParen = pMatch.indexOf('(');
var rightParen = pMatch.lastIndexOf(')');
var paramString = '{' + pMatch.substring(leftParen + 1, rightParen) + '}';
var paramStringWellFormed = paramToJson(paramString);
var paramStringWellFormed = paramToJson(paramString, patternlab);

var paramData = {};
var globalData = {};
Expand Down
33 changes: 14 additions & 19 deletions core/lib/pattern_assembler.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var path = require('path'),
pph = require('./pseudopattern_hunter'),
mp = require('./markdown_parser'),
plutils = require('./utilities'),
dataLoader = require('./data_loader')(),
patternEngines = require('./pattern_engines'),
lh = require('./lineage_hunter'),
lih = require('./list_item_hunter'),
Expand All @@ -34,7 +35,7 @@ var pattern_assembler = function () {
for (var i = 0; i < patternlab.patterns.length; i++) {
switch (partialName) {
case patternlab.patterns[i].relPath:
case patternlab.patterns[i].subdir + '/' + patternlab.patterns[i].fileName:
case patternlab.patterns[i].verbosePartial:
return patternlab.patterns[i];
}
}
Expand Down Expand Up @@ -324,16 +325,13 @@ var pattern_assembler = function () {

//look for a json file for this template
try {
var jsonFilename = path.resolve(patternsPath, currentPattern.subdir, currentPattern.fileName + ".json");
try {
var jsonFilenameStats = fs.statSync(jsonFilename);
} catch (err) {
//not a file
}
if (jsonFilenameStats && jsonFilenameStats.isFile()) {
currentPattern.jsonFileData = fs.readJSONSync(jsonFilename);
var jsonFilename = path.resolve(patternsPath, currentPattern.subdir, currentPattern.fileName);
let configData = dataLoader.loadDataFromFile(jsonFilename, fs);

if (configData) {
currentPattern.jsonFileData = configData;
if (patternlab.config.debug) {
console.log('processPatternIterative: found pattern-specific data.json for ' + currentPattern.patternPartial);
console.log('processPatternIterative: found pattern-specific config data for ' + currentPattern.patternPartial);
}
}
}
Expand All @@ -344,17 +342,14 @@ var pattern_assembler = function () {

//look for a listitems.json file for this template
try {
var listJsonFileName = path.resolve(patternsPath, currentPattern.subdir, currentPattern.fileName + ".listitems.json");
try {
var listJsonFileStats = fs.statSync(listJsonFileName);
} catch (err) {
//not a file
}
if (listJsonFileStats && listJsonFileStats.isFile()) {
currentPattern.listitems = fs.readJSONSync(listJsonFileName);
var listJsonFileName = path.resolve(patternsPath, currentPattern.subdir, currentPattern.fileName + ".listitems");
let listItemsConfig = dataLoader.loadDataFromFile(listJsonFileName, fs);

if (listItemsConfig) {
currentPattern.listitems = listItemsConfig;
buildListItems(currentPattern);
if (patternlab.config.debug) {
console.log('found pattern-specific listitems.json for ' + currentPattern.patternPartial);
console.log('found pattern-specific listitems config for ' + currentPattern.patternPartial);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/lib/pattern_registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ PatternRegistry.prototype = {
for (let thisPattern of patterns) {
switch (partialName) {
case thisPattern.relPath:
case thisPattern.subdir + '/' + thisPattern.fileName:
case thisPattern.verbosePartial:
return thisPattern;
}
}
Expand Down
22 changes: 11 additions & 11 deletions core/lib/patternlab.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* patternlab-node - v2.10.0 - 2017
* patternlab-node - v2.11.0 - 2017
*
* Brian Muenzenmeyer, Geoff Pursell, Raphael Okon, tburny and the web community.
* Licensed under the MIT license.
Expand All @@ -11,7 +11,6 @@
"use strict";

var diveSync = require('diveSync'),
glob = require('glob'),
_ = require('lodash'),
path = require('path'),
chalk = require('chalk'),
Expand All @@ -20,6 +19,7 @@ var diveSync = require('diveSync'),
pm = require('./plugin_manager'),
fs = require('fs-extra'),
packageInfo = require('../../package.json'),
dataLoader = require('./data_loader')(),
plutils = require('./utilities'),
jsonCopy = require('./json_copy'),
ui = require('./ui_builder'),
Expand All @@ -43,14 +43,14 @@ console.log(
var patternEngines = require('./pattern_engines');
var EventEmitter = require('events').EventEmitter;

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

// GTP: these two diveSync pattern processors factored out so they can be reused
Expand Down Expand Up @@ -504,9 +504,9 @@ var patternlab_engine = function (config) {
patternlab.data = {};
}
try {
patternlab.listitems = fs.readJSONSync(path.resolve(paths.source.data, 'listitems.json'));
patternlab.listitems = dataLoader.loadDataFromFile(path.resolve(paths.source.data, 'listitems.{json,yml,yaml}'));
} catch (ex) {
plutils.warning('WARNING: missing or malformed ' + paths.source.data + 'listitems.json file. Pattern Lab may not work without this file.');
plutils.warning('WARNING: missing or malformed ' + paths.source.data + 'listitems file. Pattern Lab may not work without this file.');
patternlab.listitems = {};
}
try {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "patternlab-node",
"description": "Pattern Lab is a collection of tools to help you create atomic design systems. This is the node command line interface (CLI).",
"version": "2.10.0",
"version": "2.11.0",
"main": "./core/lib/patternlab.js",
"dependencies": {
"chalk": "^1.1.3",
Expand Down
13 changes: 13 additions & 0 deletions test/data_loader_tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

const tap = require('tap');

tap.test('loadDataFromFile - Load ', function(test){
const fs = require('fs-extra'),
dataLoader = require('../core/lib/data_loader')(),
data_dir = './test/files/_data/';

let data = dataLoader.loadDataFromFile(data_dir + 'foo', fs);
test.equals(data.foo, 'bar');
test.end();
});
2 changes: 1 addition & 1 deletion test/files/_data/data.json
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
{ "data" : "test" }
{ "data" : "test", "from_json" : "from_json" }

1 change: 1 addition & 0 deletions test/files/_data/data.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from_yaml: "from_yaml"
1 change: 1 addition & 0 deletions test/files/_data/data.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from_yml: "from_yml"
2 changes: 2 additions & 0 deletions test/parameter_hunter_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function currentPatternClosure() {
"relPath": "02-organisms/02-comments/01-sticky-comment.mustache",
"fileName": "01-sticky-comment",
"subdir": "02-organisms/02-comments",
"verbosePartial": "02-organisms/02-comments/01-sticky-comment",
"name": "02-organisms-02-comments-01-sticky-comment",
"patternBaseName": "sticky-comment",
"patternLink": "02-organisms-02-comments-01-sticky-comment/02-organisms-02-comments-01-sticky-comment.html",
Expand All @@ -41,6 +42,7 @@ function patternlabClosure() {
"relPath": "01-molecules/06-components/02-single-comment.mustache",
"fileName": "02-single-comment",
"subdir": "01-molecules/06-components",
"verbosePartial": "01-molecules/06-components/02-single-comment",
"name": "01-molecules-06-components-02-single-comment",
"patternBaseName": "single-comment",
"patternLink": "01-molecules-06-components-02-single-comment/01-molecules-06-components-02-single-comment.html",
Expand Down
12 changes: 10 additions & 2 deletions test/patternlab_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ tap.test('buildPatterns - should replace data link even when pattern parameter p
only on the order of events within build.
*/
export_patterns: function (patternlab) {
var pattern = _.find(patternlab.patterns, (pattern) => {
return pattern.patternPartial === 'test-paramParent';
var pattern = _.find(patternlab.patterns, (p) => {
return p.patternPartial === 'test-paramParent';
});
//assert
test.equals(pattern.patternPartialCode.indexOf('00-test-00-foo.rendered.html') > -1, true, 'data link should be replaced properly');
Expand All @@ -68,6 +68,14 @@ tap.test('buildPatterns - should replace data link even when pattern parameter p
pl.build(function() {
test.end();
}, true);
});

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

let dataResult = plEngineModule.build_pattern_data(data_dir, fs);
test.equals(dataResult.from_yml, "from_yml");
test.equals(dataResult.from_yaml, "from_yaml");
test.equals(dataResult.from_json, "from_json");
test.end();
});

0 comments on commit fb1e031

Please sign in to comment.