From b8a3a70425628efe51f07e72bfbd2d4e46ee3215 Mon Sep 17 00:00:00 2001 From: Levy Barbosa Date: Thu, 25 Nov 2021 01:55:12 -0300 Subject: [PATCH] Implemented --watch option --- bin/elm-live.js | 1 + lib/src/init.js | 1 + lib/src/messages.js | 4 ++-- lib/src/watch.js | 26 ++++++++++++++++++++++++++ 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/bin/elm-live.js b/bin/elm-live.js index e90c30f..3f97b7a 100755 --- a/bin/elm-live.js +++ b/bin/elm-live.js @@ -15,6 +15,7 @@ program // Server .option('-p, --port [port]', 'The port to bind to.', Math.floor, 8000) .option('-h, --host [host]', 'Set the host interface to attach the server to.', 'localhost') + .option('-w, --watch [to-watch]', 'Specify additional directories or files to watch for changes. (comma separated list)', v => v.split(',')) // SSL .option('-S, --ssl [ssl]', 'Start an https server instead of http.', false) diff --git a/lib/src/init.js b/lib/src/init.js index 8de1d2f..4c8a9f1 100644 --- a/lib/src/init.js +++ b/lib/src/init.js @@ -53,6 +53,7 @@ const init = program => ({ ssl: parseSsl(program), startPage: program.startPage || 'index.html', target: parseOutput(program.args), + toWatch: program.watch || [], verbose: program.verbose || false }) diff --git a/lib/src/messages.js b/lib/src/messages.js index 31ce1df..e00e841 100644 --- a/lib/src/messages.js +++ b/lib/src/messages.js @@ -107,8 +107,8 @@ const watchingDirs = sourceDirs => .join('\n ')} ` -const eventNotification = (event, path) => (isBrowser = false) => { - const msg = `You’ve ${event} \`${path}\`. Rebuilding!` +const eventNotification = (event, path, action = 'Rebuilding') => (isBrowser = false) => { + const msg = `You’ve ${event} \`${path}\`. ${action}!` return isBrowser ? msg : `${header} ${msg} diff --git a/lib/src/watch.js b/lib/src/watch.js index e7759be..6d90029 100644 --- a/lib/src/watch.js +++ b/lib/src/watch.js @@ -126,11 +126,13 @@ const watch = model => Async((reject, _) => { // Package file changes may result in changes to the set // of watched files watcher.close() + if(additionalWatcher) additionalWatcher.close() watch(model) } function closeAndReject (x) { watcher.close() + if(additionalWatcher) additionalWatcher.close() reject(x) } @@ -145,6 +147,30 @@ const watch = model => Async((reject, _) => { .fork(closeAndReject, watch) } ) + + /** + * This watcher only watches the files and directories passed by the --watch flag. + * These files are watched on its own watcher because changes to them shouldn't trigger an rebuild of the project, only a reload of the page. + */ + const additionalWatcher = model.toWatch.length ? + chokidar.watch(model.toWatch, { + ignoreInitial: true, + followSymlinks: false, + }).on( + 'all', + debounce((event, filePath) => { + const relativePath = path.relative(process.cwd(), filePath) + const eventName = eventNameMap[event] || event + const eventMsg = eventNotification(eventName, relativePath, 'Reloading') + model.log(eventMsg()) + + if (getPropOr(true, 'useServer')()) + sendMessage(update(model, { build: { action: model.getAction('reload') } })); + maybeUpdateToWatching(model) + }), + 100 + ) : null + }) module.exports = {