Skip to content

Commit ecb3c46

Browse files
author
Tobias Brennecke
committed
Fixes #580 Renaming pattern causes patternlab to break and #581 Setting cleanPublic = true after incremental build does not recompile all patterns.
1 parent a136caf commit ecb3c46

File tree

2 files changed

+75
-20
lines changed

2 files changed

+75
-20
lines changed

core/lib/pattern_graph.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,21 @@ var nodeName =
5555

5656
PatternGraph.prototype = {
5757

58+
/**
59+
* Synchronizes the graph nodes with the set of all known patterns.
60+
* For instance when a pattern is deleted or moved, it might still have a node from the serialized
61+
* JSON, but there is no source pattern.
62+
*
63+
* @see {@link https://github.com/pattern-lab/patternlab-node/issues/580|Issue #580}
64+
*/
65+
sync: function () {
66+
// Remove any patterns that are in the graph data, but that haven't been discovered when
67+
// walking all patterns iteratively
68+
const nodesToRemove = this.nodes().filter(n => !this.patterns.has(n));
69+
nodesToRemove.forEach(n => this.remove(n));
70+
return nodesToRemove;
71+
},
72+
5873
/**
5974
* Creates an independent copy of the graph where nodes and edges can be modified without
6075
* affecting the source.
@@ -362,7 +377,7 @@ PatternGraph.resolveJsonGraphFile = function (patternlab, file) {
362377
PatternGraph.loadFromFile = function (patternlab, file) {
363378
const jsonGraphFile = this.resolveJsonGraphFile(patternlab, file);
364379

365-
// File is fresh, so simply constuct an empty graph in memory
380+
// File is fresh, so simply construct an empty graph in memory
366381
if (!fs.existsSync(jsonGraphFile)) {
367382
return PatternGraph.empty();
368383
}

core/lib/patternlab.js

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -438,10 +438,50 @@ var patternlab_engine = function (config) {
438438
return true;
439439
}
440440

441+
/**
442+
* If a graph was serialized and then {@code deletePatternDir == true}, there is a mismatch in the
443+
* pattern metadata and not all patterns might be recompiled.
444+
* For that reason an empty graph is returned in this case, so every pattern will be flagged as
445+
* "needs recompile". Otherwise the pattern graph is loaded from the meta data.
446+
*
447+
* @param patternlab
448+
* @param {boolean} deletePatternDir When {@code true}, an empty graph is returned
449+
* @return {PatternGraph}
450+
*/
451+
function loadPatternGraph(patternlab, deletePatternDir) {
452+
// Sanity check to prevent problems when code is refactored
453+
if (deletePatternDir) {
454+
return PatternGraph.empty();
455+
}
456+
return PatternGraph.loadFromFile(patternlab);
457+
}
458+
441459
function buildPatterns(deletePatternDir) {
442460

443461
patternlab.events.emit('patternlab-build-pattern-start', patternlab);
444-
patternlab.graph = PatternGraph.loadFromFile(patternlab);
462+
463+
let graph = patternlab.graph = loadPatternGraph(patternlab, deletePatternDir);
464+
465+
let graphNeedsUpgrade = !PatternGraph.checkVersion(graph);
466+
467+
if (graphNeedsUpgrade) {
468+
plutils.log.info("Due to an upgrade, a complete rebuild is required and the public/patterns directory was deleted. " +
469+
"Incremental build is available again on the next successful run.");
470+
471+
// Ensure that the freshly built graph has the latest version again.
472+
patternlab.graph.upgradeVersion();
473+
}
474+
475+
// Flags
476+
let incrementalBuildsEnabled = !(deletePatternDir || graphNeedsUpgrade);
477+
478+
if (incrementalBuildsEnabled) {
479+
plutils.log.info("Incremental builds enabled.")
480+
} else {
481+
// needs to be done BEFORE processing patterns
482+
fs.removeSync(paths.public.patterns);
483+
fs.emptyDirSync(paths.public.patterns);
484+
}
445485

446486
try {
447487
patternlab.data = buildPatternData(paths.source.data, fs);
@@ -511,34 +551,32 @@ var patternlab_engine = function (config) {
511551
cacheBuster: patternlab.cacheBuster
512552
});
513553

514-
let patternsToBuild = patternlab.patterns;
515-
516-
let graphNeedsUpgrade = !PatternGraph.checkVersion(patternlab.graph);
554+
// If deletePatternDir == true or graph needs to be updated
555+
// rebuild all patterns
556+
let patternsToBuild = null;
517557

518-
// Incremental builds are enabled, but we cannot use them
519-
if (!deletePatternDir && graphNeedsUpgrade) {
520-
plutils.log.info("Due to an upgrade, a complete rebuild is required. " +
521-
"Incremental build is available again on the next run.");
522-
523-
// Ensure that the freshly built graph has the latest version again.
524-
patternlab.graph.upgradeVersion();
525-
}
558+
if (incrementalBuildsEnabled) {
559+
// When the graph was loaded from file, some patterns might have been moved/deleted between runs
560+
// so the graph data become out of sync
561+
patternlab.graph.sync().forEach(n => {
562+
plutils.log.info("[Deleted/Moved] " + n)
563+
});
526564

527-
//delete the contents of config.patterns.public before writing
528-
//Also if the serialized graph must be updated
529-
if (deletePatternDir || graphNeedsUpgrade) {
530-
fs.removeSync(paths.public.patterns);
531-
fs.emptyDirSync(paths.public.patterns);
532-
} else {
533565
// TODO Find created or deleted files
534566
let now = new Date().getTime();
535-
var modified = pattern_assembler.find_modified_patterns(now, patternlab);
567+
let modified = pattern_assembler.find_modified_patterns(now, patternlab);
536568

537569
// First mark all modified files
538570
for (let p of modified) {
539571
p.compileState = CompileState.NEEDS_REBUILD;
540572
}
541573
patternsToBuild = patternlab.graph.compileOrder();
574+
} else {
575+
// build all patterns, mark all to be rebuilt
576+
patternsToBuild = patternlab.patterns;
577+
for (let p of patternsToBuild) {
578+
p.compileState = CompileState.NEEDS_REBUILD;
579+
}
542580
}
543581

544582

@@ -561,6 +599,8 @@ var patternlab_engine = function (config) {
561599
return getVersion();
562600
},
563601
build: function (callback, deletePatternDir) {
602+
console.log("console log");
603+
plutils.log.info("plutils log")
564604
if (patternlab && patternlab.isBusy) {
565605
console.log('Pattern Lab is busy building a previous run - returning early.');
566606
return;

0 commit comments

Comments
 (0)