Skip to content

Commit

Permalink
WIP deferred engine load!
Browse files Browse the repository at this point in the history
part of #610
part of the merge of #608

most unit tests working, only a few left - due to me hanging a property off the engine that @geoffp was not doing.
still need to see how it behaves during actual builds
  • Loading branch information
bmuenzenmeyer committed Feb 7, 2017
1 parent 457ed48 commit e6cf3b1
Show file tree
Hide file tree
Showing 31 changed files with 516 additions and 441 deletions.
6 changes: 5 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@
"space-infix-ops": 1,
"valid-typeof": 2,
"vars-on-top": 0,
"wrap-iife": [2, "inside"]
"wrap-iife": [2, "inside"],
"prefer-const": ["error", {
"destructuring": "any",
"ignoreReadBeforeAssign": false
}]
}
}
48 changes: 24 additions & 24 deletions core/lib/annotation_exporter.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
"use strict";
'use strict';
const path = require('path');
const glob = require('glob');
const fs = require('fs-extra');
const JSON5 = require('json5');
const _ = require('lodash');
const mp = require('./markdown_parser');

var path = require('path'),
glob = require('glob'),
fs = require('fs-extra'),
JSON5 = require('json5'),
_ = require('lodash'),
mp = require('./markdown_parser');
const annotations_exporter = function (pl) {

var annotations_exporter = function (pl) {

var paths = pl.config.paths;
const paths = pl.config.paths;
let oldAnnotations;

/*
Returns the array of comments that used to be wrapped in raw JS.
*/
function parseAnnotationsJS() {
//attempt to read the file
try {
var oldAnnotations = fs.readFileSync(path.resolve(paths.source.annotations, 'annotations.js'), 'utf8');
oldAnnotations = fs.readFileSync(path.resolve(paths.source.annotations, 'annotations.js'), 'utf8');
} catch (ex) {
if (pl.config.debug) {
console.log('annotations.js file missing from ' + paths.source.annotations + '. This may be expected.');
Expand All @@ -40,8 +40,8 @@ var annotations_exporter = function (pl) {
}

function buildAnnotationMD(annotationsYAML, markdown_parser) {
var annotation = {};
var markdownObj = markdown_parser.parse(annotationsYAML);
const annotation = {};
const markdownObj = markdown_parser.parse(annotationsYAML);

annotation.el = markdownObj.el || markdownObj.selector;
annotation.title = markdownObj.title;
Expand All @@ -50,16 +50,16 @@ var annotations_exporter = function (pl) {
}

function parseMDFile(annotations, parser) {
var annotations = annotations;
var markdown_parser = parser;
//let annotations = annotations;
const markdown_parser = parser;

return function (filePath) {
var annotationsMD = fs.readFileSync(path.resolve(filePath), 'utf8');
const annotationsMD = fs.readFileSync(path.resolve(filePath), 'utf8');

//take the annotation snippets and split them on our custom delimiter
var annotationsYAML = annotationsMD.split('~*~');
for (var i = 0; i < annotationsYAML.length; i++) {
var annotation = buildAnnotationMD(annotationsYAML[i], markdown_parser);
const annotationsYAML = annotationsMD.split('~*~');
for (let i = 0; i < annotationsYAML.length; i++) {
const annotation = buildAnnotationMD(annotationsYAML[i], markdown_parser);
annotations.push(annotation);
}
return false;
Expand All @@ -70,17 +70,17 @@ var annotations_exporter = function (pl) {
Converts the *.md file yaml list into an array of annotations
*/
function parseAnnotationsMD() {
var markdown_parser = new mp();
var annotations = [];
var mdFiles = glob.sync(paths.source.annotations + '/*.md');
const markdown_parser = new mp();
const annotations = [];
const mdFiles = glob.sync(paths.source.annotations + '/*.md');

mdFiles.forEach(parseMDFile(annotations, markdown_parser));
return annotations;
}

function gatherAnnotations() {
var annotationsJS = parseAnnotationsJS();
var annotationsMD = parseAnnotationsMD();
const annotationsJS = parseAnnotationsJS();
const annotationsMD = parseAnnotationsMD();
return _.unionBy(annotationsJS, annotationsMD, 'el');
}

Expand Down
18 changes: 9 additions & 9 deletions core/lib/changes_hunter.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"use strict";
const fs = require("fs-extra"),
CompileState = require('./object_factory').CompileState;
'use strict';
const fs = require("fs-extra");
const CompileState = require('./object_factory').CompileState;

/**
* For detecting changed patterns.
* @constructor
*/
let ChangesHunter = function () {
const ChangesHunter = function () {
};

ChangesHunter.prototype = {
Expand All @@ -24,11 +24,11 @@ ChangesHunter.prototype = {
checkBuildState: function (pattern, patternlab) {

//write the compiled template to the public patterns directory
let renderedTemplatePath =
const renderedTemplatePath =
patternlab.config.paths.public.patterns + pattern.getPatternLink(patternlab, 'rendered');

//write the compiled template to the public patterns directory
let markupOnlyPath =
const markupOnlyPath =
patternlab.config.paths.public.patterns + pattern.getPatternLink(patternlab, 'markupOnly');

if (!pattern.compileState) {
Expand All @@ -45,7 +45,7 @@ ChangesHunter.prototype = {
// Prevent error message if file does not exist
fs.accessSync(renderedFile, fs.F_OK)
);
let outputLastModified = fs.statSync(renderedTemplatePath).mtime.getTime();
const outputLastModified = fs.statSync(renderedTemplatePath).mtime.getTime();

if (pattern.lastModified && outputLastModified > pattern.lastModified) {
pattern.compileState = CompileState.CLEAN;
Expand All @@ -55,7 +55,7 @@ ChangesHunter.prototype = {
pattern.compileState = CompileState.NEEDS_REBUILD;
}

let node = patternlab.graph.node(pattern);
const node = patternlab.graph.node(pattern);

// Make the pattern known to the PatternGraph and remember its compileState
if (!node) {
Expand All @@ -78,7 +78,7 @@ ChangesHunter.prototype = {
checkLastModified: function (currentPattern, file) {
if (file) {
try {
let stat = fs.statSync(file);
const stat = fs.statSync(file);

// Needs recompile whenever one of the patterns files (template, json, pseudopatterns) changed
currentPattern.lastModified =
Expand Down
46 changes: 23 additions & 23 deletions core/lib/lineage_hunter.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
"use strict";
'use strict';
const extend = require("util")._extend;

var extend = require("util")._extend;
const lineage_hunter = function () {

var lineage_hunter = function () {
function findlineage(pattern, patternlab) {

var pa = require('./pattern_assembler');
const pa = require('./pattern_assembler');
const pattern_assembler = new pa();

function findlineage(pattern, patternlab) {
// As we are adding edges from pattern to ancestor patterns, ensure it is known to the graph
patternlab.graph.add(pattern);

var pattern_assembler = new pa();

//find the {{> template-name }} within patterns
var matches = pattern.findPartials();
const matches = pattern.findPartials();
if (matches !== null) {
matches.forEach(function (match) {
//get the ancestorPattern
var ancestorPattern = pattern_assembler.getPartial(pattern.findPartial(match), patternlab);
const ancestorPattern = pattern_assembler.getPartial(pattern.findPartial(match), patternlab);

if (ancestorPattern && pattern.lineageIndex.indexOf(ancestorPattern.patternPartial) === -1) {
//add it since it didnt exist
pattern.lineageIndex.push(ancestorPattern.patternPartial);

//create the more complex patternLineage object too
var l = {
const l = {
"lineagePattern": ancestorPattern.patternPartial,
"lineagePath": "../../patterns/" + ancestorPattern.patternLink
};
Expand All @@ -44,7 +44,7 @@ var lineage_hunter = function () {
ancestorPattern.lineageRIndex.push(pattern.patternPartial);

//create the more complex patternLineage object in reverse
var lr = {
const lr = {
"lineagePattern": pattern.patternPartial,
"lineagePath": "../../patterns/" + pattern.patternLink
};
Expand All @@ -69,15 +69,15 @@ var lineage_hunter = function () {
* @param graph {PatternGraph}
*/
function setPatternState(direction, pattern, targetPattern, graph) {
var index = null;
let index = null;
if (direction === 'fromPast') {
index = graph.lineage(pattern);
} else {
index = graph.lineageR(pattern);
}

// if the request came from the past, apply target pattern state to current pattern lineage
for (var i = 0; i < index.length; i++) {
for (let i = 0; i < index.length; i++) {
if (index[i].patternPartial === targetPattern.patternPartial) {
index[i].lineageState = targetPattern.patternState;
}
Expand All @@ -87,36 +87,36 @@ var lineage_hunter = function () {

function cascadePatternStates(patternlab) {

for (var i = 0; i < patternlab.patterns.length; i++) {
var pattern = patternlab.patterns[i];
for (let i = 0; i < patternlab.patterns.length; i++) {
const pattern = patternlab.patterns[i];

//for each pattern with a defined state
if (pattern.patternState) {
var lineage = patternlab.graph.lineage(pattern);
const lineage = patternlab.graph.lineage(pattern);

if (lineage && lineage.length > 0) {

//find all lineage - patterns being consumed by this one
for (var h = 0; h < lineage.length; h++) {
for (let h = 0; h < lineage.length; h++) {
// Not needed, the graph already knows the concrete pattern
// var lineagePattern = pattern_assembler.getPartial(lineageIndex[h], patternlab);
// let lineagePattern = pattern_assembler.getPartial(lineageIndex[h], patternlab);
setPatternState('fromFuture', lineage[h], pattern, patternlab.graph);
}
}
var lineageR = patternlab.graph.lineageR(pattern);
const lineageR = patternlab.graph.lineageR(pattern);
if (lineageR && lineageR.length > 0) {

//find all reverse lineage - that is, patterns consuming this one
for (var j = 0; j < lineageR.length; j++) {
for (let j = 0; j < lineageR.length; j++) {

var lineageRPattern = lineageR[j];
const lineageRPattern = lineageR[j];

//only set patternState if pattern.patternState "is less than" the lineageRPattern.patternstate
//or if lineageRPattern.patternstate (the consuming pattern) does not have a state
//this makes patternlab apply the lowest common ancestor denominator
let patternStateCascade = patternlab.config.patternStateCascade;
let patternStateIndex = patternStateCascade.indexOf(pattern.patternState);
let patternReverseStateIndex = patternStateCascade.indexOf(lineageRPattern.patternState);
const patternStateCascade = patternlab.config.patternStateCascade;
const patternStateIndex = patternStateCascade.indexOf(pattern.patternState);
const patternReverseStateIndex = patternStateCascade.indexOf(lineageRPattern.patternState);
if (lineageRPattern.patternState === '' || (patternStateIndex < patternReverseStateIndex)) {

if (patternlab.config.debug) {
Expand Down
Loading

1 comment on commit e6cf3b1

@geoffp
Copy link
Contributor

@geoffp geoffp commented on e6cf3b1 Feb 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, looking good! This is pretty much exactly what I pictured.

Please sign in to comment.