Skip to content

Commit da38be4

Browse files
Merge pull request #754 from pattern-lab/dev
Pattern Lab Node v3.0.0-alpha.5
2 parents 936dc3b + 4d779a5 commit da38be4

32 files changed

+815
-772
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ patternlab.serve({
6969

7070
```
7171

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

7474
* 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).
7575

core/lib/annotation_exporter.js

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@ const glob = require('glob');
44
const fs = require('fs-extra');
55
const _ = require('lodash');
66
const mp = require('./markdown_parser');
7+
const logger = require('./log');
78

89
const annotations_exporter = function (pl) {
910

1011
const paths = pl.config.paths;
1112
let oldAnnotations;
1213

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

@@ -31,13 +31,18 @@ const annotations_exporter = function (pl) {
3131
try {
3232
var oldAnnotationsJSON = JSON.parse(oldAnnotations);
3333
} catch (ex) {
34-
console.log('There was an error parsing JSON for ' + paths.source.annotations + 'annotations.js');
35-
console.log(ex);
34+
logger.error(`There was an error parsing JSON for ${paths.source.annotations}annotations.js`);
3635
return [];
3736
}
3837
return oldAnnotationsJSON.comments;
3938
}
4039

40+
/**
41+
* Build the annotation markdown.
42+
* @param annotationsYAML
43+
* @param markdown_parser
44+
* @returns annotation
45+
*/
4146
function buildAnnotationMD(annotationsYAML, markdown_parser) {
4247
const annotation = {};
4348
const markdownObj = markdown_parser.parse(annotationsYAML);
@@ -48,6 +53,11 @@ const annotations_exporter = function (pl) {
4853
return annotation;
4954
}
5055

56+
/**
57+
* Parse markdown file annotations.
58+
* @param annotations
59+
* @param parser
60+
*/
5161
function parseMDFile(annotations, parser) {
5262
//let annotations = annotations;
5363
const markdown_parser = parser;
@@ -65,8 +75,10 @@ const annotations_exporter = function (pl) {
6575
};
6676
}
6777

68-
/*
69-
Converts the *.md file yaml list into an array of annotations
78+
/**
79+
* Converts the *.md file yaml list into an array of annotations
80+
*
81+
* @returns annotations
7082
*/
7183
function parseAnnotationsMD() {
7284
const markdown_parser = new mp();
@@ -77,6 +89,11 @@ const annotations_exporter = function (pl) {
7789
return annotations;
7890
}
7991

92+
/**
93+
* Gathers JS & MD annotations.
94+
*
95+
* @returns array of annotations
96+
*/
8097
function gatherAnnotations() {
8198
const annotationsJS = parseAnnotationsJS();
8299
const annotationsMD = parseAnnotationsMD();

core/lib/asset_copy.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const _ = require('lodash');
33
const path = require('path');
44
const process = require('process');
5+
const logger = require('./log');
56

67
let copy = require('recursive-copy'); // eslint-disable-line prefer-const
78
let chokidar = require('chokidar'); // eslint-disable-line prefer-const
@@ -40,9 +41,7 @@ const asset_copier = () => {
4041
dest,
4142
options
4243
).on(copy.events.COPY_FILE_COMPLETE, () => {
43-
if (options.debug) {
44-
console.log(`Moved ${p} to ${dest}`);
45-
}
44+
logger.debug(`Moved ${p} to ${dest}`);
4645
options.emitter.emit('patternlab-asset-change', {
4746
file: p,
4847
dest: dest
@@ -70,9 +69,7 @@ const asset_copier = () => {
7069

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

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

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

133-
if (patternlab.config.debug) {
134-
console.log(`Pattern Lab is watching ${globalPath} for changes`);
135-
}
130+
logger.info(`Pattern Lab is watching ${globalPath} for changes`);
136131

137132
if (patternlab.watchers[globalPath]) {
138133
patternlab.watchers[globalPath].close();
@@ -180,9 +175,8 @@ const asset_copier = () => {
180175
)
181176
);
182177
_.each(patternWatches, (patternWatchPath) => {
183-
if (patternlab.config.debug) {
184-
console.log(`Pattern Lab is watching ${patternWatchPath} for changes`);
185-
}
178+
179+
logger.info(`Pattern Lab is watching ${patternWatchPath} for changes`);
186180

187181
const patternWatcher = chokidar.watch(
188182
path.resolve(patternWatchPath),

core/lib/json_copy.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
"use strict";
22

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

core/lib/lineage_hunter.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22
const extend = require("util")._extend;
3+
const logger = require('./log');
34

45
const lineage_hunter = function () {
56

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

122-
if (patternlab.config.debug) {
123-
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));
124-
}
123+
const oldState = lineageRPattern.patternState === '' ? '<<blank>>' : lineageRPattern.patternState;
124+
logger.info(`Found a lower common denominator pattern state: ${pattern.patternState} on ${pattern.patternPartial}. Setting reverse lineage pattern ${lineageRPattern.patternPartial} from ${oldState}`);
125125

126126
lineageRPattern.patternState = pattern.patternState;
127127

core/lib/list_item_hunter.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const list_item_hunter = function () {
77
const smh = require('./style_modifier_hunter');
88
const jsonCopy = require('./json_copy');
99
const Pattern = require('./object_factory').Pattern;
10+
const logger = require('./log');
1011

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

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

2625
//find the boundaries of the block
2726
const loopNumberString = liMatch.split('.')[1].split('}')[0].trim();
@@ -32,9 +31,8 @@ const list_item_hunter = function () {
3231
const repeatedBlockTemplate = [];
3332
let repeatedBlockHtml = '';
3433
for (let i = 0; i < items.indexOf(loopNumberString); i++) {
35-
if (patternlab.config.debug) {
36-
console.log('list item(s) in pattern', pattern.patternPartial, 'adding', patternBlock, 'to repeatedBlockTemplate');
37-
}
34+
35+
logger.debug(`list item(s) in pattern ${pattern.patternPartial}, adding ${patternBlock} to repeatedBlockTemplate`);
3836
repeatedBlockTemplate.push(patternBlock);
3937
}
4038

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

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

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

9694
//if we retrieved a pattern we should make sure that its extendedTemplate is reset. looks to fix #356

core/lib/utilities.js renamed to core/lib/log.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,17 @@ const log = Object.assign({
3131
*/
3232
const debug = log.debug.bind(log);
3333

34+
/**
35+
* @func info
36+
* @desc Coloured info log
37+
* @param {*} msg - The variadic messages to log out.
38+
* @return {void}
39+
*/
40+
const info = log.info.bind(log);
41+
3442
/**
3543
* @func warning
36-
* @desc Coloured error log
44+
* @desc Coloured warning log
3745
* @param {*} e - The variadic messages to log out.
3846
* @return {void}
3947
*/
@@ -62,6 +70,7 @@ const reportError = function (message) {
6270

6371
module.exports = {
6472
debug,
73+
info,
6574
warning,
6675
error,
6776
log,

core/lib/markdown_parser.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22
const md = require('markdown-it')();
33
const yaml = require('js-yaml');
4+
const logger = require('./log');
45

56
const markdown_parser = function () {
67

@@ -36,8 +37,8 @@ const markdown_parser = function () {
3637
returnObject.markdown = md.render(block);
3738
}
3839
} catch (ex) {
39-
console.log(ex);
40-
console.log('error parsing markdown block', block);
40+
logger.warning(ex);
41+
logger.warning(`error parsing markdown block ${block}`);
4142
return undefined;
4243
}
4344

core/lib/parameter_hunter.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const parameter_hunter = function () {
88
const smh = require('./style_modifier_hunter');
99
const style_modifier_hunter = new smh();
1010
const pattern_assembler = new pa();
11+
const logger = require('./log');
1112

1213
/**
1314
* This function is really to accommodate the lax JSON-like syntax allowed by
@@ -49,10 +50,9 @@ const parameter_hunter = function () {
4950
* * Return paramStringWellFormed.
5051
*
5152
* @param {string} pString
52-
* @param {object} patternlab
5353
* @returns {string} paramStringWellFormed
5454
*/
55-
function paramToJson(pString, patternlab) {
55+
function paramToJson(pString) {
5656
let colonPos = -1;
5757
const keys = [];
5858
let paramString = pString; // to not reassign param
@@ -67,10 +67,7 @@ const parameter_hunter = function () {
6767
paramStringWellFormed = JSON.stringify(JSON.parse(pString));
6868
return paramStringWellFormed;
6969
} catch (err) {
70-
//todo this might be a good candidate for a different log level, should we implement that someday
71-
if (patternlab.config.debug) {
72-
console.log(`Not valid JSON found for passed pattern parameter ${pString} will attempt to parse manually...`);
73-
}
70+
logger.debug(`Not valid JSON found for passed pattern parameter ${pString} will attempt to parse manually...`);
7471
}
7572

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

259-
if (patternlab.config.debug) {
260-
console.log('found patternParameters for ' + partialName);
261-
}
256+
logger.debug(`found patternParameters for ${partialName}`);
262257

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

269264
let paramData = {};
270265
let globalData = {};
@@ -275,8 +270,8 @@ const parameter_hunter = function () {
275270
globalData = jsonCopy(patternlab.data, 'config.paths.source.data global data');
276271
localData = jsonCopy(pattern.jsonFileData || {}, `pattern ${pattern.patternPartial} data`);
277272
} catch (err) {
278-
console.log('There was an error parsing JSON for ' + pattern.relPath);
279-
console.log(err);
273+
logger.warning(`There was an error parsing JSON for ${pattern.relPath}`);
274+
logger.warning(err);
280275
}
281276

282277
// resolve any pattern links that might be present

0 commit comments

Comments
 (0)