From 45ddaa4d38cd0bd51f126582fcddb3097493c101 Mon Sep 17 00:00:00 2001 From: Cory LaViska Date: Thu, 8 Jun 2023 14:46:12 -0400 Subject: [PATCH] cleaner watching --- docs/eleventy.config.cjs | 7 +++++++ scripts/build.js | 20 ++++++++++++++++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/docs/eleventy.config.cjs b/docs/eleventy.config.cjs index c82b3e24d..a1df2e605 100644 --- a/docs/eleventy.config.cjs +++ b/docs/eleventy.config.cjs @@ -195,6 +195,13 @@ module.exports = function (eleventyConfig) { hasBuiltSearchIndex = true; }); + // + // Send a signal to stdout that let's the build know we've reached this point + // + eleventyConfig.on('eleventy.after', () => { + console.log('[eleventy.after]'); + }); + // // Dev server options (see https://www.11ty.dev/docs/dev-server/#options) // diff --git a/scripts/build.js b/scripts/build.js index b829926bb..b6b2ade10 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -29,7 +29,8 @@ let buildResult; // process and an array of strings containing any output are included in the resolved promise. // async function buildTheDocs(watch = false) { - return new Promise((resolve, reject) => { + return new Promise(async (resolve, reject) => { + const afterSignal = '[eleventy.after]'; const args = ['@11ty/eleventy', '--quiet']; const output = []; @@ -45,12 +46,23 @@ async function buildTheDocs(watch = false) { }); child.stdout.on('data', data => { + if (data.includes(afterSignal)) return; // don't log the signal output.push(data.toString()); }); - child.on('close', () => { - resolve({ child, output }); - }); + if (watch) { + // The process doesn't terminate in watch mode so, before resolving, we listen for a known signal in stdout that + // tells us when the first build completes. + child.stdout.on('data', data => { + if (data.includes(afterSignal)) { + resolve({ child, output }); + } + }); + } else { + child.on('close', () => { + resolve({ child, output }); + }); + } }); }