aboutsummaryrefslogtreecommitdiffstats
path: root/app/webserver/fileWatcher.ts
diff options
context:
space:
mode:
authorLeonardo Bishop <me@leonardobishop.com>2023-08-29 19:22:01 +0100
committerLeonardo Bishop <me@leonardobishop.com>2023-08-29 19:22:01 +0100
commit2620b9c21761759b6b57a3b29e58c0fbb739a413 (patch)
tree322188abbe4a1ef8a8a77f6413f671a95753a5dd /app/webserver/fileWatcher.ts
parentd5802a34218a56357a7ae88eff1d3cfa86d41bc7 (diff)
Add full rebuild feature, and remove incremental build
Diffstat (limited to 'app/webserver/fileWatcher.ts')
-rw-r--r--app/webserver/fileWatcher.ts42
1 files changed, 38 insertions, 4 deletions
diff --git a/app/webserver/fileWatcher.ts b/app/webserver/fileWatcher.ts
index 772ac1b..f9a4f6c 100644
--- a/app/webserver/fileWatcher.ts
+++ b/app/webserver/fileWatcher.ts
@@ -1,7 +1,7 @@
import chokidar, { FSWatcher } from 'chokidar';
import { logger } from '../logger.js';
import { PageDirectory } from '../builder/pageDirectory.js';
-import { rebuildSinglePage } from '../builder/buildProject.js';
+import { buildPages, rebuildSinglePage } from '../builder/buildProject.js';
import path from 'path';
import fs from 'fs';
@@ -92,6 +92,36 @@ function attachViewEvents(watcher: FSWatcher, pages: PageDirectory) {
watcher.on('unlink', onViewRemoval);
}
+let buildInProgress = false;
+
+function attachFullRebuildEvents(watcher: FSWatcher) {
+
+ const onFullRebuild = async (file: string) => {
+ if (buildInProgress) {
+ logger.info(`File ${file} has been modified, but a build is already in progress. Skipping...`);
+ return;
+ }
+ buildInProgress = true;
+
+ logger.info(`File ${file} has been modified, starting full rebuild...`);
+ const startDate = new Date();
+ const {success, errors, pageDirectory} = await buildPages(false);
+ const endDate = new Date();
+ const finishString = `...done${errors > 0 ? `, with ${errors} errors` : ''}, after ${endDate.getTime() - startDate.getTime()}ms.`;
+ if (!success) {
+ logger.error(finishString);
+ } else {
+ logger.info(finishString);
+ }
+ logger.info(``);
+ buildInProgress = false;
+ }
+
+ watcher.on('add', onFullRebuild);
+ watcher.on('change', onFullRebuild);
+ watcher.on('unlink', onFullRebuild);
+}
+
export const start = (pages: PageDirectory) => {
const pagesWatcher = chokidar.watch('.', {
persistent: true,
@@ -109,9 +139,13 @@ export const start = (pages: PageDirectory) => {
ignoreInitial: true,
});
- attachPageEvents(pagesWatcher, pages);
- attachStaticEvents(staticWatcher);
- attachViewEvents(viewsWatcher, pages);
+ // attachPageEvents(pagesWatcher, pages);
+ // attachStaticEvents(staticWatcher);
+ // attachViewEvents(viewsWatcher, pages);
+ //
+ attachFullRebuildEvents(pagesWatcher);
+ attachFullRebuildEvents(staticWatcher);
+ attachFullRebuildEvents(viewsWatcher);
const exitHandler = () => {
logger.info(`Stopping file watcher...`);