From 0ce8a47cb046e2ac4e05f7e0191d63e80e0323dc Mon Sep 17 00:00:00 2001 From: pfych Date: Sat, 18 Oct 2025 15:57:09 +1100 Subject: [PATCH] Tidy watcher --- packages/site/build/config.ts | 1 + packages/site/build/watch.ts | 78 +++++++++++++++++------------------ 2 files changed, 39 insertions(+), 40 deletions(-) diff --git a/packages/site/build/config.ts b/packages/site/build/config.ts index a1c3c2b..cdfe110 100644 --- a/packages/site/build/config.ts +++ b/packages/site/build/config.ts @@ -2,6 +2,7 @@ import { resolve } from 'node:path'; import { ensureDirectoryExists } from './utils/ensure-directory-exists'; import { createHash } from 'node:crypto'; +export const templatesDirectory = resolve('build/templates'); export const postsDirectory = resolve('posts'); export const scriptsDirectory = resolve('assets/scripts'); export const stylesDirectory = resolve('assets/styles'); diff --git a/packages/site/build/watch.ts b/packages/site/build/watch.ts index a6a3a59..5f4806c 100644 --- a/packages/site/build/watch.ts +++ b/packages/site/build/watch.ts @@ -9,48 +9,17 @@ import { postsDirectory, scriptsDirectory, stylesDirectory, + templatesDirectory, } from './config'; import { logger } from 'web-shared/src/logger'; -import { buildPost } from './steps/posts/posts'; +import { buildPost, buildPosts } from './steps/posts/posts'; import { createPostObject } from './steps/posts/create-post-object'; import { build } from './index'; -import { buildScript, buildScripts } from './steps/scripts/scripts'; -import { buildStyle, buildStyles } from './steps/styles/styles'; +import { buildScripts } from './steps/scripts/scripts'; +import { buildStyles } from './steps/styles/styles'; import { buildCustom } from './steps/custom/custom'; import { buildPages } from './steps/pages/pages'; -const onPostChange = async (event: Event) => { - const post = await createPostObject(event.path, postsDirectory); - logger.info( - { title: post.frontMatter.title, path: post.file.filePath }, - 'Rebuilding Post', - ); - - await buildPost(post); -}; - -const onScriptChange = async (event: Event) => { - logger.info({ path: event.path }, 'Rebuilding Script'); - await buildScript(event.path); - await buildScripts(); // We probably want to do a full rebuild to update imports/calls/etc -}; - -const onStyleChange = async (event: Event) => { - logger.info({ path: event.path }, 'Rebuilding Styles'); - await buildStyle(event.path); - await buildStyles(); -}; - -const onCustomChange = async (event: Event) => { - logger.info({ path: event.path }, 'Rebuilding Custom'); - await buildCustom(); -}; - -const onPagesChange = async (event: Event) => { - logger.info({ path: event.path }, 'Rebuilding Pages'); - await buildPages(); -}; - const createWatcher = async ( directory: string, func: (event: Event) => Promise, @@ -76,11 +45,40 @@ const createWatcher = async ( ); }; -await createWatcher(postsDirectory, onPostChange); -await createWatcher(scriptsDirectory, onScriptChange); -await createWatcher(stylesDirectory, onStyleChange); -await createWatcher(customDirectory, onCustomChange); -await createWatcher(pagesDirectory, onPagesChange); +await createWatcher(postsDirectory, async (event: Event) => { + const post = await createPostObject(event.path, postsDirectory); + logger.info( + { title: post.frontMatter.title, path: post.file.filePath }, + 'Rebuilding Post', + ); + + await buildPost(post); +}); + +await createWatcher(scriptsDirectory, async (event: Event) => { + logger.info({ path: event.path }, 'Rebuilding Script'); + await buildScripts(); +}); + +await createWatcher(stylesDirectory, async (event: Event) => { + logger.info({ path: event.path }, 'Rebuilding Styles'); + await buildStyles(); +}); + +await createWatcher(customDirectory, async (event: Event) => { + logger.info({ path: event.path }, 'Rebuilding Custom'); + await buildCustom(); +}); + +await createWatcher(pagesDirectory, async (event: Event) => { + logger.info({ path: event.path }, 'Rebuilding Pages'); + await buildPages(); +}); + +await createWatcher(templatesDirectory, async (event: Event) => { + logger.info({ path: event.path }, 'Rebuilding all Posts'); + await buildPosts(); +}); await build(); -- 2.51.2