Skip to content

Commit

Permalink
chore: minor changes to generate pdfs integration
Browse files Browse the repository at this point in the history
  • Loading branch information
sahithyandev committed Nov 3, 2024
1 parent f9ff946 commit 47c2267
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions plugins/generate-pdfs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { spawn } from "node:child_process";
import { type ChildProcessWithoutNullStreams, spawn } from "node:child_process";
import { existsSync } from "node:fs";
import { mkdir, readFile, readdir, writeFile } from "node:fs/promises";
import { dirname, join } from "node:path";
Expand All @@ -19,7 +19,7 @@ function outputFilename(filePath: string) {
);
}

async function waitForServer(url: string, timeout = 10000, interval = 500) {
async function waitForServer(url: string, timeout = 1000, interval = 500) {
const start = Date.now();

while (Date.now() - start < timeout) {
Expand All @@ -28,13 +28,9 @@ async function waitForServer(url: string, timeout = 10000, interval = 500) {

if (response.ok) {
console.log(`Server is ready at ${url}`);
return true; // Server responded successfully, it's ready
return true;
}
} catch {
// Ignore errors and try again after a delay
}

// Wait before retrying
} catch {}
await new Promise((resolve) => setTimeout(resolve, interval));
}

Expand All @@ -43,6 +39,7 @@ async function waitForServer(url: string, timeout = 10000, interval = 500) {

export default function generatePdfsIntegration(): AstroIntegration {
let browser: Browser;
let devServer: ChildProcessWithoutNullStreams;
return {
name: "generate-pdfs",
hooks: {
Expand Down Expand Up @@ -118,6 +115,9 @@ export default function generatePdfsIntegration(): AstroIntegration {
}),
),
);
devServer = spawn("astro", ["dev"]);
await waitForServer("http://localhost:4321/");
logger.info("dev server started");
},
"astro:build:done": async ({ dir, pages, logger }) => {
const pagesToExport: string[] = [];
Expand All @@ -128,10 +128,6 @@ export default function generatePdfsIntegration(): AstroIntegration {
pagesToExport.push(page.pathname);
}

const devServer = spawn("astro", ["dev"]);
await waitForServer("http://localhost:4321/");
logger.info("dev server started");

const outputDir = join(fileURLToPath(dir.toString()), "as-pdf");
if (!existsSync(outputDir)) {
await mkdir(outputDir, { recursive: true });
Expand Down

0 comments on commit 47c2267

Please sign in to comment.