Skip to content

Commit 2764a75

Browse files
committed
feat(pattern_assembler): Further broke out files
1 parent 8c43526 commit 2764a75

12 files changed

+254
-327
lines changed

core/index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ const updateNotifier = require('update-notifier');
1818
const logger = require('./lib/log');
1919
const PatternGraph = require('./lib/pattern_graph').PatternGraph;
2020
const CompileState = require('./lib/object_factory').CompileState;
21-
const pa = require('./lib/pattern_assembler');
2221
const pe = require('./lib/pattern_exporter');
2322
const lh = require('./lib/lineage_hunter');
23+
const markModifiedPatterns = require('./lib/markModifiedPatterns');
24+
const parseAllLinks = require('./lib/parseAllLinks');
2425
const renderSync = require('./lib/renderSync');
2526

2627
const defaultConfig = require('../patternlab-config.json');
@@ -31,7 +32,6 @@ let assetCopier = require('./lib/asset_copy'); // eslint-disable-line
3132
let pattern_exporter = new pe(); // eslint-disable-line
3233
let serve = require('./lib/serve'); // eslint-disable-line
3334

34-
const pattern_assembler = new pa();
3535
const lineage_hunter = new lh();
3636

3737
//bootstrap update notifier
@@ -137,7 +137,7 @@ const patternlab_module = function (config) {
137137

138138
function cleanBuildDirectory(incrementalBuildsEnabled) {
139139
if (incrementalBuildsEnabled) {
140-
logger.log.info("Incremental builds enabled.");
140+
logger.info("Incremental builds enabled.");
141141
} else {
142142
// needs to be done BEFORE processing patterns
143143
fs.removeSync(paths.public.patterns);
@@ -154,7 +154,7 @@ const patternlab_module = function (config) {
154154
const graph = patternlab.graph = loadPatternGraph(deletePatternDir);
155155
const graphNeedsUpgrade = !PatternGraph.checkVersion(graph);
156156
if (graphNeedsUpgrade) {
157-
logger.log.info("Due to an upgrade, a complete rebuild is required and the public/patterns directory was deleted. " +
157+
logger.info("Due to an upgrade, a complete rebuild is required and the public/patterns directory was deleted. " +
158158
"Incremental build is available again on the next successful run.");
159159

160160
// Ensure that the freshly built graph has the latest version again.
@@ -178,7 +178,7 @@ const patternlab_module = function (config) {
178178

179179
//now that all the main patterns are known, look for any links that might be within data and expand them
180180
//we need to do this before expanding patterns & partials into extendedTemplates, otherwise we could lose the data -> partial reference
181-
pattern_assembler.parse_data_links(patternlab);
181+
parseAllLinks(patternlab);
182182

183183
//diveSync again to recursively include partials, filling out the
184184
//extendedTemplate property of the patternlab.patterns elements
@@ -219,12 +219,12 @@ const patternlab_module = function (config) {
219219
// When the graph was loaded from file, some patterns might have been moved/deleted between runs
220220
// so the graph data become out of sync
221221
patternlab.graph.sync().forEach(n => {
222-
logger.log.info("[Deleted/Moved] " + n);
222+
logger.info("[Deleted/Moved] " + n);
223223
});
224224

225225
// TODO Find created or deleted files
226226
const now = new Date().getTime();
227-
pattern_assembler.mark_modified_patterns(now, patternlab);
227+
markModifiedPatterns(now, patternlab);
228228
patternsToBuild = patternlab.graph.compileOrder();
229229
} else {
230230
// build all patterns, mark all to be rebuilt
@@ -244,7 +244,7 @@ const patternlab_module = function (config) {
244244
PatternGraph.storeToFile(patternlab);
245245
if (patternlab.config.exportToGraphViz) {
246246
PatternGraph.exportToDot(patternlab, "dependencyGraph.dot");
247-
logger.log.info(`Exported pattern graph to ${path.join(config.paths.public.root, "dependencyGraph.dot")}`);
247+
logger.info(`Exported pattern graph to ${path.join(config.paths.public.root, "dependencyGraph.dot")}`);
248248
}
249249

250250
//export patterns if necessary

core/lib/buildListItems.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"use strict";
2+
3+
const _ = require('lodash');
4+
5+
module.exports = function (container) {
6+
//combine all list items into one structure
7+
var list = [];
8+
for (var item in container.listitems) {
9+
if (container.listitems.hasOwnProperty(item)) {
10+
list.push(container.listitems[item]);
11+
}
12+
}
13+
container.listItemArray = _.shuffle(list);
14+
15+
for (var i = 1; i <= container.listItemArray.length; i++) {
16+
var tempItems = [];
17+
if (i === 1) {
18+
tempItems.push(container.listItemArray[0]);
19+
container.listitems['' + i ] = tempItems;
20+
} else {
21+
for (var c = 1; c <= i; c++) {
22+
tempItems.push(container.listItemArray[c - 1]);
23+
container.listitems['' + i ] = tempItems;
24+
}
25+
}
26+
}
27+
};

core/lib/markModifiedPatterns.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"use strict";
2+
3+
const path = require('path');
4+
const _ = require('lodash');
5+
6+
const CompileState = require('./object_factory').CompileState;
7+
const ch = require('./changes_hunter');
8+
const changes_hunter = new ch();
9+
10+
//this is mocked in unit tests
11+
let fs = require('fs-extra'); //eslint-disable-line prefer-const
12+
13+
/**
14+
* Finds patterns that were modified and need to be rebuilt. For clean patterns load the already
15+
* rendered markup.
16+
*
17+
* @param lastModified
18+
* @param patternlab
19+
*/
20+
module.exports = function (lastModified, patternlab) {
21+
/**
22+
* If the given array exists, apply a function to each of its elements
23+
* @param {Array} array
24+
* @param {Function} func
25+
*/
26+
const forEachExisting = (array, func) => {
27+
if (array) {
28+
array.forEach(func);
29+
}
30+
};
31+
const modifiedOrNot = _.groupBy(
32+
patternlab.patterns,
33+
p => changes_hunter.needsRebuild(lastModified, p) ? 'modified' : 'notModified');
34+
35+
// For all unmodified patterns load their rendered template output
36+
forEachExisting(modifiedOrNot.notModified, cleanPattern => {
37+
const xp = path.join(patternlab.config.paths.public.patterns, cleanPattern.getPatternLink(patternlab, 'markupOnly'));
38+
39+
// Pattern with non-existing markupOnly files were already marked for rebuild and thus are not "CLEAN"
40+
cleanPattern.patternPartialCode = fs.readFileSync(xp, 'utf8');
41+
});
42+
43+
// For all patterns that were modified, schedule them for rebuild
44+
forEachExisting(modifiedOrNot.modified, p => p.compileState = CompileState.NEEDS_REBUILD);
45+
return modifiedOrNot;
46+
};

core/lib/object_factory.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ Pattern.prototype = {
113113
render: function (data, partials) {
114114

115115
if (!this.extendedTemplate) {
116-
console.log('setting extendedTemplate because it didnt seem to be set. this is NEW CODE')
117116
this.extendedTemplate = this.template;
118117
}
119118

core/lib/parseAllLinks.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"use strict";
2+
3+
const parseLink = require('./parseLink');
4+
5+
//look for pattern links included in data files.
6+
//these will be in the form of link.* WITHOUT {{}}, which would still be there from direct pattern inclusion
7+
module.exports = function (patternlab) {
8+
//look for link.* such as link.pages-blog as a value
9+
patternlab.data = parseLink(patternlab, patternlab.data, 'data.json');
10+
11+
//loop through all patterns
12+
for (var i = 0; i < patternlab.patterns.length; i++) {
13+
patternlab.patterns[i].jsonFileData = parseLink(patternlab, patternlab.patterns[i].jsonFileData, patternlab.patterns[i].patternPartial);
14+
}
15+
};

core/lib/pattern_assembler.js

Lines changed: 1 addition & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ const path = require('path');
44
const _ = require('lodash');
55

66
const Pattern = require('./object_factory').Pattern;
7-
const CompileState = require('./object_factory').CompileState;
87
const mp = require('./markdown_parser');
98
const logger = require('./log');
109
const patternEngines = require('./pattern_engines');
1110
const ch = require('./changes_hunter');
1211
const da = require('./data_loader');
1312
const addPattern = require('./addPattern');
14-
const parseLink = require('./parseLink');
13+
const buildListItems = require('./buildListItems');
1514
const readDocumentation = require('./readDocumentation');
1615

1716
const markdown_parser = new mp();
@@ -23,30 +22,6 @@ let fs = require('fs-extra'); //eslint-disable-line prefer-const
2322

2423
const pattern_assembler = function () {
2524

26-
function buildListItems(container) {
27-
//combine all list items into one structure
28-
var list = [];
29-
for (var item in container.listitems) {
30-
if (container.listitems.hasOwnProperty(item)) {
31-
list.push(container.listitems[item]);
32-
}
33-
}
34-
container.listItemArray = _.shuffle(list);
35-
36-
for (var i = 1; i <= container.listItemArray.length; i++) {
37-
var tempItems = [];
38-
if (i === 1) {
39-
tempItems.push(container.listItemArray[0]);
40-
container.listitems['' + i ] = tempItems;
41-
} else {
42-
for (var c = 1; c <= i; c++) {
43-
tempItems.push(container.listItemArray[c - 1]);
44-
container.listitems['' + i ] = tempItems;
45-
}
46-
}
47-
}
48-
}
49-
5025
// loads a pattern from disk, creates a Pattern object from it and
5126
// all its associated files, and records it in patternlab.patterns[]
5227
function loadPatternIterative(relPath, patternlab) {
@@ -172,86 +147,9 @@ const pattern_assembler = function () {
172147
return currentPattern;
173148
}
174149

175-
/**
176-
* Finds patterns that were modified and need to be rebuilt. For clean patterns load the already
177-
* rendered markup.
178-
*
179-
* @param lastModified
180-
* @param patternlab
181-
*/
182-
function markModifiedPatterns(lastModified, patternlab) {
183-
/**
184-
* If the given array exists, apply a function to each of its elements
185-
* @param {Array} array
186-
* @param {Function} func
187-
*/
188-
const forEachExisting = (array, func) => {
189-
if (array) {
190-
array.forEach(func);
191-
}
192-
};
193-
const modifiedOrNot = _.groupBy(
194-
patternlab.patterns,
195-
p => changes_hunter.needsRebuild(lastModified, p) ? 'modified' : 'notModified');
196-
197-
// For all unmodified patterns load their rendered template output
198-
forEachExisting(modifiedOrNot.notModified, cleanPattern => {
199-
const xp = path.join(patternlab.config.paths.public.patterns, cleanPattern.getPatternLink(patternlab, 'markupOnly'));
200-
201-
// Pattern with non-existing markupOnly files were already marked for rebuild and thus are not "CLEAN"
202-
cleanPattern.patternPartialCode = fs.readFileSync(xp, 'utf8');
203-
});
204-
205-
// For all patterns that were modified, schedule them for rebuild
206-
forEachExisting(modifiedOrNot.modified, p => p.compileState = CompileState.NEEDS_REBUILD);
207-
return modifiedOrNot;
208-
}
209-
210-
//look for pattern links included in data files.
211-
//these will be in the form of link.* WITHOUT {{}}, which would still be there from direct pattern inclusion
212-
function parseDataLinks(patternlab) {
213-
//look for link.* such as link.pages-blog as a value
214-
215-
patternlab.data = parseLink(patternlab, patternlab.data, 'data.json');
216-
217-
//loop through all patterns
218-
for (var i = 0; i < patternlab.patterns.length; i++) {
219-
patternlab.patterns[i].jsonFileData = parseLink(patternlab, patternlab.patterns[i].jsonFileData, patternlab.patterns[i].patternPartial);
220-
}
221-
}
222-
223150
return {
224-
mark_modified_patterns: function (lastModified, patternlab) {
225-
return markModifiedPatterns(lastModified, patternlab);
226-
},
227-
228-
//todo review for deletion
229-
find_pattern_partials: function (pattern) {
230-
return pattern.findPartials();
231-
},
232-
233-
//todo review for deletion
234-
find_pattern_partials_with_style_modifiers: function (pattern) {
235-
return pattern.findPartialsWithStyleModifiers();
236-
},
237-
238-
//todo review for deletion
239-
find_pattern_partials_with_parameters: function (pattern) {
240-
return pattern.findPartialsWithPatternParameters();
241-
},
242-
243-
//todo review for deletion
244-
find_list_items: function (pattern) {
245-
return pattern.findListItems();
246-
},
247151
load_pattern_iterative: function (file, patternlab) {
248152
return loadPatternIterative(file, patternlab);
249-
},
250-
combine_listItems: function (patternlab) {
251-
buildListItems(patternlab);
252-
},
253-
parse_data_links: function (patternlab) {
254-
parseDataLinks(patternlab);
255153
}
256154
};
257155

core/lib/patternlab.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const cleanHtml = require('js-beautify').html;
99
const inherits = require('util').inherits;
1010
const pm = require('./plugin_manager');
1111
const packageInfo = require('../../package.json');
12+
const buildListItems = require('./buildListItems');
1213
const dataLoader = require('./data_loader')();
1314
const decompose = require('./decompose');
1415
const logger = require('./log');
@@ -182,7 +183,7 @@ module.exports = class PatternLab {
182183

183184
this.setCacheBust();
184185

185-
pattern_assembler.combine_listItems(this);
186+
buildListItems(this);
186187

187188
this.events.emit('patternlab-build-global-data-end', this);
188189
}

test/list_item_hunter_tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function createFakeListPattern(customProps) {
2525

2626
function createFakePatternLab(customProps) {
2727

28-
//NOTE: These listitems are faked so that pattern_assembler.combine_listitems has already clobbered them.
28+
//NOTE: These listitems are faked so that buildListItems has already clobbered them.
2929

3030
var pl = {
3131
graph: PatternGraph.empty(),

0 commit comments

Comments
 (0)