Skip to content

Commit 9b48fd1

Browse files
committed
watch and emit events on global file change
part of #706
1 parent 20954f2 commit 9b48fd1

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

core/lib/asset_copy.js

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,55 @@ const asset_copier = () => {
104104
}
105105
});
106106

107-
//we need to special case patterns/**/*.md|.json|.pattern-extensions
107+
//we need to special case patterns/**/*.md|.json|.pattern-extensions as well as the global structures
108108
if (options.watch) {
109+
110+
// watch global structures, such as _data/* and _meta/
111+
const globalSources = [assetDirectories.source.data, assetDirectories.source.meta]
112+
const globalPaths = globalSources.map(globalSource => path.join(
113+
basePath,
114+
globalSource,
115+
'*'
116+
));
117+
118+
_.each(globalPaths, (globalPath) => {
119+
120+
if (patternlab.config.debug) {
121+
console.log(`Pattern Lab is watching ${globalPath} for changes`);
122+
}
123+
124+
const globalWatcher = chokidar.watch(
125+
path.resolve(globalPath),
126+
{
127+
ignored: /(^|[\/\\])\../,
128+
ignoreInitial: true,
129+
awaitWriteFinish : {
130+
stabilityThreshold: 200,
131+
pollInterval: 100
132+
}
133+
}
134+
);
135+
136+
//watch for changes and rebuild
137+
globalWatcher.on('addDir', (p) => {
138+
patternlab.events.emit('patternlab-global-change', {
139+
file: p
140+
})
141+
})
142+
.on('add', (p) => {
143+
patternlab.events.emit('patternlab-global-change', {
144+
file: p
145+
});
146+
}).on('change', (p) => {
147+
console.log(145, 'global change watch')
148+
patternlab.events.emit('patternlab-global-change', {
149+
file: p
150+
});
151+
});
152+
153+
});
154+
155+
// watch patterns
109156
const baseFileExtensions = ['.json', '.yml', '.yaml', '.md'];
110157
const patternWatches = baseFileExtensions.concat(patternlab.engines.getSupportedFileExtensions()).map(
111158
dotExtension => path.join(
@@ -143,7 +190,7 @@ const asset_copier = () => {
143190
}).on('change', (p) => {
144191
patternlab.events.emit('patternlab-pattern-change', {
145192
file: p
146-
});
193+
});
147194
});
148195
});
149196
}

core/lib/patternlab.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,14 @@ const patternlab_engine = function (config) {
681681
return Promise.resolve();
682682
});
683683

684+
this.events.on('patternlab-global-change', () => {
685+
if (!patternlab.isBusy) {
686+
options.cleanPublic = false;
687+
return this.build(callback, options);
688+
}
689+
return Promise.resolve();
690+
});
691+
684692
printDebug();
685693
patternlab.isBusy = false;
686694
callback();

0 commit comments

Comments
 (0)