Skip to content

Commit 1e57ccd

Browse files
author
Tobias Brennecke
committed
#540 Move lastModified checks to new changes_hunter, fix bug with always recompiling all pseudopatterns
1 parent a920aea commit 1e57ccd

File tree

3 files changed

+79
-41
lines changed

3 files changed

+79
-41
lines changed

core/lib/changes_hunter.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"use strict";
2+
const fs = require("fs-extra"),
3+
CompileState = require('./object_factory').CompileState;
4+
5+
6+
7+
let changesHunter = function() {
8+
};
9+
10+
changesHunter.prototype = {
11+
checkBuildState: function (pattern, patternlab) {
12+
//write the compiled template to the public patterns directory
13+
var renderedTemplatePath =
14+
patternlab.config.paths.public.patterns + pattern.getPatternLink(patternlab, 'rendered');
15+
16+
if (!pattern.compileState) {
17+
pattern.compileState = CompileState.NEEDS_REBUILD;
18+
}
19+
20+
try {
21+
// Prevent error message if file does not exist
22+
fs.accessSync(renderedTemplatePath, fs.F_OK);
23+
var outputLastModified = fs.statSync(renderedTemplatePath).mtime.getTime();
24+
25+
if (pattern.lastModified && outputLastModified > pattern.lastModified) {
26+
pattern.compileState = CompileState.CLEAN;
27+
}
28+
} catch (e) {
29+
// Output does not exist yet, needs recompile
30+
}
31+
32+
let node = patternlab.graph.node(pattern);
33+
34+
// Make the pattern known to the PatternGraph and remember its compileState
35+
if (!node) {
36+
patternlab.graph.add(pattern);
37+
} else {
38+
// Works via object reference, so we directly manipulate the node data here
39+
node.compileState = pattern.compileState;
40+
}
41+
42+
43+
},
44+
45+
/**
46+
* Updates {Pattern#lastModified} to the files modification date if the file was modified
47+
* after {Pattern#lastModified}.
48+
* @param {Pattern} currentPattern
49+
* @param {string} file
50+
*/
51+
checkLastModified: function (currentPattern, file) {
52+
if (file) {
53+
try {
54+
let stat = fs.statSync(file);
55+
// Needs recompile whenever one of the patterns files (template, json, pseudopatterns) changed
56+
currentPattern.lastModified =
57+
Math.max(stat.mtime.getTime(), currentPattern.lastModified || 0);
58+
} catch (e) {
59+
// Ignore, not a regular file
60+
}
61+
}
62+
}
63+
};
64+
65+
module.exports = changesHunter;

core/lib/pattern_assembler.js

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ var path = require('path'),
1212
lih = require('./list_item_hunter'),
1313
smh = require('./style_modifier_hunter'),
1414
ph = require('./parameter_hunter'),
15+
ch = require('./changes_hunter'),
1516
JSON5 = require('json5');
1617

1718
var markdown_parser = new mp();
19+
var changes_hunter = new ch();
1820

1921
var pattern_assembler = function () {
2022
// HELPER FUNCTIONS
@@ -241,30 +243,6 @@ var pattern_assembler = function () {
241243
addPattern(pattern, patternlab);
242244
}
243245

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-
268246
function processPatternIterative(relPath, patternlab) {
269247

270248
var relativeDepth = (relPath.match(/\w(?=\\)|\w(?=\/)/g) || []).length;
@@ -334,7 +312,7 @@ var pattern_assembler = function () {
334312

335313
//see if this file has a state
336314
setState(currentPattern, patternlab, true);
337-
var jsonFileLastModified = null;
315+
338316
//look for a json file for this template
339317
try {
340318
var jsonFilename = path.resolve(patternsPath, currentPattern.subdir, currentPattern.fileName + ".json");
@@ -345,7 +323,6 @@ var pattern_assembler = function () {
345323
}
346324
if (jsonFilenameStats && jsonFilenameStats.isFile()) {
347325
currentPattern.jsonFileData = fs.readJSONSync(jsonFilename);
348-
jsonFileLastModified = jsonFilenameStats.mtime.getTime();
349326
if (patternlab.config.debug) {
350327
console.log('processPatternIterative: found pattern-specific data.json for ' + currentPattern.patternPartial);
351328
}
@@ -382,17 +359,6 @@ var pattern_assembler = function () {
382359

383360
//add the raw template to memory
384361
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);
396362

397363
currentPattern.template = fs.readFileSync(templatePath, 'utf8');
398364

@@ -402,6 +368,12 @@ var pattern_assembler = function () {
402368
//find any pattern parameters that may be in the current pattern
403369
currentPattern.parameteredPartials = currentPattern.findPartialsWithPatternParameters();
404370

371+
[templatePath, jsonFilename, listJsonFileName].forEach(file => {
372+
changes_hunter.checkLastModified(currentPattern, file)
373+
});
374+
375+
changes_hunter.checkBuildState(currentPattern, patternlab);
376+
405377
//add currentPattern to patternlab.patterns array
406378
addPattern(currentPattern, patternlab);
407379

@@ -584,9 +556,6 @@ var pattern_assembler = function () {
584556
renderPattern: function (template, data, partials) {
585557
return renderPattern(template, data, partials);
586558
},
587-
check_build_state: function (pattern, patternlab) {
588-
return checkBuildState(pattern, patternlab);
589-
},
590559
process_pattern_iterative: function (file, patternlab) {
591560
return processPatternIterative(file, patternlab);
592561
},

core/lib/pseudopattern_hunter.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"use strict";
22

3+
var ch = require('./changes_hunter');
4+
35
var pseudopattern_hunter = function () {
46

57
function findpseudopatterns(currentPattern, patternlab) {
@@ -14,6 +16,7 @@ var pseudopattern_hunter = function () {
1416

1517
var pattern_assembler = new pa();
1618
var lineage_hunter = new lh();
19+
var changes_hunter = new ch();
1720
var paths = patternlab.config.paths;
1821

1922
//look for a pseudo pattern by checking if there is a file containing same
@@ -62,9 +65,10 @@ var pseudopattern_hunter = function () {
6265
engine: currentPattern.engine
6366
}, patternlab);
6467

68+
changes_hunter.checkBuildState(patternVariant, patternlab);
6569
patternlab.graph.add(patternVariant);
6670
patternlab.graph.link(patternVariant, currentPattern);
67-
pattern_assembler.check_build_state(currentPattern, patternlab);
71+
6872
//process the companion markdown file if it exists
6973
pattern_assembler.parse_pattern_markdown(patternVariant, patternlab);
7074

0 commit comments

Comments
 (0)