Skip to content

Pattern param rework #771

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Jan 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
672ea88
checking in some sloppy wip
bmuenzenmeyer Jan 12, 2018
3fe17e5
chore(logs): Clean up logs a bit
bmuenzenmeyer Jan 13, 2018
c706267
refactor(processRecursive): Use getPartial
bmuenzenmeyer Jan 13, 2018
1021702
refactor(expandPartials): Use getPartial
bmuenzenmeyer Jan 13, 2018
be3b3f7
refactor(get): Remove path normalize call
bmuenzenmeyer Jan 13, 2018
d486abf
refactor(parameter_hunter): replaceParameter helper. may still be bri…
bmuenzenmeyer Jan 13, 2018
6ea5eee
chore(lint): Fix lint issues
bmuenzenmeyer Jan 13, 2018
f0be039
fix(parameter_hunter): Replace algorithm
bmuenzenmeyer Jan 13, 2018
6fe9785
fix(unit tests): change verbose partial paths. not sure if this is a …
bmuenzenmeyer Jan 13, 2018
438c6b1
fix(package): Remove unneeded dependency
bmuenzenmeyer Jan 13, 2018
e9d6c9e
refactor(unit tests): Commit WIP pattern parameter tests
bmuenzenmeyer Jan 13, 2018
3100402
feat(patternlab): Accept additional data during build as an option
bmuenzenmeyer Jan 14, 2018
5cc8703
chore(unit tests): WIP tests
bmuenzenmeyer Jan 14, 2018
9f3c4c9
fix(unit tests): Fix more tests
bmuenzenmeyer Jan 14, 2018
4072b61
chore(unit tests): WIP
bmuenzenmeyer Jan 15, 2018
c50484d
fix(unit tests): Fix more tests. Since we no longer render, no need t…
bmuenzenmeyer Jan 15, 2018
019c6c4
fix(replaceParameter): Support unescaped replacement
bmuenzenmeyer Jan 15, 2018
13dbeaa
chore(unit tests): Test WIP and lint fixes
bmuenzenmeyer Jan 15, 2018
2f84d9c
fix(parameter_hunter): Consume styleModifiers again
bmuenzenmeyer Jan 15, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 79 additions & 76 deletions core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ const patternlab_module = function (config) {
}
}

function buildPatterns(deletePatternDir) {
function buildPatterns(deletePatternDir, additionalData) {
patternlab.events.emit('patternlab-build-pattern-start', patternlab);

//
Expand All @@ -170,9 +170,8 @@ const patternlab_module = function (config) {
//
cleanBuildDirectory(patternlab.incrementalBuildsEnabled);

patternlab.buildGlobalData();
patternlab.buildGlobalData(additionalData);

// diveSync once to perform iterative populating of patternlab object
return patternlab.processAllPatternsIterative(paths.source.patterns).then(() => {

patternlab.events.emit('patternlab-pattern-iteration-end', patternlab);
Expand All @@ -181,90 +180,94 @@ const patternlab_module = function (config) {
//we need to do this before expanding patterns & partials into extendedTemplates, otherwise we could lose the data -> partial reference
parseAllLinks(patternlab);

//diveSync again to recursively include partials, filling out the
//dive again to recursively include partials, filling out the
//extendedTemplate property of the patternlab.patterns elements
// TODO we can reduce the time needed by only processing changed patterns and their partials
patternlab.processAllPatternsRecursive(paths.source.patterns, patternlab);

//take the user defined head and foot and process any data and patterns that apply
const headPatternPromise = processMetaPattern(`_00-head.${patternlab.config.patternExtension}`, 'userHead', patternlab);
const footPatternPromise = processMetaPattern(`_01-foot.${patternlab.config.patternExtension}`, 'userFoot', patternlab);

return Promise.all([headPatternPromise, footPatternPromise]).then(() => {

//cascade any patternStates
lineage_hunter.cascade_pattern_states(patternlab);

//set pattern-specific header if necessary
let head;
if (patternlab.userHead) {
head = patternlab.userHead;
} else {
head = patternlab.header;
}

//set the pattern-specific header by compiling the general-header with data, and then adding it to the meta header
return render(Pattern.createEmpty({extendedTemplate: patternlab.header}), {
cacheBuster: patternlab.cacheBuster
}).then((results) => {
patternlab.data.patternLabHead = results;

// If deletePatternDir == true or graph needs to be updated
// rebuild all patterns
let patternsToBuild = null;

// If deletePatternDir == true or graph needs to be updated
// rebuild all patterns
patternsToBuild = null;

if (patternlab.incrementalBuildsEnabled) {
// When the graph was loaded from file, some patterns might have been moved/deleted between runs
// so the graph data become out of sync
patternlab.graph.sync().forEach(n => {
logger.info("[Deleted/Moved] " + n);
});

// TODO Find created or deleted files
const now = new Date().getTime();
markModifiedPatterns(now, patternlab);
patternsToBuild = patternlab.graph.compileOrder();
return patternlab.processAllPatternsRecursive(paths.source.patterns).then(() => {

//take the user defined head and foot and process any data and patterns that apply
const headPatternPromise = processMetaPattern(`_00-head.${patternlab.config.patternExtension}`, 'userHead', patternlab);
const footPatternPromise = processMetaPattern(`_01-foot.${patternlab.config.patternExtension}`, 'userFoot', patternlab);

return Promise.all([headPatternPromise, footPatternPromise]).then(() => {

//cascade any patternStates
lineage_hunter.cascade_pattern_states(patternlab);

//set pattern-specific header if necessary
let head;
if (patternlab.userHead) {
head = patternlab.userHead;
} else {
// build all patterns, mark all to be rebuilt
patternsToBuild = patternlab.patterns;
for (const p of patternsToBuild) {
p.compileState = CompileState.NEEDS_REBUILD;
}
head = patternlab.header;
}

//render all patterns last, so lineageR works
return patternsToBuild
.reduce((previousPromise, pattern) => {
return previousPromise.then(() => patternlab.renderSinglePattern(pattern, head));
}, Promise.resolve())
.then(() => {
// Saves the pattern graph when all files have been compiled
PatternGraph.storeToFile(patternlab);
if (patternlab.config.exportToGraphViz) {
PatternGraph.exportToDot(patternlab, "dependencyGraph.dot");
logger.info(`Exported pattern graph to ${path.join(config.paths.public.root, "dependencyGraph.dot")}`);
//set the pattern-specific header by compiling the general-header with data, and then adding it to the meta header
return render(Pattern.createEmpty({extendedTemplate: patternlab.header}), {
cacheBuster: patternlab.cacheBuster
}).then((results) => {
patternlab.data.patternLabHead = results;

// If deletePatternDir == true or graph needs to be updated
// rebuild all patterns
let patternsToBuild = null;

// If deletePatternDir == true or graph needs to be updated
// rebuild all patterns
patternsToBuild = null;

if (patternlab.incrementalBuildsEnabled) {
// When the graph was loaded from file, some patterns might have been moved/deleted between runs
// so the graph data become out of sync
patternlab.graph.sync().forEach(n => {
logger.info("[Deleted/Moved] " + n);
});

// TODO Find created or deleted files
const now = new Date().getTime();
markModifiedPatterns(now, patternlab);
patternsToBuild = patternlab.graph.compileOrder();
} else {
// build all patterns, mark all to be rebuilt
patternsToBuild = patternlab.patterns;
for (const p of patternsToBuild) {
p.compileState = CompileState.NEEDS_REBUILD;
}
}

//export patterns if necessary
pattern_exporter.export_patterns(patternlab);

}).catch(reason => {
console.log(reason);
logger.error('Error rendering patterns');
});
//render all patterns last, so lineageR works
return patternsToBuild
.reduce((previousPromise, pattern) => {
return previousPromise.then(() => patternlab.renderSinglePattern(pattern, head));
}, Promise.resolve())
.then(() => {
// Saves the pattern graph when all files have been compiled
PatternGraph.storeToFile(patternlab);
if (patternlab.config.exportToGraphViz) {
PatternGraph.exportToDot(patternlab, "dependencyGraph.dot");
logger.info(`Exported pattern graph to ${path.join(config.paths.public.root, "dependencyGraph.dot")}`);
}

//export patterns if necessary
pattern_exporter.export_patterns(patternlab);

}).catch(reason => {
console.log(reason);
logger.error('Error rendering patterns');
});

}).catch(reason => {
console.log(reason);
logger.error('Error rendering pattern lab header');
});

}).catch(reason => {
console.log(reason);
logger.error('Error rendering pattern lab header');
logger.error('Error processing meta patterns');
});

}).catch(reason => {
console.log(reason);
logger.error('Error processing meta patterns');
logger.error('Error processing patterns recursively');
});

}).catch(reason => {
Expand Down Expand Up @@ -304,7 +307,7 @@ const patternlab_module = function (config) {
return Promise.resolve();
}
patternlab.isBusy = true;
return buildPatterns(options.cleanPublic).then(() => {
return buildPatterns(options.cleanPublic, options.data).then(() => {

return new ui_builder().buildFrontend(patternlab).then(() => {

Expand Down Expand Up @@ -352,7 +355,7 @@ const patternlab_module = function (config) {
return Promise.resolve();
}
patternlab.isBusy = true;
return buildPatterns(options.cleanPublic).then(() => {
return buildPatterns(options.cleanPublic, options.data).then(() => {
patternlab.isBusy = false;
});
},
Expand Down
81 changes: 5 additions & 76 deletions core/lib/decompose.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,11 @@
const logger = require('./log');
const lh = require('./lineage_hunter');
const lih = require('./list_item_hunter');
const ph = require('./parameter_hunter');
const smh = require('./style_modifier_hunter');
const addPattern = require('./addPattern');
const jsonCopy = require('./json_copy');
const getPartial = require('./get');
const expandPartials = require('./expandPartials');

const lineage_hunter = new lh();
const list_item_hunter = new lih();
const parameter_hunter = new ph();
const style_modifier_hunter = new smh();

function expandPartials(foundPatternPartials, patternlab, currentPattern) {

// these needs to be inside the function call, unless there is a better way to handle the recursion
const processRecursive = require('./processRecursive');

logger.debug(`found partials for ${currentPattern.patternPartial}`);

// determine if the template contains any pattern parameters. if so they
// must be immediately consumed
return parameter_hunter.find_parameters(currentPattern, patternlab).then(() => {

//do something with the regular old partials
foundPatternPartials.forEach((foundPartial) => {

var partial = currentPattern.findPartial(foundPartial);
var partialPath;

//identify which pattern this partial corresponds to
for (var j = 0; j < patternlab.patterns.length; j++) {
if (patternlab.patterns[j].patternPartial === partial ||
patternlab.patterns[j].relPath.indexOf(partial) > -1)
{
partialPath = patternlab.patterns[j].relPath;
}
}

//recurse through nested partials to fill out this extended template.
return processRecursive(partialPath, patternlab).then(() => { //eslint-disable-line no-loop-func
//complete assembly of extended template
//create a copy of the partial so as to not pollute it after the getPartial call.
var partialPattern = getPartial(partial, patternlab);
var cleanPartialPattern = jsonCopy(partialPattern, `partial pattern ${partial}`);

//if partial has style modifier data, replace the styleModifier value
if (currentPattern.stylePartials && currentPattern.stylePartials.length > 0) {
style_modifier_hunter.consume_style_modifier(cleanPartialPattern, foundPartial, patternlab);
}

//this is what we came here for
logger.debug(`within ${currentPattern.patternPartial}, replacing extendedTemplate partial ${foundPartial} with ${cleanPartialPattern.patternPartial}'s extededTemplate`);
currentPattern.extendedTemplate = currentPattern.extendedTemplate.replace(foundPartial, cleanPartialPattern.extendedTemplate);
return Promise.resolve();
});
});
}).catch(reason => {
logger.error(reason);
});
}

/**
* A helper that unravels a pattern looking for partials or listitems to unravel.
Expand All @@ -73,31 +19,14 @@ function expandPartials(foundPatternPartials, patternlab, currentPattern) {
module.exports = function (pattern, patternlab, ignoreLineage) {

//set the extendedTemplate to operate on later if we find partials to replace
pattern.extendedTemplate = pattern.template;

//find how many partials there may be for the given pattern
const foundPatternPartials = pattern.findPartials();
if (!pattern.extendedTemplate) {
pattern.extendedTemplate = pattern.template;
}

//find any listItem blocks that within the pattern, even if there are no partials
const listItemPromise = list_item_hunter.process_list_item_partials(pattern, patternlab);

// expand any partials present in this pattern; that is, drill down into
// the template and replace their calls in this template with rendered
// results
let expandPartialPromise = undefined;
if (pattern.engine.expandPartials && (foundPatternPartials !== null && foundPatternPartials.length > 0)) {

// eslint-disable-next-line
expandPartialPromise = expandPartials(foundPatternPartials, patternlab, pattern).then(() => {

// update the extendedTemplate in the partials object in case this
// pattern is consumed later
patternlab.partials[pattern.patternPartial] = pattern.extendedTemplate;

});
} else {
expandPartialPromise = Promise.resolve();
}
const expandPartialPromise = expandPartials(pattern, patternlab);

let lineagePromise;

Expand Down
70 changes: 70 additions & 0 deletions core/lib/expandPartials.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"use strict";

const logger = require('./log');
const ph = require('./parameter_hunter');
const smh = require('./style_modifier_hunter');
const jsonCopy = require('./json_copy');
const getPartial = require('./get');

const parameter_hunter = new ph();
const style_modifier_hunter = new smh();

module.exports = function (currentPattern, patternlab) {

const processRecursive = require('./processRecursive');

//find how many partials there may be for the given pattern
const foundPatternPartials = currentPattern.findPartials();

// expand any partials present in this pattern; that is, drill down into
// the template and replace their calls in this template with rendered
// results
if (currentPattern.engine.expandPartials && (foundPatternPartials !== null && foundPatternPartials.length > 0)) {

logger.debug(`found partials for ${currentPattern.patternPartial}`);

// determine if the template contains any pattern parameters. if so they
// must be immediately consumed
return parameter_hunter.find_parameters(currentPattern, patternlab).then(() => {

//do something with the regular old partials
foundPatternPartials.forEach((foundPartial) => {

var partial = currentPattern.findPartial(foundPartial);
var partialPattern = getPartial(partial, patternlab);

//recurse through nested partials to fill out this extended template.
return processRecursive(partialPattern.relPath, patternlab).then(() => { //eslint-disable-line no-loop-func

//complete assembly of extended template
//create a copy of the partial so as to not pollute it after the getPartial call.
var cleanPartialPattern = jsonCopy(partialPattern, `partial pattern ${partial}`);

//if partial has style modifier data, replace the styleModifier value
if (currentPattern.stylePartials && currentPattern.stylePartials.length > 0) {
style_modifier_hunter.consume_style_modifier(cleanPartialPattern, foundPartial, patternlab);
}

//this is what we came here for
logger.debug(`within ${currentPattern.patternPartial}, replacing extendedTemplate partial ${foundPartial} with ${cleanPartialPattern.patternPartial}'s extendedTemplate`);

currentPattern.extendedTemplate = currentPattern.extendedTemplate.replace(foundPartial, cleanPartialPattern.extendedTemplate);

// update the extendedTemplate in the partials object in case this
// pattern is consumed later
patternlab.partials[currentPattern.patternPartial] = currentPattern.extendedTemplate;

return Promise.resolve();
}).catch(reason => {
console.log(reason);
logger.error(reason);
});
});

}).catch(reason => {
console.log(reason);
logger.error(reason);
});
}
return Promise.resolve();
};
Loading