3
3
var path = require ( 'path' ) ,
4
4
fs = require ( 'fs-extra' ) ,
5
5
Pattern = require ( './object_factory' ) . Pattern ,
6
+ CompileState = require ( './object_factory' ) . CompileState ,
6
7
pph = require ( './pseudopattern_hunter' ) ,
7
8
mp = require ( './markdown_parser' ) ,
8
9
plutils = require ( './utilities' ) ,
@@ -123,7 +124,7 @@ var pattern_assembler = function () {
123
124
} else {
124
125
patternlab . partials [ pattern . patternPartial ] = pattern . patternDesc ;
125
126
}
126
-
127
+ patternlab . graph . add ( pattern ) ;
127
128
patternlab . patterns . push ( pattern ) ;
128
129
129
130
}
@@ -240,6 +241,30 @@ var pattern_assembler = function () {
240
241
addPattern ( pattern , patternlab ) ;
241
242
}
242
243
244
+ function checkBuildState ( pattern , patternlab ) {
245
+ //write the compiled template to the public patterns directory
246
+ var renderedTemplatePath =
247
+ patternlab . config . paths . public . patterns + pattern . getPatternLink ( patternlab , 'rendered' ) ;
248
+
249
+ if ( ! pattern . compileState ) {
250
+ pattern . compileState = CompileState . NEEDS_REBUILD ;
251
+ }
252
+
253
+ try {
254
+ // Prevent error message if file does not exist
255
+ fs . accessSync ( renderedTemplatePath , fs . F_OK ) ;
256
+ var outputLastModified = fs . statSync ( renderedTemplatePath ) . mtime . getTime ( ) ;
257
+
258
+ if ( pattern . lastModified && outputLastModified > pattern . lastModified ) {
259
+ pattern . compileState = CompileState . CLEAN ;
260
+ }
261
+ } catch ( e ) {
262
+ // Output does not exist yet, needs recompile
263
+ }
264
+ // Make the pattern known to the PatternGraph and remember its compileState
265
+ patternlab . graph . add ( pattern ) ;
266
+ }
267
+
243
268
function processPatternIterative ( relPath , patternlab ) {
244
269
245
270
var relativeDepth = ( relPath . match ( / \w (? = \\ ) | \w (? = \/ ) / g) || [ ] ) . length ;
@@ -309,7 +334,7 @@ var pattern_assembler = function () {
309
334
310
335
//see if this file has a state
311
336
setState ( currentPattern , patternlab , true ) ;
312
-
337
+ var jsonFileLastModified = null ;
313
338
//look for a json file for this template
314
339
try {
315
340
var jsonFilename = path . resolve ( patternsPath , currentPattern . subdir , currentPattern . fileName + ".json" ) ;
@@ -320,6 +345,7 @@ var pattern_assembler = function () {
320
345
}
321
346
if ( jsonFilenameStats && jsonFilenameStats . isFile ( ) ) {
322
347
currentPattern . jsonFileData = fs . readJSONSync ( jsonFilename ) ;
348
+ jsonFileLastModified = jsonFilenameStats . mtime . getTime ( ) ;
323
349
if ( patternlab . config . debug ) {
324
350
console . log ( 'processPatternIterative: found pattern-specific data.json for ' + currentPattern . patternPartial ) ;
325
351
}
@@ -355,7 +381,20 @@ var pattern_assembler = function () {
355
381
parsePatternMarkdown ( currentPattern , patternlab ) ;
356
382
357
383
//add the raw template to memory
358
- currentPattern . template = fs . readFileSync ( path . resolve ( patternsPath , relPath ) , 'utf8' ) ;
384
+ var templatePath = path . resolve ( patternsPath , currentPattern . relPath ) ;
385
+ if ( templatePath ) {
386
+ try {
387
+ var stat = fs . statSync ( templatePath ) ;
388
+ // Needs recompile whenever either the JSON or the source file has been changed
389
+ currentPattern . lastModified = Math . max ( stat . mtime . getTime ( ) , jsonFileLastModified ) ;
390
+ } catch ( e ) {
391
+ // Ignore, not a regular file
392
+ }
393
+ }
394
+
395
+ checkBuildState ( currentPattern , patternlab ) ;
396
+
397
+ currentPattern . template = fs . readFileSync ( templatePath , 'utf8' ) ;
359
398
360
399
//find any stylemodifiers that may be in the current pattern
361
400
currentPattern . stylePartials = currentPattern . findPartialsWithStyleModifiers ( ) ;
@@ -393,6 +432,15 @@ var pattern_assembler = function () {
393
432
decomposePattern ( currentPattern , patternlab ) ;
394
433
}
395
434
435
+ function findModifiedPatterns ( lastModified , patternlab ) {
436
+ return patternlab . patterns . filter ( p => {
437
+ if ( p . compileState !== CompileState . CLEAN || ! p . lastModified ) {
438
+ return true ;
439
+ }
440
+ return p . lastModified >= lastModified ;
441
+ } ) ;
442
+ }
443
+
396
444
function expandPartials ( foundPatternPartials , list_item_hunter , patternlab , currentPattern ) {
397
445
398
446
var style_modifier_hunter = new smh ( ) ,
@@ -506,6 +554,9 @@ var pattern_assembler = function () {
506
554
}
507
555
508
556
return {
557
+ find_modified_patterns : function ( lastModified , patternlab ) {
558
+ return findModifiedPatterns ( lastModified , patternlab )
559
+ } ,
509
560
find_pattern_partials : function ( pattern ) {
510
561
return pattern . findPartials ( ) ;
511
562
} ,
@@ -533,6 +584,9 @@ var pattern_assembler = function () {
533
584
renderPattern : function ( template , data , partials ) {
534
585
return renderPattern ( template , data , partials ) ;
535
586
} ,
587
+ check_build_state : function ( pattern , patternlab ) {
588
+ return checkBuildState ( pattern , patternlab ) ;
589
+ } ,
536
590
process_pattern_iterative : function ( file , patternlab ) {
537
591
return processPatternIterative ( file , patternlab ) ;
538
592
} ,
0 commit comments