@@ -438,10 +438,50 @@ var patternlab_engine = function (config) {
438
438
return true ;
439
439
}
440
440
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
+
441
459
function buildPatterns ( deletePatternDir ) {
442
460
443
461
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
+ }
445
485
446
486
try {
447
487
patternlab . data = buildPatternData ( paths . source . data , fs ) ;
@@ -511,34 +551,32 @@ var patternlab_engine = function (config) {
511
551
cacheBuster : patternlab . cacheBuster
512
552
} ) ;
513
553
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 ;
517
557
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
+ } ) ;
526
564
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 {
533
565
// TODO Find created or deleted files
534
566
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 ) ;
536
568
537
569
// First mark all modified files
538
570
for ( let p of modified ) {
539
571
p . compileState = CompileState . NEEDS_REBUILD ;
540
572
}
541
573
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
+ }
542
580
}
543
581
544
582
0 commit comments