Skip to content

Commit 20954f2

Browse files
committed
copy / watch / serve
part of #609 part of #706
1 parent c198aad commit 20954f2

File tree

2 files changed

+58
-14
lines changed

2 files changed

+58
-14
lines changed

core/lib/asset_copy.js

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
"use strict";
2-
const plutils = require('./utilities');
32
const _ = require('lodash');
43
const path = require('path');
54
const process = require('process');
65

7-
let copy = require('recursive-copy'); // eslint-disable-line
8-
let chokidar = require('chokidar'); // eslint-disable-line
6+
let copy = require('recursive-copy'); // eslint-disable-line prefer-const
7+
let chokidar = require('chokidar'); // eslint-disable-line prefer-const
98

109
const asset_copier = () => {
1110

@@ -44,7 +43,7 @@ const asset_copier = () => {
4443
if (options.debug) {
4544
console.log(`Moved ${p} to ${dest}`);
4645
}
47-
options.emitter.emit('patternlab-file-change', {
46+
options.emitter.emit('patternlab-asset-change', {
4847
file: p,
4948
dest: dest
5049
});
@@ -67,14 +66,14 @@ const asset_copier = () => {
6766
};
6867

6968
//loop through each directory asset object (source / public pairing)
70-
_.each(dirs, (dir, key) => {
69+
_.each(dirs, (dir) => {
7170

7271
//if we want to watch files, do so, otherwise just copy each file
7372
if (options.watch) {
7473
if (patternlab.config.debug) {
7574
console.log(`Pattern Lab is watching ${path.resolve(basePath, dir.source)} for changes`);
7675
}
77-
const watcher = chokidar.watch(
76+
const assetWatcher = chokidar.watch(
7877
path.resolve(basePath, dir.source),
7978
{
8079
ignored: /(^|[\/\\])\../,
@@ -87,7 +86,7 @@ const asset_copier = () => {
8786
);
8887

8988
//watch for changes and copy
90-
watcher.on('addDir', (p) => {
89+
assetWatcher.on('addDir', (p) => {
9190
const destination = path.resolve(basePath, dir.public + '/' + path.basename(p));
9291
copyFile(p, destination, copyOptions);
9392
}).on('add', (p) => {
@@ -105,14 +104,49 @@ const asset_copier = () => {
105104
}
106105
});
107106

108-
109107
//we need to special case patterns/**/*.md|.json|.pattern-extensions
110108
if (options.watch) {
111-
console.log(111, basePath, assetDirectories.source.patterns)
112-
const patterns = patternlab.engines.getSupportedFileExtensions().map(dotExtension => path.join(basePath, assetDirectories.source.patterns, `/**/*${dotExtension}`));
113-
console.log(112, patterns);
114-
}
109+
const baseFileExtensions = ['.json', '.yml', '.yaml', '.md'];
110+
const patternWatches = baseFileExtensions.concat(patternlab.engines.getSupportedFileExtensions()).map(
111+
dotExtension => path.join(
112+
basePath,
113+
assetDirectories.source.patterns,
114+
`/**/*${dotExtension}`
115+
)
116+
);
117+
_.each(patternWatches, (patternWatchPath) => {
118+
if (patternlab.config.debug) {
119+
console.log(`Pattern Lab is watching ${patternWatchPath} for changes`);
120+
}
121+
122+
const patternWatcher = chokidar.watch(
123+
path.resolve(patternWatchPath),
124+
{
125+
ignored: /(^|[\/\\])\../,
126+
ignoreInitial: true,
127+
awaitWriteFinish : {
128+
stabilityThreshold: 200,
129+
pollInterval: 100
130+
}
131+
}
132+
);
115133

134+
//watch for changes and rebuild
135+
patternWatcher.on('addDir', (p) => {
136+
patternlab.events.emit('patternlab-pattern-change', {
137+
file: p
138+
});
139+
}).on('add', (p) => {
140+
patternlab.events.emit('patternlab-pattern-change', {
141+
file: p
142+
});
143+
}).on('change', (p) => {
144+
patternlab.events.emit('patternlab-pattern-change', {
145+
file: p
146+
});
147+
});
148+
});
149+
}
116150
};
117151

118152
return {

core/lib/patternlab.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -669,8 +669,18 @@ const patternlab_engine = function (config) {
669669
}
670670
patternlab.isBusy = true;
671671
return buildPatterns(options.cleanPublic).then(() => {
672+
672673
new ui_builder().buildFrontend(patternlab);
673674
assetCopier().copyAssets(patternlab.config.paths, patternlab, options);
675+
676+
this.events.on('patternlab-pattern-change', () => {
677+
if (!patternlab.isBusy) {
678+
options.cleanPublic = false;
679+
return this.build(callback, options);
680+
}
681+
return Promise.resolve();
682+
});
683+
674684
printDebug();
675685
patternlab.isBusy = false;
676686
callback();
@@ -679,13 +689,13 @@ const patternlab_engine = function (config) {
679689
help: function () {
680690
help();
681691
},
682-
patternsonly: function (callback, deletePatternDir) {
692+
patternsonly: function (callback, options) {
683693
if (patternlab && patternlab.isBusy) {
684694
console.log('Pattern Lab is busy building a previous run - returning early.');
685695
return Promise.resolve();
686696
}
687697
patternlab.isBusy = true;
688-
return buildPatterns(deletePatternDir).then(() => {
698+
return buildPatterns(options.cleanPublic).then(() => {
689699
printDebug();
690700
patternlab.isBusy = false;
691701
callback();

0 commit comments

Comments
 (0)