Skip to content

Commit b7ba5b0

Browse files
committed
fix(loadPattern): Check proper data file paths for modification
1 parent 0fc9bd7 commit b7ba5b0

File tree

4 files changed

+84
-6
lines changed

4 files changed

+84
-6
lines changed

core/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,16 +373,16 @@ const patternlab_module = function(config) {
373373

374374
this.events.on('patternlab-pattern-change', () => {
375375
if (!patternlab.isBusy) {
376-
options.cleanPublic = false;
377376
return this.build(options);
378377
}
379378
return Promise.resolve();
380379
});
381380

382381
this.events.on('patternlab-global-change', () => {
383382
if (!patternlab.isBusy) {
384-
options.cleanPublic = true; //rebuild everything
385-
return this.build(options);
383+
return this.build(
384+
Object.assign({}, options, { cleanPublic: true }) // rebuild everything
385+
);
386386
}
387387
return Promise.resolve();
388388
});

core/lib/changes_hunter.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
'use strict';
2-
const fs = require('fs-extra');
32
const CompileState = require('./object_factory').CompileState;
43

4+
//this is mocked in unit tests
5+
let fs = require('fs-extra'); //eslint-disable-line prefer-const
6+
57
/**
68
* For detecting changed patterns.
79
* @constructor
@@ -73,7 +75,7 @@ ChangesHunter.prototype = {
7375
* @param {string} file
7476
*/
7577
checkLastModified: function(currentPattern, file) {
76-
if (file) {
78+
if (file && fs.pathExistsSync(file)) {
7779
try {
7880
const stat = fs.statSync(file);
7981

core/lib/loadPattern.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,15 @@ module.exports = function(relPath, patternlab) {
178178
//find any pattern parameters that may be in the current pattern
179179
currentPattern.parameteredPartials = currentPattern.findPartialsWithPatternParameters();
180180

181-
[templatePath, jsonFilename, listJsonFileName].forEach(file => {
181+
[
182+
templatePath,
183+
`${jsonFilename}.json`,
184+
`${jsonFilename}.yml`,
185+
`${jsonFilename}.yaml`,
186+
`${listJsonFileName}.json`,
187+
`${listJsonFileName}.yml`,
188+
`${listJsonFileName}.yaml`,
189+
].forEach(file => {
182190
changes_hunter.checkLastModified(currentPattern, file);
183191
});
184192

test/changes_hunter_tests.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
'use strict';
2+
3+
const tap = require('tap');
4+
const rewire = require('rewire');
5+
6+
const ch = rewire('../core/lib/changes_hunter');
7+
8+
const fsMock = {
9+
statSync: function() {
10+
return {
11+
mtime: {
12+
getTime: () => {
13+
console.log('????');
14+
return 100;
15+
},
16+
},
17+
};
18+
},
19+
pathExistsSync: () => {
20+
return true;
21+
},
22+
};
23+
24+
//set our mocks in place of usual require()
25+
ch.__set__({
26+
fs: fsMock,
27+
});
28+
29+
const changes_hunter = new ch();
30+
31+
tap.test('checkLastModified - sets lastModified to fileTime ', function(test) {
32+
//arrange
33+
const mockPattern = { lastModified: 0 };
34+
//act
35+
changes_hunter.checkLastModified(mockPattern, {});
36+
37+
//assert
38+
test.equals(mockPattern.lastModified, 100);
39+
test.end();
40+
});
41+
42+
tap.test(
43+
'checkLastModified - does not alter pattern if file not found',
44+
function(test) {
45+
//arrange
46+
const mockPattern = { lastModified: 1010 };
47+
//act
48+
changes_hunter.checkLastModified(mockPattern, null);
49+
50+
//assert
51+
test.equals(mockPattern.lastModified, 1010);
52+
test.end();
53+
}
54+
);
55+
56+
tap.test(
57+
'checkLastModified - uses pattern.lastModified if greater than file time',
58+
function(test) {
59+
//arrange
60+
const mockPattern = { lastModified: 101 };
61+
//act
62+
changes_hunter.checkLastModified(mockPattern, {});
63+
64+
//assert
65+
test.equals(mockPattern.lastModified, 101);
66+
test.end();
67+
}
68+
);

0 commit comments

Comments
 (0)