Skip to content

Allow using --watch option in patternlab build #1120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Aug 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/cli/bin/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ function build(config, options) {
// Initiate Pattern Lab core with the config
const patternLab = pl(config);

if (options && options.watch) {
config.watch = options.watch;
}

/**
* Check whether a flag was passed for build
* 1. Build only patterns
Expand All @@ -31,7 +35,7 @@ function build(config, options) {
} else {
// 2
debug(`build: Building your project now into ${config.paths.public.root}`);
return patternLab.build(config.cleanPublic);
return patternLab.build(config);
}
}

Expand Down
5 changes: 2 additions & 3 deletions packages/cli/bin/patternlab.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ log.on('patternlab.error', err => console.log(err)); // eslint-disable-line
log.on('patternlab.info', msg => console.log(msg)); // eslint-disable-line

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

// Conditionally unregister all logging
const silenceLogs = () => {
Expand Down Expand Up @@ -58,7 +57,7 @@ cli
.alias('compile')
.description('Build Pattern Lab. Optionally (re-)build only the patterns')
.option('-p, --patterns-only', 'Whether to only build patterns')
.option('--no-watch', 'Start watching for changes')
.option('--watch', 'Start watching for changes')
.action(build);

/**
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/lib/patternlab.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ module.exports = class PatternLab {
// dive once to perform iterative populating of patternlab object
processAllPatternsIterative(patterns_dir) {
const self = this;

// before updating the patterns has to be reset, otherwise
// deleted pattern would still be present in the patterns array
this.patterns = [];

const promiseAllPatternFiles = new Promise(function(resolve) {
dive(
patterns_dir,
Expand Down
25 changes: 24 additions & 1 deletion packages/core/src/lib/watchPatternLabFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,31 @@ const watchPatternLabFiles = (
file: p,
}
);
})
// the watcher does not react on unlink and unlinkDir
// events, so patterns are never removed
.on('unlink', async p => {
patternlab.graph.sync();
patternlab.graph.upgradeVersion();
await pluginMananger.raiseEvent(
patternlab,
events.PATTERNLAB_PATTERN_CHANGE,
{
file: p,
}
);
})
.on('unlinkDir', async p => {
patternlab.graph.sync();
patternlab.graph.upgradeVersion();
await pluginMananger.raiseEvent(
patternlab,
events.PATTERNLAB_PATTERN_CHANGE,
{
file: p,
}
);
});

patternlab.watchers[patternWatchPath] = patternWatcher;
});

Expand Down