Skip to content

Commit 03fcddb

Browse files
Merge pull request #552 from pattern-lab/dev
Pattern Lab Node 2.6.1
2 parents b048e70 + 3772f53 commit 03fcddb

35 files changed

+3269
-3014
lines changed

.eslintrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
"node": true,
44
"builtin": true
55
},
6+
"parserOptions": {
7+
"ecmaVersion": 6,
8+
"sourceType": "module"
9+
},
610
"globals": {},
711
"rules": {
812
"block-scoped-var": 0,

.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ language: node_js
33
node_js:
44
- node
55
- 6
6-
- 5
76
- 4
87

98
before_install:
@@ -19,7 +18,6 @@ branches:
1918
only:
2019
- master
2120
- dev
22-
- issue/438-runAllTestsTravis
2321

2422
notifications:
2523
webhooks:

Gruntfile.js

Lines changed: 0 additions & 41 deletions
This file was deleted.

core/lib/object_factory.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ var patternEngines = require('./pattern_engines');
44
var path = require('path');
55
var extend = require('util')._extend;
66

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

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

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

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

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

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

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

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

60+
// Let's calculate the verbose name ahead of time! We don't use path.sep here
61+
// on purpose. This isn't a file name!
62+
this.verbosePartial = this.subdir + '/' + this.fileName;
63+
5564
this.isPattern = true;
5665
this.isFlatPattern = this.patternGroup === this.patternSubGroup;
5766
this.patternState = '';

core/lib/pattern_assembler.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ var pattern_assembler = function () {
4545
return patternlab.patterns[i];
4646
}
4747
}
48-
if (patternlab.config.debug) {
49-
console.error('Could not find pattern with partial ' + partialName);
50-
}
48+
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.');
5149
return undefined;
5250
}
5351

@@ -442,8 +440,8 @@ var pattern_assembler = function () {
442440
function parseDataLinksHelper(patternlab, obj, key) {
443441
var linkRE, dataObjAsString, linkMatches;
444442

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

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

460458
//get the partial the link refers to
461-
var linkPatternPartial = dataLink.split('.')[1];
459+
var linkPatternPartial = dataLink.split('.')[1].replace('"', '').replace("'", "");
462460
var pattern = getPartial(linkPatternPartial, patternlab);
463461
if (pattern !== undefined) {
464462

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

473471
//also make sure our global replace didn't mess up a protocol
474472
fullLink = fullLink.replace(/:\//g, '://');
475-
dataObjAsString = dataObjAsString.replace(dataLink, fullLink);
473+
dataObjAsString = dataObjAsString.replace('link.' + linkPatternPartial, fullLink);
476474
}
477475
} else {
478476
if (patternlab.config.debug) {

core/lib/pattern_exporter.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ var fs = require('fs-extra');
44

55
var pattern_exporter = function () {
66

7+
/**
8+
* Exports all pattern's final HTML as defined in patternlab-config.json to desired location.
9+
* Originally created to help facilitate easier consumption by jekyll.
10+
* This method is off spec with PL PHP and will change or be augmented some day.
11+
*
12+
* @param patternlab {object} patternlab reference
13+
*/
714
function exportPatterns(patternlab) {
815
//read the config export options
916
var exportPartials = patternlab.config.patternExportPatternPartials;

core/lib/patternlab.js

Lines changed: 71 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
/*
2-
* patternlab-node - v2.6.0-alpha - 2016
3-
*
1+
/*
2+
* patternlab-node - v2.6.1 - 2016
3+
*
44
* Brian Muenzenmeyer, Geoff Pursell, and the web community.
5-
* Licensed under the MIT license.
6-
*
7-
* Many thanks to Brad Frost and Dave Olsen for inspiration, encouragement, and advice.
5+
* Licensed under the MIT license.
6+
*
7+
* Many thanks to Brad Frost and Dave Olsen for inspiration, encouragement, and advice.
88
*
99
*/
1010

@@ -17,15 +17,16 @@ var diveSync = require('diveSync'),
1717
cleanHtml = require('js-beautify').html,
1818
inherits = require('util').inherits,
1919
pm = require('./plugin_manager'),
20+
fs = require('fs-extra'),
2021
plutils = require('./utilities');
2122

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

24-
function buildPatternData(dataFilesPath, fs) {
25+
function buildPatternData(dataFilesPath, fsDep) {
2526
var dataFiles = glob.sync(dataFilesPath + '*.json', {"ignore" : [dataFilesPath + 'listitems.json']});
2627
var mergeObject = {};
2728
dataFiles.forEach(function (filePath) {
28-
var jsonData = fs.readJSONSync(path.resolve(filePath), 'utf8');
29+
var jsonData = fsDep.readJSONSync(path.resolve(filePath), 'utf8');
2930
mergeObject = _.merge(mergeObject, jsonData);
3031
});
3132
return mergeObject;
@@ -83,18 +84,42 @@ function checkConfiguration(patternlab) {
8384
* @param patternlab - global data store
8485
*/
8586
function initializePlugins(patternlab) {
87+
88+
if (!patternlab.config.plugins) { return; }
89+
8690
var plugin_manager = new pm(patternlab.config, path.resolve(__dirname, '../../patternlab-config.json'));
8791
var foundPlugins = plugin_manager.detect_plugins();
8892

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

9195
for (var i = 0; i < foundPlugins.length; i++) {
92-
var plugin = plugin_manager.load_plugin(foundPlugins[i]);
96+
97+
let pluginKey = foundPlugins[i];
98+
99+
if (patternlab.config.debug) {
100+
console.log('Found plugin: ', pluginKey);
101+
console.log('Attempting to load and initialize plugin.');
102+
}
103+
104+
var plugin = plugin_manager.load_plugin(pluginKey);
93105
plugin(patternlab);
94106
}
95107
}
96108
}
97109

110+
/**
111+
* Installs a given plugin. Assumes it has already been pulled down via npm
112+
* @param pluginName - the name of the plugin
113+
*/
114+
function installPlugin(pluginName) {
115+
//get the config
116+
var configPath = path.resolve(process.cwd(), 'patternlab-config.json');
117+
var config = fs.readJSONSync(path.resolve(configPath), 'utf8');
118+
var plugin_manager = new pm(config, configPath);
119+
120+
plugin_manager.install_plugin(pluginName);
121+
}
122+
98123
function PatternLabEventEmitter() {
99124
EventEmitter.call(this);
100125
}
@@ -104,7 +129,6 @@ var patternlab_engine = function (config) {
104129
'use strict';
105130

106131
var JSON5 = require('json5'),
107-
fs = require('fs-extra'),
108132
pa = require('./pattern_assembler'),
109133
pe = require('./pattern_exporter'),
110134
lh = require('./lineage_hunter'),
@@ -123,7 +147,6 @@ var patternlab_engine = function (config) {
123147

124148
checkConfiguration(patternlab);
125149

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

129152
var paths = patternlab.config.paths;
@@ -266,6 +289,37 @@ var patternlab_engine = function (config) {
266289
}
267290
}
268291

292+
function writePatternFiles(headHTML, pattern, footerHTML) {
293+
const nullFormatter = str => str;
294+
const defaultFormatter = codeString => cleanHtml(codeString, {indent_size: 2});
295+
const makePath = type => path.join(paths.public.patterns, pattern.getPatternLink(patternlab, type));
296+
const patternPage = headHTML + pattern.patternPartialCode + footerHTML;
297+
const eng = pattern.engine;
298+
299+
//beautify the output if configured to do so
300+
const formatters = config.cleanOutputHtml ? {
301+
rendered: eng.renderedCodeFormatter || defaultFormatter,
302+
rawTemplate: eng.rawTemplateCodeFormatter || defaultFormatter,
303+
markupOnly: eng.markupOnlyCodeFormatter || defaultFormatter
304+
} : {
305+
rendered: nullFormatter,
306+
rawTemplate: nullFormatter,
307+
markupOnly: nullFormatter
308+
};
309+
310+
//prepare the path and contents of each output file
311+
const outputFiles = [
312+
{ path: makePath('rendered'), content: formatters.rendered(patternPage, pattern) },
313+
{ path: makePath('rawTemplate'), content: formatters.rawTemplate(pattern.template, pattern) },
314+
{ path: makePath('markupOnly'), content: formatters.markupOnly(pattern.patternPartialCode, pattern) }
315+
].concat(
316+
eng.addOutputFiles ? eng.addOutputFiles(paths, patternlab) : []
317+
);
318+
319+
//write the compiled template to the public patterns directory
320+
outputFiles.forEach(outFile => fs.outputFileSync(outFile.path, outFile.content));
321+
}
322+
269323
function buildPatterns(deletePatternDir) {
270324

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

414+
patternlab.events.emit('patternlab-pattern-before-data-merge', patternlab, pattern);
415+
360416
//render the pattern, but first consolidate any data we may have
361417
var allData;
362418
try {
@@ -425,21 +481,7 @@ var patternlab_engine = function (config) {
425481
patternlab.events.emit('patternlab-pattern-write-begin', patternlab, pattern);
426482

427483
//write the compiled template to the public patterns directory
428-
var patternPage = headHTML + pattern.patternPartialCode + footerHTML;
429-
430-
//beautify the output if configured to do so
431-
var cleanedPatternPage = config.cleanOutputHtml ? cleanHtml(patternPage, {indent_size: 2}) : patternPage;
432-
var cleanedPatternPartialCode = config.cleanOutputHtml ? cleanHtml(pattern.patternPartialCode, {indent_size: 2}) : pattern.patternPartialCode;
433-
var cleanedPatternTemplateCode = config.cleanOutputHtml ? cleanHtml(pattern.template, {indent_size: 2}) : pattern.template;
434-
435-
//write the compiled template to the public patterns directory
436-
fs.outputFileSync(paths.public.patterns + pattern.getPatternLink(patternlab, 'rendered'), cleanedPatternPage);
437-
438-
//write the mustache file too
439-
fs.outputFileSync(paths.public.patterns + pattern.getPatternLink(patternlab, 'rawTemplate'), cleanedPatternTemplateCode);
440-
441-
//write the encoded version too
442-
fs.outputFileSync(paths.public.patterns + pattern.getPatternLink(patternlab, 'markupOnly'), cleanedPatternPartialCode);
484+
writePatternFiles(headHTML, pattern, footerHTML);
443485

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

@@ -485,6 +527,9 @@ var patternlab_engine = function (config) {
485527
},
486528
loadstarterkit: function (starterkitName, clean) {
487529
loadStarterKit(starterkitName, clean);
530+
},
531+
installplugin: function (pluginName) {
532+
installPlugin(pluginName);
488533
}
489534
};
490535
};

0 commit comments

Comments
 (0)