Skip to content

Commit

Permalink
feat: replace pagefind cli call with node api
Browse files Browse the repository at this point in the history
  • Loading branch information
shishkin committed Jul 21, 2024
1 parent cdb9c06 commit 5188319
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions packages/astro-pagefind/src/pagefind.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { AstroIntegration } from "astro";
import { fileURLToPath } from "node:url";
import { execSync } from "child_process";
import path from "node:path";
import { createIndex } from "pagefind";
import sirv from "sirv";

export default function pagefind(): AstroIntegration {
Expand Down Expand Up @@ -46,18 +47,38 @@ export default function pagefind(): AstroIntegration {
}
});
},
"astro:build:done": ({ logger }) => {
"astro:build:done": async ({ logger }) => {
if (!outDir) {
logger.warn(
"astro-pagefind couldn't reliably determine the output directory. Search index will not be built.",
);
return;
}

const cmd = `npx pagefind --site "${outDir}"`;
execSync(cmd, {
stdio: [process.stdin, process.stdout, process.stderr],
const { index, errors: createErrors } = await createIndex({});
if (!index) {
logger.error("Pagefind failed to create index");
createErrors.forEach((e) => logger.error(e));
return;
}
const { page_count, errors: addErrors } = await index.addDirectory({ path: outDir });
if (addErrors.length) {
logger.error("Pagefind failed to index files");
addErrors.forEach((e) => logger.error(e));
return;
} else {
logger.info(`Pagefind indexed ${page_count} pages`);
}
const { outputPath, errors: writeErrors } = await index.writeFiles({
outputPath: path.join(outDir, "pagefind"),
});
if (writeErrors.length) {
logger.error("Pagefind failed to write index");
writeErrors.forEach((e) => logger.error(e));
return;
} else {
logger.info(`Pagefind wrote index to ${outputPath}`);
}
},
},
};
Expand Down

0 comments on commit 5188319

Please sign in to comment.