chore: extract withPageName util (#1389)
This commit is contained in:
+29
-1
@@ -1,4 +1,4 @@
|
||||
import { TextLineStream } from "./deps.ts";
|
||||
import { delay, Page, puppeteer, TextLineStream } from "./deps.ts";
|
||||
|
||||
export async function startFreshServer(options: Deno.CommandOptions) {
|
||||
const serverProcess = new Deno.Command(Deno.execPath(), {
|
||||
@@ -29,3 +29,31 @@ export async function startFreshServer(options: Deno.CommandOptions) {
|
||||
|
||||
return { serverProcess, lines, address };
|
||||
}
|
||||
|
||||
export async function withPageName(
|
||||
name: string,
|
||||
fn: (page: Page, address: string) => Promise<void>,
|
||||
) {
|
||||
const { lines, serverProcess, address } = await startFreshServer({
|
||||
args: ["run", "-A", name],
|
||||
});
|
||||
|
||||
try {
|
||||
await delay(100);
|
||||
const browser = await puppeteer.launch({ args: ["--no-sandbox"] });
|
||||
|
||||
try {
|
||||
const page = await browser.newPage();
|
||||
await fn(page, address);
|
||||
} finally {
|
||||
await browser.close();
|
||||
}
|
||||
} finally {
|
||||
await lines.cancel();
|
||||
|
||||
serverProcess.kill("SIGTERM");
|
||||
|
||||
// Wait until the process exits
|
||||
await serverProcess.status;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user