Skip to content

Commit

Permalink
No throw anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugos68 committed May 25, 2024
1 parent 44cc047 commit b82f87c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/clever-forks-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"vite-plugin-pagefind": patch
---

Catch errors and log instead of throwing
60 changes: 38 additions & 22 deletions src/dev.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -13,45 +16,58 @@ 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': {
const pagefind_in_assets = existsSync(
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: {
Expand Down

0 comments on commit b82f87c

Please sign in to comment.