From b82f87ce590ed5ce1b7db470b91997648db6185e Mon Sep 17 00:00:00 2001 From: hugos68 Date: Sat, 25 May 2024 13:12:31 +0200 Subject: [PATCH] No throw anymore --- .changeset/clever-forks-mix.md | 5 +++ src/dev.ts | 60 +++++++++++++++++++++------------- 2 files changed, 43 insertions(+), 22 deletions(-) create mode 100644 .changeset/clever-forks-mix.md diff --git a/.changeset/clever-forks-mix.md b/.changeset/clever-forks-mix.md new file mode 100644 index 0000000..97ecd57 --- /dev/null +++ b/.changeset/clever-forks-mix.md @@ -0,0 +1,5 @@ +--- +"vite-plugin-pagefind": patch +--- + +Catch errors and log instead of throwing diff --git a/src/dev.ts b/src/dev.ts index 19abc0c..510e0b3 100644 --- a/src/dev.ts +++ b/src/dev.ts @@ -1,10 +1,13 @@ import { PluginOption } from 'vite'; import { resolve } from 'path'; import { existsSync, promises } from 'fs'; -import { execSync } from 'child_process'; +import { exec as exec_callback } from 'child_process'; import { get_pagefind_config } from './util/config.js'; import { console_log } from './util/log.js'; import { PACKAGE_NAME } from './util/constants.js'; +import { promisify } from 'util'; + +const exec = promisify(exec_callback); export default function dev(): PluginOption { return { @@ -13,19 +16,38 @@ export default function dev(): PluginOption { async config(vite_config) { const pagefind_config = get_pagefind_config(vite_config.root); - switch (pagefind_config.dev_strategy) { - case 'eager': { - console_log('Building site...'); - execSync(pagefind_config.build_command, { + async function build() { + console_log('Building site...'); + try { + await exec(pagefind_config.build_command, { cwd: vite_config.root }); + } catch (e) { + console_log( + `Failed to build site: ${e instanceof Error ? e.message : e}` + ); + } + } - console_log(`Copying pagefind bundle to assets dir...`); + async function copy_bundle() { + console_log(`Copying pagefind bundle to assets dir...`); + try { await promises.cp( resolve(pagefind_config.site_dir, 'pagefind'), resolve(pagefind_config.assets_dir, 'pagefind'), { recursive: true } ); + } catch (e) { + console_log( + `Failed to copy pagefind bundle: ${e instanceof Error ? e.message : e}` + ); + } + } + + switch (pagefind_config.dev_strategy) { + case 'eager': { + await build(); + await copy_bundle(); break; } case 'lazy': { @@ -33,25 +55,19 @@ export default function dev(): PluginOption { resolve(pagefind_config.assets_dir, 'pagefind') ); - if (!pagefind_in_assets) { - const pagefind_in_site = existsSync( - resolve(pagefind_config.site_dir, 'pagefind') - ); + if (pagefind_in_assets) { + return; + } - if (!pagefind_in_site) { - console_log('Building site...'); - execSync(pagefind_config.build_command, { - cwd: vite_config.root - }); - } + const pagefind_in_site = existsSync( + resolve(pagefind_config.site_dir, 'pagefind') + ); - console_log(`Copying pagefind bundle to assets dir...`); - await promises.cp( - resolve(pagefind_config.site_dir, 'pagefind'), - resolve(pagefind_config.assets_dir, 'pagefind'), - { recursive: true } - ); + if (!pagefind_in_site) { + await build(); } + + await copy_bundle(); break; } default: {