forked from pattern-lab/patternlab-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchanges_hunter.js
More file actions
121 lines (105 loc) · 3.74 KB
/
changes_hunter.js
File metadata and controls
121 lines (105 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
'use strict';
const path = require('path');
const _ = require('lodash');
const CompileState = require('./object_factory').CompileState;
//this is mocked in unit tests
let fs = require('fs-extra'); //eslint-disable-line prefer-const
/**
* For detecting changed patterns.
* @constructor
*/
const ChangesHunter = function() {};
ChangesHunter.prototype = {
/**
* Checks the build state of a pattern by comparing the modification date of the rendered output
* file with the {@link Pattern.lastModified}. If the pattern was modified after the last
* time it has been rendered, it is flagged for rebuilding via {@link CompileState.NEEDS_REBUILD}.
*
* @param {Pattern} pattern
* @param patternlab
*
* @see {@link CompileState}
*/
checkBuildState: function(pattern, patternlab) {
//write the compiled template to the public patterns directory
const renderedTemplatePath =
patternlab.config.paths.public.patterns +
pattern.getPatternLink(patternlab, 'rendered');
//write the compiled template to the public patterns directory
const markupOnlyPath =
patternlab.config.paths.public.patterns +
pattern.getPatternLink(patternlab, 'markupOnly');
if (!pattern.compileState) {
pattern.compileState = CompileState.NEEDS_REBUILD;
}
_.each(patternlab.uikits, uikit => {
try {
// renderedTemplatePath required to display a single element
// Markup only is required for "View All" pages. It will get loaded later on.
// If any of these is missing, mark pattern for recompile
[renderedTemplatePath, markupOnlyPath].forEach(renderedFile => {
// Prevent error message if file does not exist
fs.accessSync(
path.join(process.cwd(), uikit.outputDir, renderedFile),
fs.F_OK
);
});
const outputLastModified = fs
.statSync(
path.join(process.cwd(), uikit.outputDir, renderedTemplatePath)
)
.mtime.getTime();
if (pattern.lastModified && outputLastModified > pattern.lastModified) {
pattern.compileState = CompileState.CLEAN;
}
} catch (e) {
// Output does not exist yet, force recompile
pattern.compileState = CompileState.NEEDS_REBUILD;
}
});
const node = patternlab.graph.node(pattern);
// IF we are rebuilding due to watching and incrementally building, force add patterns to graph
if (
patternlab.incrementalBuildsEnabled &&
Object.keys(patternlab.watchers).length
) {
patternlab.graph.add(pattern);
} else {
// Make the pattern known to the PatternGraph and remember its compileState
if (!node) {
patternlab.graph.add(pattern);
} else {
// Works via object reference, so we directly manipulate the node data here
node.compileState = pattern.compileState;
}
}
},
/**
* Updates {Pattern#lastModified} to the files modification date if the file was modified
* after {Pattern#lastModified}.
*
* @param {Pattern} currentPattern
* @param {string} file
*/
checkLastModified: function(currentPattern, file) {
if (file && fs.pathExistsSync(file)) {
try {
const stat = fs.statSync(file);
// Needs recompile whenever one of the patterns files (template, json, pseudopatterns) changed
currentPattern.lastModified = Math.max(
stat.mtime.getTime(),
currentPattern.lastModified || 0
);
} catch (e) {
// Ignore, not a regular file
}
}
},
needsRebuild: function(lastModified, p) {
if (p.compileState !== CompileState.CLEAN || !p.lastModified) {
return true;
}
return p.lastModified >= lastModified;
}
};
module.exports = ChangesHunter;