Skip to content

Commit

Permalink
Merge pull request #552 from pattern-lab/dev
Browse files Browse the repository at this point in the history
Pattern Lab Node 2.6.1
  • Loading branch information
bmuenzenmeyer authored Nov 4, 2016
2 parents b048e70 + 3772f53 commit 03fcddb
Show file tree
Hide file tree
Showing 35 changed files with 3,269 additions and 3,014 deletions.
4 changes: 4 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
"node": true,
"builtin": true
},
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
},
"globals": {},
"rules": {
"block-scoped-var": 0,
Expand Down
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ language: node_js
node_js:
- node
- 6
- 5
- 4

before_install:
Expand All @@ -19,7 +18,6 @@ branches:
only:
- master
- dev
- issue/438-runAllTestsTravis

notifications:
webhooks:
Expand Down
41 changes: 0 additions & 41 deletions Gruntfile.js

This file was deleted.

15 changes: 12 additions & 3 deletions core/lib/object_factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ var patternEngines = require('./pattern_engines');
var path = require('path');
var extend = require('util')._extend;

// patternPrefixMatcher is intended to match the leading maybe-underscore,
// zero or more digits, and maybe-dash at the beginning of a pattern file name we can hack them
// off and get at the good part.
var patternPrefixMatcher = /^_?(\d+-)?/;

// Pattern properties

var Pattern = function (relPath, data, patternlab) {
Expand All @@ -22,21 +27,21 @@ var Pattern = function (relPath, data, patternlab) {
this.jsonFileData = data || {};

// strip leading "00-" from the file name and flip tildes to dashes
this.patternBaseName = this.fileName.replace(/^\d*\-/, '').replace('~', '-'); // 'colors'
this.patternBaseName = this.fileName.replace(patternPrefixMatcher, '').replace('~', '-'); // 'colors'

// Fancy name. No idea how this works. 'Colors'
this.patternName = this.patternBaseName.split('-').reduce(function (val, working) {
return val.charAt(0).toUpperCase() + val.slice(1) + ' ' + working.charAt(0).toUpperCase() + working.slice(1);
}, '').trim(); //this is the display name for the ui. strip numeric + hyphen prefixes

// the top-level pattern group this pattern belongs to. 'atoms'
this.patternGroup = this.subdir.split(path.sep)[0].replace(/^\d*-/, '');
this.patternGroup = this.subdir.split(path.sep)[0].replace(patternPrefixMatcher, '');

//00-atoms if needed
this.patternType = this.subdir.split(path.sep)[0];

// the sub-group this pattern belongs to.
this.patternSubGroup = path.basename(this.subdir).replace(/^\d*-/, ''); // 'global'
this.patternSubGroup = path.basename(this.subdir).replace(patternPrefixMatcher, ''); // 'global'

//00-colors if needed
this.patternSubType = path.basename(this.subdir);
Expand All @@ -52,6 +57,10 @@ var Pattern = function (relPath, data, patternlab) {
// name of the pattern. UPDATE: this.key is now known as this.patternPartial
this.patternPartial = this.patternGroup + '-' + this.patternBaseName;

// 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.isPattern = true;
this.isFlatPattern = this.patternGroup === this.patternSubGroup;
this.patternState = '';
Expand Down
12 changes: 5 additions & 7 deletions core/lib/pattern_assembler.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ var pattern_assembler = function () {
return patternlab.patterns[i];
}
}
if (patternlab.config.debug) {
console.error('Could not find pattern with partial ' + partialName);
}
plutils.logOrange('Could not find pattern referenced with partial syntax ' + partialName + '. This can occur when a pattern was renamed, moved, or no longer exists but it still called within a different template somewhere.');
return undefined;
}

Expand Down Expand Up @@ -442,8 +440,8 @@ var pattern_assembler = function () {
function parseDataLinksHelper(patternlab, obj, key) {
var linkRE, dataObjAsString, linkMatches;

//check for link.patternPartial
linkRE = /link\.[A-z0-9-_]+/g;
//check for 'link.patternPartial'
linkRE = /(?:'|")(link\.[A-z0-9-_]+)(?:'|")/g;

//stringify the passed in object
dataObjAsString = JSON5.stringify(obj);
Expand All @@ -458,7 +456,7 @@ var pattern_assembler = function () {
if (dataLink && dataLink.split('.').length >= 2) {

//get the partial the link refers to
var linkPatternPartial = dataLink.split('.')[1];
var linkPatternPartial = dataLink.split('.')[1].replace('"', '').replace("'", "");
var pattern = getPartial(linkPatternPartial, patternlab);
if (pattern !== undefined) {

Expand All @@ -472,7 +470,7 @@ var pattern_assembler = function () {

//also make sure our global replace didn't mess up a protocol
fullLink = fullLink.replace(/:\//g, '://');
dataObjAsString = dataObjAsString.replace(dataLink, fullLink);
dataObjAsString = dataObjAsString.replace('link.' + linkPatternPartial, fullLink);
}
} else {
if (patternlab.config.debug) {
Expand Down
7 changes: 7 additions & 0 deletions core/lib/pattern_exporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ var fs = require('fs-extra');

var pattern_exporter = function () {

/**
* Exports all pattern's final HTML as defined in patternlab-config.json to desired location.
* Originally created to help facilitate easier consumption by jekyll.
* This method is off spec with PL PHP and will change or be augmented some day.
*
* @param patternlab {object} patternlab reference
*/
function exportPatterns(patternlab) {
//read the config export options
var exportPartials = patternlab.config.patternExportPatternPartials;
Expand Down
97 changes: 71 additions & 26 deletions core/lib/patternlab.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*
* patternlab-node - v2.6.0-alpha - 2016
*
/*
* patternlab-node - v2.6.1 - 2016
*
* Brian Muenzenmeyer, Geoff Pursell, and the web community.
* Licensed under the MIT license.
*
* Many thanks to Brad Frost and Dave Olsen for inspiration, encouragement, and advice.
* Licensed under the MIT license.
*
* Many thanks to Brad Frost and Dave Olsen for inspiration, encouragement, and advice.
*
*/

Expand All @@ -17,15 +17,16 @@ var diveSync = require('diveSync'),
cleanHtml = require('js-beautify').html,
inherits = require('util').inherits,
pm = require('./plugin_manager'),
fs = require('fs-extra'),
plutils = require('./utilities');

var EventEmitter = require('events').EventEmitter;

function buildPatternData(dataFilesPath, fs) {
function buildPatternData(dataFilesPath, fsDep) {
var dataFiles = glob.sync(dataFilesPath + '*.json', {"ignore" : [dataFilesPath + 'listitems.json']});
var mergeObject = {};
dataFiles.forEach(function (filePath) {
var jsonData = fs.readJSONSync(path.resolve(filePath), 'utf8');
var jsonData = fsDep.readJSONSync(path.resolve(filePath), 'utf8');
mergeObject = _.merge(mergeObject, jsonData);
});
return mergeObject;
Expand Down Expand Up @@ -83,18 +84,42 @@ function checkConfiguration(patternlab) {
* @param patternlab - global data store
*/
function initializePlugins(patternlab) {

if (!patternlab.config.plugins) { return; }

var plugin_manager = new pm(patternlab.config, path.resolve(__dirname, '../../patternlab-config.json'));
var foundPlugins = plugin_manager.detect_plugins();

if (foundPlugins && foundPlugins.length > 0) {

for (var i = 0; i < foundPlugins.length; i++) {
var plugin = plugin_manager.load_plugin(foundPlugins[i]);

let pluginKey = foundPlugins[i];

if (patternlab.config.debug) {
console.log('Found plugin: ', pluginKey);
console.log('Attempting to load and initialize plugin.');
}

var plugin = plugin_manager.load_plugin(pluginKey);
plugin(patternlab);
}
}
}

/**
* Installs a given plugin. Assumes it has already been pulled down via npm
* @param pluginName - the name of the plugin
*/
function installPlugin(pluginName) {
//get the config
var configPath = path.resolve(process.cwd(), 'patternlab-config.json');
var config = fs.readJSONSync(path.resolve(configPath), 'utf8');
var plugin_manager = new pm(config, configPath);

plugin_manager.install_plugin(pluginName);
}

function PatternLabEventEmitter() {
EventEmitter.call(this);
}
Expand All @@ -104,7 +129,6 @@ var patternlab_engine = function (config) {
'use strict';

var JSON5 = require('json5'),
fs = require('fs-extra'),
pa = require('./pattern_assembler'),
pe = require('./pattern_exporter'),
lh = require('./lineage_hunter'),
Expand All @@ -123,7 +147,6 @@ var patternlab_engine = function (config) {

checkConfiguration(patternlab);

//todo: determine if this is the best place to wire up plugins
initializePlugins(patternlab);

var paths = patternlab.config.paths;
Expand Down Expand Up @@ -266,6 +289,37 @@ var patternlab_engine = function (config) {
}
}

function writePatternFiles(headHTML, pattern, footerHTML) {
const nullFormatter = str => str;
const defaultFormatter = codeString => cleanHtml(codeString, {indent_size: 2});
const makePath = type => path.join(paths.public.patterns, pattern.getPatternLink(patternlab, type));
const patternPage = headHTML + pattern.patternPartialCode + footerHTML;
const eng = pattern.engine;

//beautify the output if configured to do so
const formatters = config.cleanOutputHtml ? {
rendered: eng.renderedCodeFormatter || defaultFormatter,
rawTemplate: eng.rawTemplateCodeFormatter || defaultFormatter,
markupOnly: eng.markupOnlyCodeFormatter || defaultFormatter
} : {
rendered: nullFormatter,
rawTemplate: nullFormatter,
markupOnly: nullFormatter
};

//prepare the path and contents of each output file
const outputFiles = [
{ path: makePath('rendered'), content: formatters.rendered(patternPage, pattern) },
{ path: makePath('rawTemplate'), content: formatters.rawTemplate(pattern.template, pattern) },
{ path: makePath('markupOnly'), content: formatters.markupOnly(pattern.patternPartialCode, pattern) }
].concat(
eng.addOutputFiles ? eng.addOutputFiles(paths, patternlab) : []
);

//write the compiled template to the public patterns directory
outputFiles.forEach(outFile => fs.outputFileSync(outFile.path, outFile.content));
}

function buildPatterns(deletePatternDir) {

patternlab.events.emit('patternlab-build-pattern-start', patternlab);
Expand Down Expand Up @@ -357,6 +411,8 @@ var patternlab_engine = function (config) {
pattern.patternLineageRExists = pattern.lineageR.length > 0;
pattern.patternLineageEExists = pattern.patternLineageExists || pattern.patternLineageRExists;

patternlab.events.emit('patternlab-pattern-before-data-merge', patternlab, pattern);

//render the pattern, but first consolidate any data we may have
var allData;
try {
Expand Down Expand Up @@ -425,21 +481,7 @@ var patternlab_engine = function (config) {
patternlab.events.emit('patternlab-pattern-write-begin', patternlab, pattern);

//write the compiled template to the public patterns directory
var patternPage = headHTML + pattern.patternPartialCode + footerHTML;

//beautify the output if configured to do so
var cleanedPatternPage = config.cleanOutputHtml ? cleanHtml(patternPage, {indent_size: 2}) : patternPage;
var cleanedPatternPartialCode = config.cleanOutputHtml ? cleanHtml(pattern.patternPartialCode, {indent_size: 2}) : pattern.patternPartialCode;
var cleanedPatternTemplateCode = config.cleanOutputHtml ? cleanHtml(pattern.template, {indent_size: 2}) : pattern.template;

//write the compiled template to the public patterns directory
fs.outputFileSync(paths.public.patterns + pattern.getPatternLink(patternlab, 'rendered'), cleanedPatternPage);

//write the mustache file too
fs.outputFileSync(paths.public.patterns + pattern.getPatternLink(patternlab, 'rawTemplate'), cleanedPatternTemplateCode);

//write the encoded version too
fs.outputFileSync(paths.public.patterns + pattern.getPatternLink(patternlab, 'markupOnly'), cleanedPatternPartialCode);
writePatternFiles(headHTML, pattern, footerHTML);

patternlab.events.emit('patternlab-pattern-write-end', patternlab, pattern);

Expand Down Expand Up @@ -485,6 +527,9 @@ var patternlab_engine = function (config) {
},
loadstarterkit: function (starterkitName, clean) {
loadStarterKit(starterkitName, clean);
},
installplugin: function (pluginName) {
installPlugin(pluginName);
}
};
};
Expand Down
Loading

0 comments on commit 03fcddb

Please sign in to comment.