Skip to content

Commit 165afa7

Browse files
committed
feat(loadPattern): Rename pattern_assembler to loadPattern and simplify export
1 parent 3691ab2 commit 165afa7

16 files changed

+259
-283
lines changed

core/lib/lineage_hunter.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const lineage_hunter = function () {
1010
// As we are adding edges from pattern to ancestor patterns, ensure it is known to the graph
1111
patternlab.graph.add(pattern);
1212

13-
1413
//find the {{> template-name }} within patterns
1514
const matches = pattern.findPartials();
1615
if (matches !== null) {

core/lib/loadPattern.js

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
"use strict";
2+
3+
const path = require('path');
4+
5+
const Pattern = require('./object_factory').Pattern;
6+
const mp = require('./markdown_parser');
7+
const logger = require('./log');
8+
const patternEngines = require('./pattern_engines');
9+
const ch = require('./changes_hunter');
10+
const da = require('./data_loader');
11+
const addPattern = require('./addPattern');
12+
const buildListItems = require('./buildListItems');
13+
const readDocumentation = require('./readDocumentation');
14+
15+
const markdown_parser = new mp();
16+
const changes_hunter = new ch();
17+
const dataLoader = new da();
18+
19+
//this is mocked in unit tests
20+
let fs = require('fs-extra'); //eslint-disable-line prefer-const
21+
22+
// loads a pattern from disk, creates a Pattern object from it and
23+
// all its associated files, and records it in patternlab.patterns[]
24+
module.exports = function (relPath, patternlab) {
25+
26+
var relativeDepth = (relPath.match(/\w(?=\\)|\w(?=\/)/g) || []).length;
27+
if (relativeDepth > 2) {
28+
logger.warning('');
29+
logger.warning('Warning:');
30+
logger.warning('A pattern file: ' + relPath + ' was found greater than 2 levels deep from ' + patternlab.config.paths.source.patterns + '.');
31+
logger.warning('It\'s strongly suggested to not deviate from the following structure under _patterns/');
32+
logger.warning('[patternType]/[patternSubtype]/[patternName].[patternExtension]');
33+
logger.warning('');
34+
logger.warning('While Pattern Lab may still function, assets may 404 and frontend links may break. Consider yourself warned. ');
35+
logger.warning('Read More: http://patternlab.io/docs/pattern-organization.html');
36+
logger.warning('');
37+
}
38+
39+
//check if the found file is a top-level markdown file
40+
var fileObject = path.parse(relPath);
41+
if (fileObject.ext === '.md') {
42+
try {
43+
var proposedDirectory = path.resolve(patternlab.config.paths.source.patterns, fileObject.dir, fileObject.name);
44+
var proposedDirectoryStats = fs.statSync(proposedDirectory);
45+
if (proposedDirectoryStats.isDirectory()) {
46+
var subTypeMarkdownFileContents = fs.readFileSync(proposedDirectory + '.md', 'utf8');
47+
var subTypeMarkdown = markdown_parser.parse(subTypeMarkdownFileContents);
48+
var subTypePattern = new Pattern(relPath, null, patternlab);
49+
subTypePattern.patternSectionSubtype = true;
50+
subTypePattern.patternLink = subTypePattern.name + '/index.html';
51+
subTypePattern.patternDesc = subTypeMarkdown.markdown;
52+
subTypePattern.flatPatternPath = subTypePattern.flatPatternPath + '-' + subTypePattern.fileName;
53+
subTypePattern.isPattern = false;
54+
subTypePattern.engine = null;
55+
56+
patternlab.subtypePatterns[subTypePattern.patternPartial] = subTypePattern;
57+
58+
return subTypePattern;
59+
}
60+
} catch (err) {
61+
// no file exists, meaning it's a pattern markdown file
62+
if (err.code !== 'ENOENT') {
63+
logger.warning(err);
64+
}
65+
}
66+
}
67+
68+
69+
//extract some information
70+
var filename = fileObject.base;
71+
var ext = fileObject.ext;
72+
var patternsPath = patternlab.config.paths.source.patterns;
73+
74+
// skip non-pattern files
75+
if (!patternEngines.isPatternFile(filename, patternlab)) { return null; }
76+
77+
//make a new Pattern Object
78+
var currentPattern = new Pattern(relPath, null, patternlab);
79+
80+
//if file is named in the syntax for variants
81+
if (patternEngines.isPseudoPatternJSON(filename)) {
82+
return currentPattern;
83+
}
84+
85+
//can ignore all non-supported files at this point
86+
if (patternEngines.isFileExtensionSupported(ext) === false) {
87+
return currentPattern;
88+
}
89+
90+
//look for a json file for this template
91+
try {
92+
var jsonFilename = path.resolve(patternsPath, currentPattern.subdir, currentPattern.fileName);
93+
const patternData = dataLoader.loadDataFromFile(jsonFilename, fs);
94+
95+
if (patternData) {
96+
currentPattern.jsonFileData = patternData;
97+
logger.debug(`found pattern-specific data for ${currentPattern.patternPartial}`);
98+
}
99+
}
100+
catch (err) {
101+
logger.warning(`There was an error parsing sibling JSON for ${currentPattern.relPath}`);
102+
logger.warning(err);
103+
}
104+
105+
//look for a listitems.json file for this template
106+
try {
107+
var listJsonFileName = path.resolve(patternsPath, currentPattern.subdir, currentPattern.fileName + ".listitems");
108+
const listItemsData = dataLoader.loadDataFromFile(listJsonFileName, fs);
109+
110+
if (listItemsData) {
111+
logger.debug(`found pattern-specific listitems data for ${currentPattern.patternPartial}`);
112+
currentPattern.listitems = listItemsData;
113+
buildListItems(currentPattern);
114+
}
115+
}
116+
catch (err) {
117+
logger.warning(`There was an error parsing sibling listitem JSON for ${currentPattern.relPath}`);
118+
logger.warning(err);
119+
}
120+
121+
//look for a markdown file for this template
122+
readDocumentation(currentPattern, patternlab);
123+
124+
//add the raw template to memory
125+
var templatePath = path.resolve(patternsPath, currentPattern.relPath);
126+
127+
currentPattern.template = fs.readFileSync(templatePath, 'utf8');
128+
129+
//find any stylemodifiers that may be in the current pattern
130+
currentPattern.stylePartials = currentPattern.findPartialsWithStyleModifiers();
131+
132+
//find any pattern parameters that may be in the current pattern
133+
currentPattern.parameteredPartials = currentPattern.findPartialsWithPatternParameters();
134+
135+
[templatePath, jsonFilename, listJsonFileName].forEach(file => {
136+
changes_hunter.checkLastModified(currentPattern, file);
137+
});
138+
139+
changes_hunter.checkBuildState(currentPattern, patternlab);
140+
141+
//add currentPattern to patternlab.patterns array
142+
addPattern(currentPattern, patternlab);
143+
144+
return currentPattern;
145+
};
146+

core/lib/pattern_assembler.js

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

core/lib/pattern_registry.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ PatternRegistry.prototype = {
4242

4343
getPartial: function (partialName) {
4444
/*
45-
Code in here has been moved from pattern_assembler.getPartial() to prepare for some refactoring.
45+
Code in here has been moved from getPartial() to prepare for some refactoring.
4646
There are a few advantages to this method:
4747
- use a map lookup instead of interating through all patterns
4848
- get rid of dependency to the patternlab object

core/lib/patternlab.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const processIterative = require('./processIterative');
1616
const processRecursive = require('./processRecursive');
1717
const jsonCopy = require('./json_copy');
1818
const render = require('./render');
19-
const pa = require('./pattern_assembler');
19+
const loadPattern = require('./loadPattern');
2020
const sm = require('./starterkit_manager');
2121
const pe = require('./pattern_exporter');
2222
const Pattern = require('./object_factory').Pattern;
@@ -29,8 +29,6 @@ let pattern_exporter = new pe(); // eslint-disable-line
2929
let assetCopier = require('./asset_copy'); // eslint-disable-line
3030
let serve = require('./serve'); // eslint-disable-line
3131

32-
const pattern_assembler = new pa();
33-
3432
const patternEngines = require('./pattern_engines');
3533
const EventEmitter = require('events').EventEmitter;
3634

@@ -458,7 +456,7 @@ module.exports = class PatternLab {
458456
// please, if you're reading this: don't.
459457

460458
// NOTE: sync for now
461-
pattern_assembler.load_pattern_iterative(path.relative(patterns_dir, file), self);
459+
loadPattern(path.relative(patterns_dir, file), self);
462460
},
463461
resolve
464462
);

core/lib/processRecursive.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports = function (file, patternlab) {
2323
//call our helper method to actually unravel the pattern with any partials
2424
return decompose(currentPattern, patternlab)
2525
.catch(reason => {
26-
console.log(reason)
26+
console.log(reason);
2727
logger.error(reason);
2828
});
2929
};

core/lib/ui_builder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ const ui_builder = function () {
106106
* @returns the found or created pattern object
107107
*/
108108
function injectDocumentationBlock(pattern, patternlab, isSubtypePattern) {
109-
//first see if pattern_assembler processed one already
109+
//first see if loadPattern processed one already
110110
let docPattern = patternlab.subtypePatterns[pattern.patternGroup + (isSubtypePattern ? '-' + pattern.patternSubGroup : '')];
111111
if (docPattern) {
112112
docPattern.isDocPattern = true;

0 commit comments

Comments
 (0)