From 2620b9c21761759b6b57a3b29e58c0fbb739a413 Mon Sep 17 00:00:00 2001 From: Leonardo Bishop Date: Tue, 29 Aug 2023 19:22:01 +0100 Subject: Add full rebuild feature, and remove incremental build --- app/webserver/fileWatcher.ts | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) (limited to 'app/webserver') 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...`); -- cgit v1.2.3-70-g09d2