Skip to content

Commit 5d09bb0

Browse files
drhossabmuenzenmeyerJosefBredereck
authored
Allow using --watch option in patternlab build (#1120)
* add unlink events to patternWatcher * reset patterns before updating * allow passing the watch flag * #1120: Fix eslint errors * #1120: Fix watch not taken in account Co-authored-by: Brian Muenzenmeyer <brian.muenzenmeyer@gmail.com> Co-authored-by: Josef Bredreck <slime.games@outlook.de>
1 parent cb7049f commit 5d09bb0

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed

packages/cli/bin/build.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ function build(config, options) {
1919
// Initiate Pattern Lab core with the config
2020
const patternLab = pl(config);
2121

22+
if (options && options.watch) {
23+
config.watch = options.watch;
24+
}
25+
2226
/**
2327
* Check whether a flag was passed for build
2428
* 1. Build only patterns
@@ -31,7 +35,7 @@ function build(config, options) {
3135
} else {
3236
// 2
3337
debug(`build: Building your project now into ${config.paths.public.root}`);
34-
return patternLab.build(config.cleanPublic);
38+
return patternLab.build(config);
3539
}
3640
}
3741

packages/cli/bin/patternlab.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ log.on('patternlab.error', err => console.log(err)); // eslint-disable-line
2020
log.on('patternlab.info', msg => console.log(msg)); // eslint-disable-line
2121

2222
// Conditionally register verbose logging
23-
const verboseLogs = verbose =>
24-
log.on('patternlab.debug', msg => console.log(msg)); // eslint-disable-line
23+
const verboseLogs = () => log.on('patternlab.debug', msg => console.log(msg)); // eslint-disable-line
2524

2625
// Conditionally unregister all logging
2726
const silenceLogs = () => {
@@ -58,7 +57,7 @@ cli
5857
.alias('compile')
5958
.description('Build Pattern Lab. Optionally (re-)build only the patterns')
6059
.option('-p, --patterns-only', 'Whether to only build patterns')
61-
.option('--no-watch', 'Start watching for changes')
60+
.option('--watch', 'Start watching for changes')
6261
.action(build);
6362

6463
/**

packages/core/src/lib/patternlab.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,11 @@ module.exports = class PatternLab {
307307
// dive once to perform iterative populating of patternlab object
308308
processAllPatternsIterative(patterns_dir) {
309309
const self = this;
310+
311+
// before updating the patterns has to be reset, otherwise
312+
// deleted pattern would still be present in the patterns array
313+
this.patterns = [];
314+
310315
const promiseAllPatternFiles = new Promise(function(resolve) {
311316
dive(
312317
patterns_dir,

packages/core/src/lib/watchPatternLabFiles.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,31 @@ const watchPatternLabFiles = (
131131
file: p,
132132
}
133133
);
134+
})
135+
// the watcher does not react on unlink and unlinkDir
136+
// events, so patterns are never removed
137+
.on('unlink', async p => {
138+
patternlab.graph.sync();
139+
patternlab.graph.upgradeVersion();
140+
await pluginMananger.raiseEvent(
141+
patternlab,
142+
events.PATTERNLAB_PATTERN_CHANGE,
143+
{
144+
file: p,
145+
}
146+
);
147+
})
148+
.on('unlinkDir', async p => {
149+
patternlab.graph.sync();
150+
patternlab.graph.upgradeVersion();
151+
await pluginMananger.raiseEvent(
152+
patternlab,
153+
events.PATTERNLAB_PATTERN_CHANGE,
154+
{
155+
file: p,
156+
}
157+
);
134158
});
135-
136159
patternlab.watchers[patternWatchPath] = patternWatcher;
137160
});
138161

0 commit comments

Comments
 (0)