Skip to content

Commit 85c039a

Browse files
committed
feat(logs): Refactor logs for pattern_assembler
1 parent 5021384 commit 85c039a

File tree

1 file changed

+36
-52
lines changed

1 file changed

+36
-52
lines changed

core/lib/pattern_assembler.js

Lines changed: 36 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const Pattern = require('./object_factory').Pattern;
66
const CompileState = require('./object_factory').CompileState;
77
const pph = require('./pseudopattern_hunter');
88
const mp = require('./markdown_parser');
9-
const plutils = require('./utilities');
9+
const logger = require('./log');
1010
const patternEngines = require('./pattern_engines');
1111
const lh = require('./lineage_hunter');
1212
const lih = require('./list_item_hunter');
@@ -52,7 +52,7 @@ const pattern_assembler = function () {
5252
return patternlab.patterns[i];
5353
}
5454
}
55-
plutils.warning('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.');
55+
logger.warning('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.');
5656
return undefined;
5757
}
5858

@@ -103,9 +103,7 @@ const pattern_assembler = function () {
103103
// if the pattern is new, we must register it with various data structures!
104104
if (isNew) {
105105

106-
if (patternlab.config.debug) {
107-
console.log('found new pattern ' + pattern.patternPartial);
108-
}
106+
logger.debug(`found new pattern ${pattern.patternPartial}`);
109107

110108
// do global registration
111109
if (pattern.isPattern) {
@@ -182,20 +180,15 @@ const pattern_assembler = function () {
182180
currentPattern.links = markdownObject.links;
183181
}
184182
} else {
185-
if (patternlab.config.debug) {
186-
console.log('error processing markdown for ' + currentPattern.patternPartial);
187-
}
188-
}
189-
190-
if (patternlab.config.debug) {
191-
console.log('found pattern-specific markdown for ' + currentPattern.patternPartial);
183+
logger.warning(`error processing markdown for ${currentPattern.patternPartial}`);
192184
}
185+
logger.debug(`found pattern-specific markdown for ${currentPattern.patternPartial}`);
193186
}
194187
catch (err) {
195188
// do nothing when file not found
196189
if (err.code !== 'ENOENT') {
197-
console.log('there was an error setting pattern keys after markdown parsing of the companion file for pattern ' + currentPattern.patternPartial);
198-
console.log(err);
190+
logger.warning(`'there was an error setting pattern keys after markdown parsing of the companion file for pattern ${currentPattern.patternPartial}`)
191+
logger.warning(err);
199192
}
200193
}
201194
}
@@ -248,15 +241,15 @@ const pattern_assembler = function () {
248241

249242
var relativeDepth = (relPath.match(/\w(?=\\)|\w(?=\/)/g) || []).length;
250243
if (relativeDepth > 2) {
251-
console.log('');
252-
plutils.warning('Warning:');
253-
plutils.warning('A pattern file: ' + relPath + ' was found greater than 2 levels deep from ' + patternlab.config.paths.source.patterns + '.');
254-
plutils.warning('It\'s strongly suggested to not deviate from the following structure under _patterns/');
255-
plutils.warning('[patternType]/[patternSubtype]/[patternName].[patternExtension]');
256-
console.log('');
257-
plutils.warning('While Pattern Lab may still function, assets may 404 and frontend links may break. Consider yourself warned. ');
258-
plutils.warning('Read More: http://patternlab.io/docs/pattern-organization.html');
259-
console.log('');
244+
logger.warning('');
245+
logger.warning('Warning:');
246+
logger.warning('A pattern file: ' + relPath + ' was found greater than 2 levels deep from ' + patternlab.config.paths.source.patterns + '.');
247+
logger.warning('It\'s strongly suggested to not deviate from the following structure under _patterns/');
248+
logger.warning('[patternType]/[patternSubtype]/[patternName].[patternExtension]');
249+
logger.warning('');
250+
logger.warning('While Pattern Lab may still function, assets may 404 and frontend links may break. Consider yourself warned. ');
251+
logger.warning('Read More: http://patternlab.io/docs/pattern-organization.html');
252+
logger.warning('');
260253
}
261254

262255
//check if the found file is a top-level markdown file
@@ -282,7 +275,7 @@ const pattern_assembler = function () {
282275
} catch (err) {
283276
// no file exists, meaning it's a pattern markdown file
284277
if (err.code !== 'ENOENT') {
285-
console.log(err);
278+
logger.warning(err);
286279
}
287280
}
288281
}
@@ -312,36 +305,32 @@ const pattern_assembler = function () {
312305
//look for a json file for this template
313306
try {
314307
var jsonFilename = path.resolve(patternsPath, currentPattern.subdir, currentPattern.fileName);
315-
const configData = dataLoader.loadDataFromFile(jsonFilename, fs);
308+
const patternData = dataLoader.loadDataFromFile(jsonFilename, fs);
316309

317-
if (configData) {
318-
currentPattern.jsonFileData = configData;
319-
if (patternlab.config.debug) {
320-
console.log('processPatternIterative: found pattern-specific config data for ' + currentPattern.patternPartial);
321-
}
310+
if (patternData) {
311+
currentPattern.jsonFileData = patternData;
312+
logger.debug(`found pattern-specific data for ${currentPattern.patternPartial}`);
322313
}
323314
}
324315
catch (err) {
325-
console.log('There was an error parsing sibling JSON for ' + currentPattern.relPath);
326-
console.log(err);
316+
logger.warning(`There was an error parsing sibling JSON for ${currentPattern.relPath}`);
317+
logger.warning(err);
327318
}
328319

329320
//look for a listitems.json file for this template
330321
try {
331322
var listJsonFileName = path.resolve(patternsPath, currentPattern.subdir, currentPattern.fileName + ".listitems");
332-
const listItemsConfig = dataLoader.loadDataFromFile(listJsonFileName, fs);
323+
const listItemsData = dataLoader.loadDataFromFile(listJsonFileName, fs);
333324

334-
if (listItemsConfig) {
335-
currentPattern.listitems = listItemsConfig;
325+
if (listItemsData) {
326+
logger.debug(`found pattern-specific listitems data for ${currentPattern.patternPartial}`);
327+
currentPattern.listitems = listItemsData;
336328
buildListItems(currentPattern);
337-
if (patternlab.config.debug) {
338-
console.log('found pattern-specific listitems config for ' + currentPattern.patternPartial);
339-
}
340329
}
341330
}
342331
catch (err) {
343-
console.log('There was an error parsing sibling listitem JSON for ' + currentPattern.relPath);
344-
console.log(err);
332+
logger.warning(`There was an error parsing sibling listitem JSON for ${currentPattern.relPath}`);
333+
logger.warning(err);
345334
}
346335

347336
//look for a markdown file for this template
@@ -382,7 +371,7 @@ const pattern_assembler = function () {
382371
//find any pattern parameters that may be in the current pattern
383372
pattern.parameteredPartials = pattern.findPartialsWithPatternParameters();
384373
return pattern;
385-
}).catch(plutils.reportError('There was an error in processPatternIterative():'));
374+
}).catch(logger.reportError('There was an error in processPatternIterative():'));
386375
}
387376

388377
function processPatternRecursive(file, patternlab) {
@@ -447,9 +436,7 @@ const pattern_assembler = function () {
447436
var style_modifier_hunter = new smh(),
448437
parameter_hunter = new ph();
449438

450-
if (patternlab.config.debug) {
451-
console.log('found partials for ' + currentPattern.patternPartial);
452-
}
439+
logger.debug(`found partials for ${currentPattern.patternPartial}`);
453440

454441
// determine if the template contains any pattern parameters. if so they
455442
// must be immediately consumed
@@ -513,18 +500,15 @@ const pattern_assembler = function () {
513500
var fullLink = patternlab.data.link[linkPatternPartial];
514501
if (fullLink) {
515502
fullLink = path.normalize(fullLink).replace(/\\/g, '/');
516-
if (patternlab.config.debug) {
517-
console.log('expanded data link from ' + dataLink + ' to ' + fullLink + ' inside ' + key);
518-
}
503+
504+
logger.debug(`expanded data link from ${dataLink} to ${fullLink} inside ${key}`);
519505

520506
//also make sure our global replace didn't mess up a protocol
521507
fullLink = fullLink.replace(/:\//g, '://');
522508
dataObjAsString = dataObjAsString.replace('link.' + linkPatternPartial, fullLink);
523509
}
524510
} else {
525-
if (patternlab.config.debug) {
526-
console.log('pattern not found for', dataLink, 'inside', key);
527-
}
511+
logger.warning(`pattern not found for ${dataLink} inside ${key}`);
528512
}
529513
}
530514
}
@@ -534,8 +518,8 @@ const pattern_assembler = function () {
534518
try {
535519
dataObj = JSON.parse(dataObjAsString);
536520
} catch (err) {
537-
console.log('There was an error parsing JSON for ' + key);
538-
console.log(err);
521+
logger.warning(`There was an error parsing JSON for ${key}`);
522+
logger.warning(err);
539523
}
540524

541525
return dataObj;

0 commit comments

Comments
 (0)