Skip to content

Commit

Permalink
Merge pull request #754 from pattern-lab/dev
Browse files Browse the repository at this point in the history
Pattern Lab Node v3.0.0-alpha.5
  • Loading branch information
bmuenzenmeyer authored Nov 15, 2017
2 parents 936dc3b + 4d779a5 commit da38be4
Show file tree
Hide file tree
Showing 32 changed files with 815 additions and 772 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ patternlab.serve({

```

* Read more about [configuration](https://github.com/pattern-lab/patternlab-node/wiki/Configuration) via `patternlab-config.json`.
* Read more about [configuration](http://patternlab.io/docs/advanced-config-options.html#node) via `patternlab-config.json`.

* Read more about the rest of [Public API](https://github.com/pattern-lab/patternlab-node/wiki/Public-API), and already implemented for you within [Editions](#editions).

Expand Down
35 changes: 26 additions & 9 deletions core/lib/annotation_exporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ const glob = require('glob');
const fs = require('fs-extra');
const _ = require('lodash');
const mp = require('./markdown_parser');
const logger = require('./log');

const annotations_exporter = function (pl) {

const paths = pl.config.paths;
let oldAnnotations;

/*
Returns the array of comments that used to be wrapped in raw JS.
/**
* Parses JS annotations.
* @returns array of comments that used to be wrapped in raw JS
*/
function parseAnnotationsJS() {
//attempt to read the file
try {
oldAnnotations = fs.readFileSync(path.resolve(paths.source.annotations, 'annotations.js'), 'utf8');
} catch (ex) {
if (pl.config.debug) {
console.log('annotations.js file missing from ' + paths.source.annotations + '. This may be expected.');
}
logger.debug(`annotations.js file missing from ${paths.source.annotations}. This may be expected if you do not use annotations or are using markdown.`);
return [];
}

Expand All @@ -31,13 +31,18 @@ const annotations_exporter = function (pl) {
try {
var oldAnnotationsJSON = JSON.parse(oldAnnotations);
} catch (ex) {
console.log('There was an error parsing JSON for ' + paths.source.annotations + 'annotations.js');
console.log(ex);
logger.error(`There was an error parsing JSON for ${paths.source.annotations}annotations.js`);
return [];
}
return oldAnnotationsJSON.comments;
}

/**
* Build the annotation markdown.
* @param annotationsYAML
* @param markdown_parser
* @returns annotation
*/
function buildAnnotationMD(annotationsYAML, markdown_parser) {
const annotation = {};
const markdownObj = markdown_parser.parse(annotationsYAML);
Expand All @@ -48,6 +53,11 @@ const annotations_exporter = function (pl) {
return annotation;
}

/**
* Parse markdown file annotations.
* @param annotations
* @param parser
*/
function parseMDFile(annotations, parser) {
//let annotations = annotations;
const markdown_parser = parser;
Expand All @@ -65,8 +75,10 @@ const annotations_exporter = function (pl) {
};
}

/*
Converts the *.md file yaml list into an array of annotations
/**
* Converts the *.md file yaml list into an array of annotations
*
* @returns annotations
*/
function parseAnnotationsMD() {
const markdown_parser = new mp();
Expand All @@ -77,6 +89,11 @@ const annotations_exporter = function (pl) {
return annotations;
}

/**
* Gathers JS & MD annotations.
*
* @returns array of annotations
*/
function gatherAnnotations() {
const annotationsJS = parseAnnotationsJS();
const annotationsMD = parseAnnotationsMD();
Expand Down
18 changes: 6 additions & 12 deletions core/lib/asset_copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const _ = require('lodash');
const path = require('path');
const process = require('process');
const logger = require('./log');

let copy = require('recursive-copy'); // eslint-disable-line prefer-const
let chokidar = require('chokidar'); // eslint-disable-line prefer-const
Expand Down Expand Up @@ -40,9 +41,7 @@ const asset_copier = () => {
dest,
options
).on(copy.events.COPY_FILE_COMPLETE, () => {
if (options.debug) {
console.log(`Moved ${p} to ${dest}`);
}
logger.debug(`Moved ${p} to ${dest}`);
options.emitter.emit('patternlab-asset-change', {
file: p,
dest: dest
Expand Down Expand Up @@ -70,9 +69,7 @@ const asset_copier = () => {

//if we want to watch files, do so, otherwise just copy each file
if (options.watch) {
if (patternlab.config.debug) {
console.log(`Pattern Lab is watching ${path.resolve(basePath, dir.source)} for changes`);
}
logger.info(`Pattern Lab is watching ${path.resolve(basePath, dir.source)} for changes`);

if (patternlab.watchers[key]) {
patternlab.watchers[key].close();
Expand Down Expand Up @@ -130,9 +127,7 @@ const asset_copier = () => {

_.each(globalPaths, (globalPath) => {

if (patternlab.config.debug) {
console.log(`Pattern Lab is watching ${globalPath} for changes`);
}
logger.info(`Pattern Lab is watching ${globalPath} for changes`);

if (patternlab.watchers[globalPath]) {
patternlab.watchers[globalPath].close();
Expand Down Expand Up @@ -180,9 +175,8 @@ const asset_copier = () => {
)
);
_.each(patternWatches, (patternWatchPath) => {
if (patternlab.config.debug) {
console.log(`Pattern Lab is watching ${patternWatchPath} for changes`);
}

logger.info(`Pattern Lab is watching ${patternWatchPath} for changes`);

const patternWatcher = chokidar.watch(
path.resolve(patternWatchPath),
Expand Down
6 changes: 3 additions & 3 deletions core/lib/json_copy.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"use strict";

const plutils = require('./utilities');
const logger = require('./log');
const json_copy = (data, callee) => {
try {
return JSON.parse(JSON.stringify(data));
} catch (e) {
//this is unlikely to be hit due to the passed in data already being loaded using JSON parsers
plutils.error(`JSON provided by ${callee} is invalid and cannot be copied`);
return {};
logger.warning(`JSON provided by ${callee} is invalid and cannot be copied`);
throw (e);
}
};

Expand Down
6 changes: 3 additions & 3 deletions core/lib/lineage_hunter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
const extend = require("util")._extend;
const logger = require('./log');

const lineage_hunter = function () {

Expand Down Expand Up @@ -119,9 +120,8 @@ const lineage_hunter = function () {
const patternReverseStateIndex = patternStateCascade.indexOf(lineageRPattern.patternState);
if (lineageRPattern.patternState === '' || (patternStateIndex < patternReverseStateIndex)) {

if (patternlab.config.debug) {
console.log('Found a lower common denominator pattern state: ' + pattern.patternState + ' on ' + pattern.patternPartial + '. Setting reverse lineage pattern ' + lineageRPattern.patternPartial + ' from ' + (lineageRPattern.patternState === '' ? '<<blank>>' : lineageRPattern.patternState));
}
const oldState = lineageRPattern.patternState === '' ? '<<blank>>' : lineageRPattern.patternState;
logger.info(`Found a lower common denominator pattern state: ${pattern.patternState} on ${pattern.patternPartial}. Setting reverse lineage pattern ${lineageRPattern.patternPartial} from ${oldState}`);

lineageRPattern.patternState = pattern.patternState;

Expand Down
22 changes: 10 additions & 12 deletions core/lib/list_item_hunter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const list_item_hunter = function () {
const smh = require('./style_modifier_hunter');
const jsonCopy = require('./json_copy');
const Pattern = require('./object_factory').Pattern;
const logger = require('./log');

const pattern_assembler = new pa();
const style_modifier_hunter = new smh();
Expand All @@ -19,9 +20,7 @@ const list_item_hunter = function () {
if (matches !== null) {
matches.forEach(function (liMatch) {

if (patternlab.config.debug) {
console.log('found listItem of size ' + liMatch + ' inside ' + pattern.patternPartial);
}
logger.debug(`found listItem of size ${liMatch} inside ${pattern.patternPartial}`);

//find the boundaries of the block
const loopNumberString = liMatch.split('.')[1].split('}')[0].trim();
Expand All @@ -32,9 +31,8 @@ const list_item_hunter = function () {
const repeatedBlockTemplate = [];
let repeatedBlockHtml = '';
for (let i = 0; i < items.indexOf(loopNumberString); i++) {
if (patternlab.config.debug) {
console.log('list item(s) in pattern', pattern.patternPartial, 'adding', patternBlock, 'to repeatedBlockTemplate');
}

logger.debug(`list item(s) in pattern ${pattern.patternPartial}, adding ${patternBlock} to repeatedBlockTemplate`);
repeatedBlockTemplate.push(patternBlock);
}

Expand All @@ -43,8 +41,8 @@ const list_item_hunter = function () {
try {
listData = jsonCopy(patternlab.listitems, 'config.paths.source.data listitems');
} catch (err) {
console.log('There was an error parsing JSON for ' + pattern.relPath);
console.log(err);
logger.warning(`There was an error parsing JSON for ${pattern.relPath}`);
logger.warning(err);
}

listData = _.merge(listData, pattern.listitems);
Expand All @@ -64,8 +62,8 @@ const list_item_hunter = function () {
globalData = jsonCopy(patternlab.data, 'config.paths.source.data global data');
localData = jsonCopy(pattern.jsonFileData, `${pattern.patternPartial} data`);
} catch (err) {
console.log('There was an error parsing JSON for ' + pattern.relPath);
console.log(err);
logger.warning(`There was an error parsing JSON for ${pattern.relPath}`);
logger.warning(err);
}

let allData = _.merge(globalData, localData);
Expand All @@ -89,8 +87,8 @@ const list_item_hunter = function () {
cleanPartialPattern = JSON.parse(JSON.stringify(partialPattern));
cleanPartialPattern = jsonCopy(partialPattern, `partial pattern ${partialName}`);
} catch (err) {
console.log('There was an error parsing JSON for ' + pattern.relPath);
console.log(err);
logger.warning(`There was an error parsing JSON for ${pattern.relPath}`);
logger.warning(err);
}

//if we retrieved a pattern we should make sure that its extendedTemplate is reset. looks to fix #356
Expand Down
11 changes: 10 additions & 1 deletion core/lib/utilities.js → core/lib/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,17 @@ const log = Object.assign({
*/
const debug = log.debug.bind(log);

/**
* @func info
* @desc Coloured info log
* @param {*} msg - The variadic messages to log out.
* @return {void}
*/
const info = log.info.bind(log);

/**
* @func warning
* @desc Coloured error log
* @desc Coloured warning log
* @param {*} e - The variadic messages to log out.
* @return {void}
*/
Expand Down Expand Up @@ -62,6 +70,7 @@ const reportError = function (message) {

module.exports = {
debug,
info,
warning,
error,
log,
Expand Down
5 changes: 3 additions & 2 deletions core/lib/markdown_parser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
const md = require('markdown-it')();
const yaml = require('js-yaml');
const logger = require('./log');

const markdown_parser = function () {

Expand Down Expand Up @@ -36,8 +37,8 @@ const markdown_parser = function () {
returnObject.markdown = md.render(block);
}
} catch (ex) {
console.log(ex);
console.log('error parsing markdown block', block);
logger.warning(ex);
logger.warning(`error parsing markdown block ${block}`);
return undefined;
}

Expand Down
19 changes: 7 additions & 12 deletions core/lib/parameter_hunter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const parameter_hunter = function () {
const smh = require('./style_modifier_hunter');
const style_modifier_hunter = new smh();
const pattern_assembler = new pa();
const logger = require('./log');

/**
* This function is really to accommodate the lax JSON-like syntax allowed by
Expand Down Expand Up @@ -49,10 +50,9 @@ const parameter_hunter = function () {
* * Return paramStringWellFormed.
*
* @param {string} pString
* @param {object} patternlab
* @returns {string} paramStringWellFormed
*/
function paramToJson(pString, patternlab) {
function paramToJson(pString) {
let colonPos = -1;
const keys = [];
let paramString = pString; // to not reassign param
Expand All @@ -67,10 +67,7 @@ const parameter_hunter = function () {
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...`);
}
logger.debug(`Not valid JSON found for passed pattern parameter ${pString} will attempt to parse manually...`);
}

//replace all escaped double-quotes with escaped unicode
Expand Down Expand Up @@ -256,15 +253,13 @@ const parameter_hunter = function () {
//if we retrieved a pattern we should make sure that its extendedTemplate is reset. looks to fix #190
partialPattern.extendedTemplate = partialPattern.template;

if (patternlab.config.debug) {
console.log('found patternParameters for ' + partialName);
}
logger.debug(`found patternParameters for ${partialName}`);

//strip out the additional data, convert string to JSON.
const leftParen = pMatch.indexOf('(');
const rightParen = pMatch.lastIndexOf(')');
const paramString = '{' + pMatch.substring(leftParen + 1, rightParen) + '}';
const paramStringWellFormed = paramToJson(paramString, patternlab);
const paramStringWellFormed = paramToJson(paramString);

let paramData = {};
let globalData = {};
Expand All @@ -275,8 +270,8 @@ const parameter_hunter = function () {
globalData = jsonCopy(patternlab.data, 'config.paths.source.data global data');
localData = jsonCopy(pattern.jsonFileData || {}, `pattern ${pattern.patternPartial} data`);
} catch (err) {
console.log('There was an error parsing JSON for ' + pattern.relPath);
console.log(err);
logger.warning(`There was an error parsing JSON for ${pattern.relPath}`);
logger.warning(err);
}

// resolve any pattern links that might be present
Expand Down
Loading

0 comments on commit da38be4

Please sign in to comment.