@@ -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 ( 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 ( 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
0 commit comments